Write history out to the persistent history file as compressed JSON.
(self)
| 5386 | self.history.start_session() |
| 5387 | |
| 5388 | def _persist_history(self) -> None: |
| 5389 | """Write history out to the persistent history file as compressed JSON.""" |
| 5390 | if not self.persistent_history_file: |
| 5391 | return |
| 5392 | |
| 5393 | try: |
| 5394 | import lzma as compress_lib |
| 5395 | except ModuleNotFoundError: # pragma: no cover |
| 5396 | import bz2 as compress_lib # type: ignore[no-redef] |
| 5397 | |
| 5398 | self.history.truncate(self._persistent_history_length) |
| 5399 | history_json = self.history.to_json() |
| 5400 | compressed_bytes = compress_lib.compress(history_json.encode(encoding="utf-8")) |
| 5401 | |
| 5402 | try: |
| 5403 | with open(self.persistent_history_file, "wb") as fobj: |
| 5404 | fobj.write(compressed_bytes) |
| 5405 | except OSError as ex: |
| 5406 | self.perror(f"Cannot write persistent history file '{self.persistent_history_file}': {ex}") |
| 5407 | |
| 5408 | @classmethod |
| 5409 | def _build_edit_parser(cls) -> Cmd2ArgumentParser: |