From: Menno Smits Date: Tue, 17 May 2011 20:39:43 +0000 (+0100) Subject: Added support for interact.py to read livetest.py config files (#66) X-Git-Tag: 0.8~34^2~10 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=5994d4228477bd5dbec64112e83a5bb9e4715fad;p=imapclient.git Added support for interact.py to read livetest.py config files (#66) --- diff --git a/imapclient/config.py b/imapclient/config.py new file mode 100644 index 0000000..f710b9c --- /dev/null +++ b/imapclient/config.py @@ -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 + diff --git a/interact.py b/interact.py index 7ebd865..faa151b 100755 --- a/interact.py +++ b/interact.py @@ -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 diff --git a/livetest.py b/livetest.py index b001280..539c096 100755 --- a/livetest.py +++ b/livetest.py @@ -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):