summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2022-05-11 14:53:52 +0200
committerAnton Khirnov <anton@khirnov.net>2022-05-11 14:53:52 +0200
commitba50cd942fd80dfca6e86a573e01b00b98b1f5f9 (patch)
tree4b748489f890d4614d203ff4499251c0b5dae0f0 /alot
parentcb36f43beb2ef07b9af243a16333b27a4f8067a2 (diff)
ui: move _{save,load}_history* out of the UI class
They are currently private staticmethods and there is no good reason to keep them inside UI, they only make the already large class larger.
Diffstat (limited to 'alot')
-rw-r--r--alot/ui.py117
1 files changed, 58 insertions, 59 deletions
diff --git a/alot/ui.py b/alot/ui.py
index b975a511..9e6de9eb 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -86,6 +86,55 @@ class _StatusBar(urwid.WidgetWrap):
for wgt, fmt in zip(self._text, fmtstrings):
wgt.set_text(fmt.format_map(info))
+def _load_history_from_file(path, size=-1):
+ """Load a history list from a file and split it into lines.
+
+ :param path: the path to the file that should be loaded
+ :type path: str
+ :param size: the number of lines to load (0 means no lines, < 0 means
+ all lines)
+ :type size: int
+ :returns: a list of history items (the lines of the file)
+ :rtype: list(str)
+ """
+ if size == 0:
+ return []
+ if os.path.exists(path):
+ with codecs.open(path, 'r', encoding='utf-8') as histfile:
+ lines = [line.rstrip('\n') for line in histfile]
+ if size > 0:
+ lines = lines[-size:]
+ return lines
+ else:
+ return []
+
+def _save_history_to_file(history, path, size=-1):
+ """Save a history list to a file for later loading (possibly in another
+ session).
+
+ :param history: the history list to save
+ :type history: list(str)
+ :param path: the path to the file where to save the history
+ :param size: the number of lines to save (0 means no lines, < 0 means
+ all lines)
+ :type size: int
+ :type path: str
+ :returns: None
+ """
+ if size == 0:
+ return
+ if size > 0:
+ history = history[-size:]
+ directory = os.path.dirname(path)
+ if not os.path.exists(directory):
+ os.makedirs(directory)
+ # Write linewise to avoid building a large string in menory.
+ with codecs.open(path, 'w', encoding='utf-8') as histfile:
+ for line in history:
+ histfile.write(line)
+ histfile.write('\n')
+
+
class UI:
"""
This class integrates all components of alot and offers
@@ -177,11 +226,11 @@ class UI:
self._sender_hist_file = os.path.join(self._cache, 'senders')
self._recipients_hist_file = os.path.join(self._cache, 'recipients')
size = settings.get('history_size')
- self.commandprompthistory = self._load_history_from_file(
+ self.commandprompthistory = _load_history_from_file(
self._cmd_hist_file, size=size)
- self.senderhistory = self._load_history_from_file(
+ self.senderhistory = _load_history_from_file(
self._sender_hist_file, size=size)
- self.recipienthistory = self._load_history_from_file(
+ self.recipienthistory = _load_history_from_file(
self._recipients_hist_file, size=size)
# set up main loop
@@ -755,12 +804,12 @@ class UI:
async def cleanup(self):
"""Do the final clean up before shutting down."""
size = settings.get('history_size')
- self._save_history_to_file(self.commandprompthistory,
- self._cmd_hist_file, size=size)
- self._save_history_to_file(self.senderhistory, self._sender_hist_file,
- size=size)
- self._save_history_to_file(self.recipienthistory,
- self._recipients_hist_file, size=size)
+ _save_history_to_file(self.commandprompthistory,
+ self._cmd_hist_file, size = size)
+ _save_history_to_file(self.senderhistory,
+ self._sender_hist_file, size = size)
+ _save_history_to_file(self.recipienthistory,
+ self._recipients_hist_file, size = size)
logging.info('cancelling pending tasks: %s', self._pending_tasks)
for t in self._pending_tasks:
@@ -774,55 +823,5 @@ class UI:
self._pending_tasks.add(task)
task.add_done_callback(self._pending_tasks.discard)
- @staticmethod
- def _load_history_from_file(path, size=-1):
- """Load a history list from a file and split it into lines.
-
- :param path: the path to the file that should be loaded
- :type path: str
- :param size: the number of lines to load (0 means no lines, < 0 means
- all lines)
- :type size: int
- :returns: a list of history items (the lines of the file)
- :rtype: list(str)
- """
- if size == 0:
- return []
- if os.path.exists(path):
- with codecs.open(path, 'r', encoding='utf-8') as histfile:
- lines = [line.rstrip('\n') for line in histfile]
- if size > 0:
- lines = lines[-size:]
- return lines
- else:
- return []
-
- @staticmethod
- def _save_history_to_file(history, path, size=-1):
- """Save a history list to a file for later loading (possibly in another
- session).
-
- :param history: the history list to save
- :type history: list(str)
- :param path: the path to the file where to save the history
- :param size: the number of lines to save (0 means no lines, < 0 means
- all lines)
- :type size: int
- :type path: str
- :returns: None
- """
- if size == 0:
- return
- if size > 0:
- history = history[-size:]
- directory = os.path.dirname(path)
- if not os.path.exists(directory):
- os.makedirs(directory)
- # Write linewise to avoid building a large string in menory.
- with codecs.open(path, 'w', encoding='utf-8') as histfile:
- for line in history:
- histfile.write(line)
- histfile.write('\n')
-
def get_cols_rows(self):
return self.mainloop.screen.get_cols_rows()