diff options
-rwxr-xr-x | imapArchive.py | 74 |
1 files changed, 30 insertions, 44 deletions
diff --git a/imapArchive.py b/imapArchive.py index 59cfa1e..c334144 100755 --- a/imapArchive.py +++ b/imapArchive.py @@ -1,6 +1,10 @@ #!/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 configparser @@ -70,17 +74,15 @@ class ArchivableFolder(list): self.append(msg) def doArchive(self): - if debug: - for message in self: - print >>sys.stderr, "Moving %s from %s to %s." \ - % (message, self.sourceFolder.getFullName(), - self.targetFolder.getFullName()) - else: - self.sourceFolder.copyMessages(array(self, Message), - self.targetFolder) - for message in self: - message.setFlag(Flags.Flag.DELETED, true) - self.sourceFolder.expunge() + for message in self: + log.debug("Moving %s from %s to %s.", + message, self.sourceFolder.getFullName(), + self.targetFolder.getFullName()) + # self.sourceFolder.copyMessages(array(self, Message), + # self.targetFolder) + # for message in self: + # message.setFlag(Flags.Flag.DELETED, true) + # self.sourceFolder.expunge() class Archives(dict): @@ -136,6 +138,9 @@ class Archives(dict): class ArchivedStore(object): + """ + TODO Make Archive store into context manager. + """ def __init__(self, serverKey=None): config = configparser.ConfigParser() @@ -155,31 +160,9 @@ class ArchivedStore(object): newmonth = self._threeMonthAgo.month - 3 self._threeMonthAgo = self._threeMonthAgo.replace(month=newmonth) - conffile = os.path.expanduser("~/.bugzillarc") - confUser = ConfigParser() - defSection = "localhost" - user, host, port, password = self.props.getProperty("user.name"),\ - "localhost", 993, "" - if confUser.read(conffile): - if confUser.has_section(defSection): - if confUser.has_option(defSection, "name"): - user = confUser.get(defSection, "name") - if confUser.has_option(defSection, "host"): - host = confUser.get(defSection, "host") - if confUser.has_option(defSection, "port"): - port = confUser.get(defSection, "port") - if confUser.has_option(defSection, "password"): - password = confUser.get(defSection, "password") - - if paramServer: - host = paramServer - if paramUser: - user = paramUser - if paramPassword: - password = paramPassword - - print >>sys.stderr, "host = %s, user = %s, password = %s" % \ - (host, user, password) + log.debug("host = %s, user = %s, password = %s", + self.cfg['host'], self.cfg['username'], + self.cfg['password']) self.__login(host, user, password) @@ -187,8 +170,8 @@ class ArchivedStore(object): try: self.store.connect(server, user, password) except: - print >> sys.stderr, "Cannot connect to %s as %s with password %s" %\ - (server, user, password) + log.debug("Cannot connect to %s as %s with password %s", + server, user, password) raise def archive(self): @@ -199,7 +182,7 @@ class ArchivedStore(object): if folder.getFullName()[:len("INBOX/Archiv")] != "INBOX/Archiv": archMsgsCnt = self.__archiveFolder(folder) # folder.close(False) - print "Processed messages = %d" % archMsgsCnt + print("Processed messages = %d" % archMsgsCnt) def __archiveFolder(self, fld): fld.open(Folder.READ_WRITE) @@ -211,8 +194,8 @@ class ArchivedStore(object): # print >> sys.stderr, archFolder # fld.copyMessages(array([msg],type(msg)), archFolder) # msg.setFlag(Flags.Flag.DELETED, true) - print "%s -> %s : %s" % (fld.getFullName(), - archFolder.getFullName(), msgDate) + print("%s -> %s : %s" % (fld.getFullName(), + archFolder.getFullName(), msgDate)) folderLen = len(fld.getMessages()) fld.close(False) return(folderLen) @@ -223,6 +206,9 @@ if __name__ == '__main__': help='Symbolic name of the server to be used') argp.add_argument('folder', help='Folder which should be archived') - myStore = ArchivedStore() - myStore.archive() - myStore.store.close() + argp.add_argument('archive', + help='Root folder to store annual archives to') + args = argp.parse_args() + + with ArchivedStore() as myStore: + myStore.archive() |