diff options
-rwxr-xr-x | archive_folder.py | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/archive_folder.py b/archive_folder.py index 8bc9017..86afaa0 100755 --- a/archive_folder.py +++ b/archive_folder.py @@ -7,7 +7,6 @@ import configparser import email import email.header import email.utils -import functools import locale import logging import imaplib @@ -74,6 +73,8 @@ class Folder(object): self.__create_missing = create self.folder_sep = self.__get_separator() + log.debug('self.__create_missing = %s, self.__list_folder = %s', + self.__create_missing, self.__list_folder()) if self.__create_missing and self.__list_folder() is None: self.__create_folder() @@ -87,15 +88,16 @@ class Folder(object): def __create_folder(self): sep = self.folder_sep split_name = self.fld_name.split(sep) + log.debug('split_name = %s', split_name) target = '' for part in split_name: + log.debug('part = %s', part) target += '{}{}'.format(part, self.folder_sep) - if self.__list_folder(target) is None: - self.box.create(target) - self.box.subscribe(target) - self.__list_folder.cache_clear() + fld = target.strip(self.folder_sep) + if self.__list_folder(fld) is None: + self.box.create(fld) + self.box.subscribe(fld) - @functools.lru_cache() def __list_folder(self, name=None, wildcards='*'): if name is None: name = self.fld_name @@ -129,19 +131,26 @@ class Folder(object): msgs = [] for uid in res[0].decode().split(' '): - msgs.append(Message(self.box, uid)) + msgs.append(Message(self, uid)) return msgs def emails_before(self, before_str): return self.__emails_search('BEFORE', before_str) def get_archive_folder(self, msg, aroot): - return self.folder_sep.join((aroot, msg.date.strftime("%Y"), self.fld_name)) + raw_name = self.folder_sep.join((aroot, msg.date.strftime("%Y"), + self.fld_name)) + return raw_name.strip('/') def move_messages(self, messages): assert self.selected == False, 'Target folder should not be selected.' if self.box.features_present.MOVE: - ok, data = self.box.uid('MOVE', messages, self.fld_name) + # We cannot move self.box.uid('MOVE', messages, self.fld_name) + # because IMAP4_SSL.uid has a protection against unknown + # commands. https://bugs.python.org/issue33336 + typ, dat = self.box._simple_command('UID', 'MOVE', + messages, self.fld_name) + ok, data = self.box._untagged_response(typ, dat, 'MOVE') log.debug('MOVE ok = %s, data = %s', ok, data) if ok != 'OK': raise FolderError('Cannot move messages to folder %s' % @@ -182,6 +191,7 @@ class EmailServer(object): self.cfg = dict(config.items(acc_name)) self.archive_root = archive_root self.box = self.__login(**self.cfg) + # self.box.debug = 4 def __login(self, host='localhost', username=None, password=None, ssl=None): box = imaplib.IMAP4_SSL(host=host) @@ -192,6 +202,7 @@ class EmailServer(object): ok, data = box.capability() capas = data[0].decode() + log.debug('capas = %s', capas) box.features_present = Capas._make(['MOVE' in capas, 'UIDPLUS' in capas]) return box |