]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Extracted config defaults so that interact.py doesn't have to keep up with new config...
authorMenno Smits <menno@freshfoo.com>
Tue, 8 Sep 2015 02:22:48 +0000 (14:22 +1200)
committerMenno Smits <menno@freshfoo.com>
Tue, 8 Sep 2015 02:22:48 +0000 (14:22 +1200)
imapclient/config.py
imapclient/interact.py

index af95a04bc6a0fa0238e821ce41e5893fae5cb69e..cc8021301029cdb820d7e2018c332d949fe72c11 100644 (file)
@@ -13,6 +13,7 @@ except ImportError:
 from os import path
 from backports import ssl
 
+from six import iteritems
 from six.moves.urllib.request import urlopen
 from six.moves.urllib.parse import urlencode
 
@@ -24,32 +25,36 @@ try:
 except ImportError:
     json = None
 
-
-def parse_config_file(filename):
-    """Parse INI files containing IMAP connection details.
-
-    Used by livetest.py and interact.py
-    """
-    parser = SafeConfigParser(dict(
+def get_config_defaults():
+    return dict(
         username=None,
         password=None,
-        ssl='false',
-        ssl_check_hostname='true',
-        ssl_verify_cert='true',
+        ssl=False,
+        ssl_check_hostname=True,
+        ssl_verify_cert=True,
         ssl_ca_file=None,
         timeout=None,
-        starttls='false',
-        stream='false',
-        oauth='false',
+        starttls=False,
+        stream=False,
+        oauth=False,
         oauth_token=None,
         oauth_token_secret=None,
         oauth_url=None,
-        oauth2='false',
+        oauth2=False,
         oauth2_client_id=None,
         oauth2_client_secret=None,
         oauth2_refresh_token=None,
         expect_failure=None,
-    ))
+    )
+
+
+def parse_config_file(filename):
+    """Parse INI files containing IMAP connection details.
+
+    Used by livetest.py and interact.py
+    """
+
+    parser = SafeConfigParser(get_string_config_defaults())
     with open(filename, 'r') as fh:
         parser.readfp(fh)
 
@@ -64,6 +69,17 @@ def parse_config_file(filename):
     return conf
 
 
+def get_string_config_defaults():
+    out = {}
+    for k, v in iteritems(get_config_defaults()):
+        if v is True:
+            v = 'true'
+        elif v is False:
+            v = 'false'
+        out[k] = v
+    return out
+
+
 def _read_config_section(parser, section):
     get = lambda name: parser.get(section, name)
     getboolean = lambda name: parser.getboolean(section, name)
index a567a27608abe94720ce471d193a53ab7cd2a4d1..1b8bf0c34dabd397a39798d1ba239507587f2e49 100644 (file)
@@ -10,7 +10,9 @@ from __future__ import unicode_literals
 from getpass import getpass
 from optparse import OptionParser
 
-from .config import parse_config_file, create_client_from_config
+from six import iteritems
+
+from .config import parse_config_file, create_client_from_config, get_config_defaults
 
 
 def command_line():
@@ -39,18 +41,15 @@ def command_line():
         # 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 + ': '))
-        # Options not supported on the command line
-        opts.ssl_check_hostname = False
-        opts.ssl_verify_cert = False
-        opts.ssl_ca_file = None
-        opts.starttls = False
-        opts.oauth = False
-        opts.oauth2 = False
-        opts.stream = False
+        # Scan through options, filling in defaults and prompting when
+        # a compulsory option wasn't provided.
+        compulsory_opts = ('host', 'username', 'password')
+        for name, default_value in iteritems(get_config_defaults()):
+            value = getattr(opts, name, default_value)
+            if name in compulsory_opts and value is None:
+                value = getpass(name + ': ')
+            setattr(opts, name, value)
+
     return opts