aboutsummaryrefslogtreecommitdiffstats
path: root/archive_folder.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2018-04-22 02:03:06 +0200
committerMatěj Cepl <mcepl@cepl.eu>2018-04-22 02:03:06 +0200
commitca9285e80b9fa2df82b135b3684f933d05222f69 (patch)
tree5f78c55a9385cc7726390360abfa436c3731ee6c /archive_folder.py
parent75be2c284dc0647765046b55655ecd0dbb64f131 (diff)
downloadimapArch-ca9285e80b9fa2df82b135b3684f933d05222f69.tar.gz
Add some more tests.
Diffstat (limited to 'archive_folder.py')
-rwxr-xr-xarchive_folder.py56
1 files changed, 21 insertions, 35 deletions
diff --git a/archive_folder.py b/archive_folder.py
index 744fcba..4b86707 100755
--- a/archive_folder.py
+++ b/archive_folder.py
@@ -1,11 +1,6 @@
#!/usr/bin/env python3.6
# note http://docs.python.org/lib/module-doctest.html
# resp. file:///usr/share/doc/python-docs-*/html/lib/module-doctest.html
-# FIXME
-# read https://www.toptal.com/python/an-introduction-to-mocking-in-python
-# & https://blog.fugue.co/2016-02-11-python-mocking-101.html
-# for unittest.mock tutorials
-# for testing
import argparse
import collections
import configparser
@@ -17,6 +12,7 @@ import locale
import logging
import imaplib
import os
+import re
import sys
from datetime import date, timedelta
@@ -34,6 +30,7 @@ class MessageError(IOError):
pass
Capas = collections.namedtuple('Capas', ['MOVE', 'UIDPLUS'])
+SEP_RE = re.compile(r'\s+"([/.])"\s+')
class Message(object):
@@ -69,14 +66,11 @@ class Message(object):
class Folder(object):
def __init__(self, box, fld_name, create=False):
self.box = box
- log.debug('fld_name = %s', fld_name)
self.fld_name = fld_name
self.selected = False
self.__create_missing = create
- self.__folder_sep = self.__get_separator()
+ self.folder_sep = self.__get_separator()
- log.debug('create_missing = %s', self.__create_missing)
- log.debug('list_folder = %s', self.__list_folder)
if self.__create_missing and self.__list_folder() is None:
self.__create_folder()
@@ -88,13 +82,11 @@ class Folder(object):
return self
def __create_folder(self):
- sep = self.__folder_sep
+ 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)
+ target += '{}{}'.format(part, self.folder_sep)
if self.__list_folder(target) is None:
self.box.create(target)
self.box.subscribe(target)
@@ -117,14 +109,12 @@ class Folder(object):
"""
data = self.__list_folder()
- log.debug('data = %s (%s)', data, type(data))
-
if data is None:
data = self.__list_folder('""', '""')
- data = data.decode().split(' ')
- if len(data) == 3:
- return data[1].strip(' "')
+ parse_data = SEP_RE.search(data.decode())
+ if parse_data is not None:
+ return parse_data.group(1)
else:
raise ServerError('Cannot find folder separator from %s' % data)
@@ -143,26 +133,24 @@ class Folder(object):
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))
+ return self.folder_sep.join((aroot, msg.date.strftime("%Y"), self.fld_name))
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',
- '%s %s' % (messages, self.fld_name))
+ ok, data = self.box.uid('MOVE', messages, self.fld_name)
log.debug('MOVE ok = %s, data = %s', ok, data)
if ok != 'OK':
raise FolderError('Cannot move messages to folder %s' %
self.fld_name)
elif self.box.features_present.UIDPLUS:
- ok, data = self.box.uid('COPY',
- '%s %s' % (messages, self.fld_name))
+ ok, data = self.box.uid('COPY', messages, self.fld_name)
log.debug('COPY ok = %s, data = %s', ok, data)
if ok != 'OK':
raise FolderError('Cannot copy messages to folder %s' %
self.fld_name)
ok, data = self.box.uid('STORE',
- r'+FLAGS.SILENT (\DELETED) %s' % messages)
+ r'+FLAGS.SILENT (\DELETED)', messages)
log.debug('STORE ok = %s, data = %s', ok, data)
if ok != 'OK':
raise FolderError('Cannot delete messages-')
@@ -171,14 +159,13 @@ class Folder(object):
if ok != 'OK':
raise FolderError('Cannot expunge messages.')
else:
- ok, data = self.box.uid('COPY',
- '%s %s' % (messages, self.fld_name))
+ ok, data = self.box.uid('COPY', messages, self.fld_name)
log.debug('COPY ok = %s, data = %s', ok, data)
if ok != 'OK':
raise FolderError('Cannot copy messages to folder %s' %
self.fld_name)
ok, data = self.box.uid('STORE',
- r'+FLAGS.SILENT (\DELETED) %s' % messages)
+ r'+FLAGS.SILENT (\DELETED)', messages)
log.debug('STORE ok = %s, data = %s', ok, data)
if ok != 'OK':
raise FolderError('Cannot delete messages-')
@@ -191,12 +178,11 @@ class EmailServer(object):
else config['general']['account']
self.cfg = dict(config.items(acc_name))
self.archive_root = archive_root
- self.__box = self.__login(**self.cfg)
+ self.box = self.__login(**self.cfg)
def __login(self, host='localhost', username=None, password=None, ssl=None):
box = imaplib.IMAP4_SSL(host=host)
- res = box.login(username, password)
- ok, data = res
+ ok, data = box.login(username, password)
if ok != 'OK':
raise ServerError('Cannot login with credentials %s' %
str((host, username, password,)))
@@ -204,9 +190,9 @@ class EmailServer(object):
ok, data = box.capability()
capas = data[0].decode()
box.features_present = Capas._make(['MOVE' in capas, 'UIDPLUS' in capas])
+ return box
@staticmethod
- @functools.lru_cache()
def get_config():
# In case the configuration file is missing, only empty list will be
# returned
@@ -223,7 +209,7 @@ class EmailServer(object):
:param: before_date
"""
copy_cache = {}
- fld = Folder(self.__box, folder_name).select()
+ fld = Folder(self.box, folder_name).select()
before_str = before_date.strftime('%d-%b-%Y')
for msg in fld.emails_before(before_str):
arch_folder = fld.get_archive_folder(msg, self.archive_root)
@@ -242,8 +228,8 @@ class EmailServer(object):
msg_ids = ','.join([x.uid for x in copy_cache[arch_dir]])
log.debug('arch_dir = %s, msgs = %s', arch_dir,
msg_ids)
- dir = Folder(self.__box, arch_dir, create=True)
- dir.move_messages(msg_ids)
+ dir = Folder(self.box, arch_dir, create=True)
+ # dir.move_messages(msg_ids)
def __enter__(self):
return self
@@ -251,7 +237,7 @@ class EmailServer(object):
def __exit__(self, *args):
if args != (None, None, None):
log.warning('args = %s', args)
- self.__box.close()
+ self.box.close()
if __name__ == '__main__':