changeset 1:479badee55ce

Sketching in argument parsing.
author Brian Neal <bgneal@gmail.com>
date Wed, 16 May 2012 20:52:51 -0500
parents f53fab34ba78
children 3c5c1269e037
files weighmail/config.py weighmail/main.py
diffstat 2 files changed, 43 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/weighmail/config.py	Tue May 15 20:52:12 2012 -0500
+++ b/weighmail/config.py	Wed May 16 20:52:51 2012 -0500
@@ -7,6 +7,14 @@
     pass
 
 
+DEFAULTS = dict(
+    username=None,
+    password=None,
+    host='imap.gmail.com',
+    ssl='True',
+    port='993',
+)
+
 LIMIT_RE = re.compile(r'^(\d+)(GB|MB|KB)?$', re.IGNORECASE)
 
 KB = 1024
@@ -20,26 +28,21 @@
     'GB': GB
 }
 
+Label = collections.namedtuple('Label', 'name min max')
+Options = collections.namedtuple('Options', DEFAULTS.keys() + ['labels'])
+
 
 def parse_config_file(path):
     """Parse INI file containing configuration details.
 
     """
     # Parse options file
-    defaults = dict(
-        username=None,
-        password=None,
-        host='imap.gmail.com',
-        ssl='True',
-        port='993',
-    )
-    parser = SafeConfigParser(defaults=defaults, allow_no_value=True)
+    parser = SafeConfigParser(defaults=DEFAULTS)
 
     with open(path, 'r') as fp:
         parser.readfp(fp)
 
     # Build a list of label named tuples
-    Label = collections.namedtuple('Label', 'name min max')
 
     label_set = set(parser.sections()) - {'auth', 'connection'}
     if not label_set:
@@ -57,8 +60,6 @@
         labels.append(Label(name=label, min=min_val, max=max_val))
 
     # Build an options object and return it
-    fields = defaults.keys() + ['labels']
-    Options = collections.namedtuple('Options', fields)
 
     opts = Options(
         username=parser.get('auth', 'username'),
--- a/weighmail/main.py	Tue May 15 20:52:12 2012 -0500
+++ b/weighmail/main.py	Wed May 16 20:52:51 2012 -0500
@@ -1,8 +1,39 @@
+from argparse import ArgumentParser
+import os.path
+
 from config import parse_config_file
 
 
+PROG_DESC = "Adds labels to your Gmail according to message size"
+
+
+def parse_args():
+    parser = ArgumentParser(description=PROG_DESC,
+        epilog="Command-line arguments override config file settings.")
+
+    default_config_file = os.path.expanduser(os.path.join('~',
+        '.weighmail.ini'))
+    parser.add_argument('-c', '--config', default=default_config_file,
+            help="path to configuration file [default=%(default)s]")
+    parser.add_argument('-u', '--user', default=None,
+            help="Gmail username")
+    parser.add_argument('-p', '--password', default=None,
+            help="Gmail password")
+    parser.add_argument('-H', '--host', default='imap.gmail.com',
+            help="Gmail server name [default=%(default)s]")
+    parser.add_argument('-P', '--port', default=993, type=int,
+            help="Gmail server port [default=%(default)s]")
+    parser.add_argument('-n', '--nossl', action='store_true',
+            help="do not use SSL [default=%(default)s]")
+
+    args = parser.parse_args()
+    print args
+    print
+
 def main():
+    parse_args()
     opts = parse_config_file('weighmail.ini')
+    print opts
 
 
 if __name__ == '__main__':