diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-29 19:11:38 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-29 19:11:38 -0400 |
commit | ede283decec7138b315b0c088a27c8f15c488335 (patch) | |
tree | 3cb96b3cf74809c072bcf063e8484de82a4681b5 /cmd2 | |
parent | f419192b71f816952b4f160c6b97986a5b8c9fd9 (diff) | |
download | cmd2-git-ede283decec7138b315b0c088a27c8f15c488335.tar.gz |
Creating directory of persistent history file if it does not exist
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/cmd2.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index adadbdf8..7c529acc 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3603,15 +3603,25 @@ class Cmd(cmd.Cmd): hist_file = os.path.abspath(os.path.expanduser(hist_file)) - # first we try and unpickle the history file - history = History() # on Windows, trying to open a directory throws a permission # error, not a `IsADirectoryError`. So we'll check it ourselves. if os.path.isdir(hist_file): - msg = "persistent history file '{}' is a directory" + msg = "Persistent history file '{}' is a directory" self.perror(msg.format(hist_file)) return + # Create the directory for the history file if it doesn't already exist + hist_file_dir = os.path.dirname(hist_file) + try: + os.makedirs(hist_file_dir, exist_ok=True) + except OSError as ex: + msg = "Error creating persistent history file directory '{}': {}".format(hist_file_dir, ex) + self.pexcept(msg) + return + + # first we try and unpickle the history file + history = History() + try: with open(hist_file, 'rb') as fobj: history = pickle.load(fobj) @@ -3619,7 +3629,7 @@ class Cmd(cmd.Cmd): # If any non-operating system error occurs when attempting to unpickle, just use an empty history pass except OSError as ex: - msg = "can not read persistent history file '{}': {}" + msg = "Can not read persistent history file '{}': {}" self.pexcept(msg.format(hist_file, ex)) return @@ -3655,7 +3665,7 @@ class Cmd(cmd.Cmd): with open(self.persistent_history_file, 'wb') as fobj: pickle.dump(self.history, fobj) except OSError as ex: - msg = "can not write persistent history file '{}': {}" + msg = "Can not write persistent history file '{}': {}" self.pexcept(msg.format(self.persistent_history_file, ex)) def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None: |