diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-30 11:19:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-30 11:19:09 -0400 |
commit | dca6d251ffd92250ed74b133ff5aece2d6ec9cb4 (patch) | |
tree | 5ae857fef44694c8fcfb5b2fe7a9698dca9e0c6a /cmd2/cmd2.py | |
parent | a5e52820df8da804347ef5a59519758f547bcb0f (diff) | |
parent | 179475c3fdb016aa33e69ba2888ff92e01c42869 (diff) | |
download | cmd2-git-dca6d251ffd92250ed74b133ff5aece2d6ec9cb4.tar.gz |
Merge pull request #744 from python-cmd2/make_history_directory
Create directory for the persistent history file if it does not already exist
Diffstat (limited to 'cmd2/cmd2.py')
-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: |