Commits

Felix Friedrich committed 79502fc

removed config file mess

Comments (0)

Files changed (3)

mailbox_cleanup/mailbox_cleanup_cli.py

 # -*- coding: UTF-8 -*-
 
 import sys
+import os
 
-from mailbox_cleanup.util import download_attachments, commandline_arguments, accounts
+from mailbox_cleanup.util import download_attachments
 
 
 OK = 0
 def main():
     args = commandline_arguments()
 
-    if args.list_accounts:
-        for account in accounts():
-            print account
-        sys.exit(OK)
-
     if args.download_attachments:
-        if args.account is None:
-            print 'Please provide an account name. Use -a option.'
-        if not args.account in accounts():
-            print 'Account', args.account, 'not configured.'
-            sys.exit(ACCOUNT_DOES_NOT_EXIST)
-        else:
-            print args.account
-            download_attachments(args.account)
+        download_attachments(args.host, args.port, args.ssl, args.username, args.folder, args.password)
         sys.exit(OK)
     else:
         print 'Please use --help to list options'
         sys.exit(OK)
 
 
+def commandline_arguments():
+    import argparse
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-d', '--download-attachments', action='store_true')
+    parser.add_argument('-n', '--dry-run', action='store_true', help='do not perform actions. e.g. saving attachments')
+    parser.add_argument('--host', type=unicode, help='host of IMAP server')
+    parser.add_argument('--port', type=int, default=143, help='port of IMAP server')
+    parser.add_argument('--ssl', action='store_true', default=False, help='use SSL')
+    parser.add_argument('-u', '--username', type=unicode, help='username for IMAP login')
+    default_folder = '~/attachments/'
+    parser.add_argument('-f', '--folder', type=unicode, help='folder to save attachments. default: {}'.format(default_folder), default=default_folder)
+    parser.add_argument('-p', '--password', type=unicode, default=None, help='passwort for IMAP login. default: ask on commandline')
+    args = parser.parse_args()
+    args.folder = os.path.expanduser(args.folder)
+    return parser.parse_args()
+
+
 if __name__ == '__main__':
     main()

mailbox_cleanup/tests/config_tests.py

-# -*- coding: utf-8 -*-
-
-import unittest
-import StringIO
-
-from mailbox_cleanup.util import config, example_accountname, example_host
-
-
-class TestConfig(unittest.TestCase):
-
-    def setUp(self):
-        self.configfile = StringIO.StringIO()
-
-    def test_example_account_exists(self):
-        try:
-            config('mail@example.com', 'host', filename=StringIO.StringIO())
-        except NoSectionError:
-            self.assertTrue(False)
-
-    def test_example_host(self):
-        host = config(example_accountname, 'host', filename=StringIO.StringIO())
-        self.assertEqual(host, example_host)
-
-
-if __name__ == '__main__':
-    unittest.main()

mailbox_cleanup/util.py

 from imapclient import IMAPClient
 
 
-config_filename = os.path.expanduser('~/.mailbox-cleanup.config')
-example_accountname = 'mail@example.com'
-example_destination = os.path.expanduser('~/attachments/')
-example_host = 'imap.example.com'
-example_user = 'user'
-example_ssl = True
-
-
-def download_attachments(account):
-    server = IMAPClient(config(account, 'host'), use_uid=True, ssl=config(account, 'ssl'))
-    server.login(config(account, 'username'), getpass.getpass())
+def download_attachments(host, port, ssl, username, destination, password=None):
+    print 'SSL', ssl
+    server = IMAPClient(host, port, use_uid=True, ssl=ssl)
+    if not password:
+        password = getpass.getpass()
+    server.login(username, password)
     server.select_folder('INBOX')
 
     print 'Downloading attachments:'
                 sender_list.add(sender)
                 sender_as_filename.add(normalize_filename(sender))
 
-            process_message(message, config(account, 'destination'))
+            process_message(message, destination)
 
     fp = open('sender.list', 'w')
     fp.write('\n'.join(sender_list))
     fp.write('\n'.join(names))
 
 
-def commandline_arguments():
-    import argparse
-    parser = argparse.ArgumentParser()
-    parser.add_argument('-l', '--list-accounts', action='store_true', help='list accounts')
-    parser.add_argument('-a', '--account', type=unicode, help='account name')
-    parser.add_argument('-d', '--download-attachments', action='store_true', help='increase output verbosity')
-    parser.add_argument('-n', '--dry-run', action='store_true', help='do not perform actions. e.g. saving attachments')
-    return parser.parse_args()
 
 
-def process_message(message, destination):
+def process_message(message, destination, dry_run=False):
     if message.is_multipart():
         for i in range(0, len(message.get_payload())):
             payload = message.get_payload(i)
             filename = message.get_filename()
             normalized_filename = normalize_filename(filename)
 
-            if not commandline_arguments().dry_run:
+            if not dry_run:
+                # TODO check if file already exists
                 fp = open(destination + normalized_filename, 'wb')
                 fp.write(payload)
 
     else:
         return filename, ''
 
-
-def config(account, option, filename=config_filename):
-    config = ConfigParser.ConfigParser()
-
-    if testmode(filename):
-        add_default_data(config)
-        config.write(filename)
-    else:
-        if config_file_not_exists(filename):
-            add_default_data(config)
-            config.write(filename)
-
-        config.read(filename)
-        if not os.path.exists(config.get(account, 'destination')):
-            os.makedirs(config.get(account, 'destination'))
-
-    config.read(filename)
-    return config.get(account, option)
-
-
-def accounts():
-    config = ConfigParser.ConfigParser()
-    if config_file_not_exists(config_filename):
-        add_default_data(config)
-        config.write(open(config_filename, 'w'))
-    config.read(config_filename)
-    return config.sections()
-
-
-def add_default_data(config):
-    account = example_accountname
-    config.add_section(account)
-    config.set(account, 'host', example_host)
-    config.set(account, 'username', example_user)
-    config.set(account, 'ssl', example_ssl)
-    config.set(account, 'destination', example_destination)
-
-
-def testmode(filename):
-    from StringIO import StringIO
-    return isinstance(filename, StringIO)
-
-
-def config_file_not_exists(filename):
-    return not os.path.exists(filename)