Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle absent lzma library #1318

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4813,7 +4813,12 @@ def _initialize_history(self, hist_file: str) -> None:
to this file when the application exits.
"""
import json
import lzma
try:
import lzma
bz2 = None
kmvanbrunt marked this conversation as resolved.
Show resolved Hide resolved
except ModuleNotFoundError: # pragma: no cover
lzma = None
import bz2

self.history = History()
# with no persistent history, nothing else in this method is relevant
Expand Down Expand Up @@ -4841,7 +4846,10 @@ def _initialize_history(self, hist_file: str) -> None:
try:
with open(hist_file, 'rb') as fobj:
compressed_bytes = fobj.read()
history_json = lzma.decompress(compressed_bytes).decode(encoding='utf-8')
if lzma is not None:
history_json = lzma.decompress(compressed_bytes).decode(encoding='utf-8')
else:
history_json = bz2.decompress(compressed_bytes).decode(encoding='utf-8')
self.history = History.from_json(history_json)
except FileNotFoundError:
# Just use an empty history
Expand Down Expand Up @@ -4879,15 +4887,23 @@ def _initialize_history(self, hist_file: str) -> None:

def _persist_history(self) -> None:
"""Write history out to the persistent history file as compressed JSON"""
import lzma
try:
import lzma
bz2 = None
except ModuleNotFoundError: # pragma: no cover
lzma = None
import bz2

if not self.persistent_history_file:
return

self.history.truncate(self._persistent_history_length)
try:
history_json = self.history.to_json()
compressed_bytes = lzma.compress(history_json.encode(encoding='utf-8'))
if lzma is not None:
compressed_bytes = lzma.compress(history_json.encode(encoding='utf-8'))
else:
compressed_bytes = bz2.compress(history_json.encode(encoding='utf-8'))

with open(self.persistent_history_file, 'wb') as fobj:
fobj.write(compressed_bytes)
Expand Down
Loading