]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added support for interact.py to read livetest.py config files (#66)
authorMenno Smits <menno@freshfoo.com>
Tue, 17 May 2011 20:39:43 +0000 (21:39 +0100)
committerMenno Smits <menno@freshfoo.com>
Tue, 17 May 2011 20:39:43 +0000 (21:39 +0100)
imapclient/config.py [new file with mode: 0644]
interact.py
livetest.py

diff --git a/imapclient/config.py b/imapclient/config.py
new file mode 100644 (file)
index 0000000..f710b9c
--- /dev/null
@@ -0,0 +1,60 @@
+from ConfigParser import SafeConfigParser, NoOptionError
+import imapclient
+
+
+def parse_config_file(path):
+    """Parse INI files containing IMAP connection details.
+
+    Used by livetest.py and interact.py
+    """
+    parser = SafeConfigParser(dict(ssl='false',
+                                   username=None,
+                                   password=None,
+                                   oauth='false',
+                                   oauth_url=None,
+                                   oauth_token=None,
+                                   oauth_token_secret=None))
+    fh = file(path)
+    parser.readfp(fh)
+    fh.close()
+    section = 'main'
+    assert parser.sections() == [section], 'Only expected a [main] section'
+
+    try:
+        port = parser.getint(section, 'port')
+    except NoOptionError:
+        port = None
+        
+    return Bunch(
+        host=parser.get(section, 'host'),
+        port=port,
+        ssl=parser.getboolean(section, 'ssl'),
+        username=parser.get(section, 'username'),
+        password=parser.get(section, 'password'),
+        oauth=parser.getboolean(section, 'oauth'),
+        oauth_url=parser.get(section, 'oauth_url'),
+        oauth_token=parser.get(section, 'oauth_token'),
+        oauth_token_secret=parser.get(section, 'oauth_token_secret'),
+    )
+
+def create_client_from_config(conf):
+    client = imapclient.IMAPClient(conf.host, port=conf.port, ssl=conf.ssl)
+    if conf.oauth:
+        client.oauth_login(conf.oauth_url,
+                           conf.oauth_token,
+                           conf.oauth_token_secret)
+    else:
+        client.login(conf.username, conf.password)
+    return client
+
+class Bunch(dict):
+
+    def __getattr__(self, k):
+        try:
+            return self[k]
+        except KeyError:
+            raise AttributeError
+
+    def __setattr__(self, k, v):
+        self[k] = v
+
index 7ebd865b970b3f5ca9e67edfd9bd9ada9e9486a8..faa151bf2c27ba7251bcc114f71714b073621b18 100755 (executable)
@@ -6,10 +6,10 @@
 
 
 import code
-from optparse import OptionParser
-import imapclient
 from getpass import getpass
+from optparse import OptionParser
 
+from imapclient.config import parse_config_file, create_client_from_config
 
 def command_line():
     p = OptionParser()
@@ -20,31 +20,35 @@ def command_line():
     p.add_option('-p', '--password', dest='password', action='store',
                  help='Password to login with')
     p.add_option('-P', '--port', dest='port', action='store',
-                 default=143, help='IMAP port to use (default is 143)')
+                 default=None, help='IMAP port to use (default is 143)')
     p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False,
                  help='Use SSL connection')
+    p.add_option('-f', '--file', dest='file', action='store', default=None,
+                 help='Config file (same as livetest.py)')
 
-    options, args = p.parse_args()
+    opts, args = p.parse_args()
     if args:
         p.error('unexpected arguments %s' % ' '.join(args))
 
-    # Get compulsory options if not given on the command line
-    for opt_name in ('host', 'username', 'password'):
-        if not getattr(options, opt_name):
-            setattr(options, opt_name, getpass(opt_name + ': '))
-
-    return options
-
-
-def setup_client():
-    options = command_line()
-    client = imapclient.IMAPClient(options.host, ssl=options.ssl)
-    client.login(options.username, options.password)
-    return client
+    if opts.file:
+        if opts.host or opts.username or opts.password or opts.port or opts.ssl:
+            p.error('If -f/--file is given no other options can be used')
+        # Use the options in the config file
+        opts = parse_config_file(opts.file)
+    else:
+        # Get compulsory options if not given on the command line
+        for opt_name in ('host', 'username', 'password'):
+            if not getattr(opts, opt_name):
+                setattr(opts, opt_name, getpass(opt_name + ': '))
+        if not opts.port:
+            opts.port = 143
+        opts.oauth = False     # OAUTH not supported on command line
+    return opts
 
 
 if __name__ == '__main__':
-    client = setup_client()
+    opts = command_line()
+    client = create_client_from_config(opts)
     banner = '\nIMAPClient instance is "c"'
     try:
         from IPython.Shell import IPShellEmbed
index b00128043f159a6f3a6b3b02f8b1aec3daef779a..539c0964b2de7a11fc5108314dedb6735827df39 100755 (executable)
@@ -10,12 +10,12 @@ import os
 import sys
 import time
 from datetime import datetime
-from ConfigParser import SafeConfigParser, NoOptionError
 
 import imapclient
 from imapclient.test.util import unittest
+from imapclient.config import parse_config_file, create_client_from_config
 
-# TODO cleaner verbose output: avoid "__main__" and separater between classes
+# TODO cleaner verbose output: avoid "__main__" and separator between classes
 
 
 SIMPLE_MESSAGE = 'Subject: something\r\n\r\nFoo\r\n'
@@ -499,48 +499,6 @@ def have_matching_types(a, b, type_or_types):
         return False
     return isinstance(b, type(a))
 
-class Bunch(dict):
-
-    def __getattr__(self, k):
-        try:
-            return self[k]
-        except KeyError:
-            raise AttributeError
-
-    def __setattr__(self, k, v):
-        self[k] = v
-
-def parse_config_file(path):
-    parser = SafeConfigParser(dict(ssl='false',
-                                   username=None,
-                                   password=None,
-                                   oauth='false',
-                                   oauth_url=None,
-                                   oauth_token=None,
-                                   oauth_token_secret=None))
-    fh = file(path)
-    parser.readfp(fh)
-    fh.close()
-    section = 'main'
-    assert parser.sections() == [section], 'Only expected a [main] section'
-
-    try:
-        port = parser.getint(section, 'port')
-    except NoOptionError:
-        port = None
-        
-    return Bunch(
-        host=parser.get(section, 'host'),
-        port=port,
-        ssl=parser.getboolean(section, 'ssl'),
-        username=parser.get(section, 'username'),
-        password=parser.get(section, 'password'),
-        oauth=parser.getboolean(section, 'oauth'),
-        oauth_url=parser.get(section, 'oauth_url'),
-        oauth_token=parser.get(section, 'oauth_token'),
-        oauth_token_secret=parser.get(section, 'oauth_token_secret'),
-    )
-
 def argv_error(msg):
     print >> sys.stderr, msg
     print >> sys.stderr
@@ -557,15 +515,6 @@ def parse_argv():
     host_config = parse_config_file(ini_path)
     return host_config
 
-def create_client_from_config(conf):
-    client = imapclient.IMAPClient(conf.host, port=conf.port, ssl=conf.ssl)
-    if conf.oauth:
-        client.oauth_login(conf.oauth_url,
-                           conf.oauth_token,
-                           conf.oauth_token_secret)
-    else:
-        client.login(conf.username, conf.password)
-    return client
     
 
 def probe_host(config):