From: Menno Smits Date: Tue, 8 Sep 2015 02:22:48 +0000 (+1200) Subject: Extracted config defaults so that interact.py doesn't have to keep up with new config... X-Git-Tag: 1.0.0~1^2~22 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=7a5529978a04d9135b06a045bfd590bb57d58d1c;p=imapclient.git Extracted config defaults so that interact.py doesn't have to keep up with new config options. --- diff --git a/imapclient/config.py b/imapclient/config.py index af95a04..cc80213 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -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) diff --git a/imapclient/interact.py b/imapclient/interact.py index a567a27..1b8bf0c 100644 --- a/imapclient/interact.py +++ b/imapclient/interact.py @@ -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