Changeset 224:c4af68270085

Show
Ignore:
Timestamp:
17/05/11 21:39:43 (12 months ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

Added support for interact.py to read livetest.py config files (#66)

Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • interact.py

    r204 r224  
    77 
    88import code 
     9from getpass import getpass 
    910from optparse import OptionParser 
    10 import imapclient 
    11 from getpass import getpass 
    1211 
     12from imapclient.config import parse_config_file, create_client_from_config 
    1313 
    1414def command_line(): 
     
    2121                 help='Password to login with') 
    2222    p.add_option('-P', '--port', dest='port', action='store', 
    23                  default=143, help='IMAP port to use (default is 143)') 
     23                 default=None, help='IMAP port to use (default is 143)') 
    2424    p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False, 
    2525                 help='Use SSL connection') 
     26    p.add_option('-f', '--file', dest='file', action='store', default=None, 
     27                 help='Config file (same as livetest.py)') 
    2628 
    27     options, args = p.parse_args() 
     29    opts, args = p.parse_args() 
    2830    if args: 
    2931        p.error('unexpected arguments %s' % ' '.join(args)) 
    3032 
    31     # Get compulsory options if not given on the command line 
    32     for opt_name in ('host', 'username', 'password'): 
    33         if not getattr(options, opt_name): 
    34             setattr(options, opt_name, getpass(opt_name + ': ')) 
    35  
    36     return options 
    37  
    38  
    39 def setup_client(): 
    40     options = command_line() 
    41     client = imapclient.IMAPClient(options.host, ssl=options.ssl) 
    42     client.login(options.username, options.password) 
    43     return client 
     33    if opts.file: 
     34        if opts.host or opts.username or opts.password or opts.port or opts.ssl: 
     35            p.error('If -f/--file is given no other options can be used') 
     36        # Use the options in the config file 
     37        opts = parse_config_file(opts.file) 
     38    else: 
     39        # Get compulsory options if not given on the command line 
     40        for opt_name in ('host', 'username', 'password'): 
     41            if not getattr(opts, opt_name): 
     42                setattr(opts, opt_name, getpass(opt_name + ': ')) 
     43        if not opts.port: 
     44            opts.port = 143 
     45        opts.oauth = False     # OAUTH not supported on command line 
     46    return opts 
    4447 
    4548 
    4649if __name__ == '__main__': 
    47     client = setup_client() 
     50    opts = command_line() 
     51    client = create_client_from_config(opts) 
    4852    banner = '\nIMAPClient instance is "c"' 
    4953    try: 
  • livetest.py

    r223 r224  
    1111import time 
    1212from datetime import datetime 
    13 from ConfigParser import SafeConfigParser, NoOptionError 
    1413 
    1514import imapclient 
    1615from imapclient.test.util import unittest 
    17  
    18 # TODO cleaner verbose output: avoid "__main__" and separater between classes 
     16from imapclient.config import parse_config_file, create_client_from_config 
     17 
     18# TODO cleaner verbose output: avoid "__main__" and separator between classes 
    1919 
    2020 
     
    500500    return isinstance(b, type(a)) 
    501501 
    502 class Bunch(dict): 
    503  
    504     def __getattr__(self, k): 
    505         try: 
    506             return self[k] 
    507         except KeyError: 
    508             raise AttributeError 
    509  
    510     def __setattr__(self, k, v): 
    511         self[k] = v 
    512  
    513 def parse_config_file(path): 
    514     parser = SafeConfigParser(dict(ssl='false', 
    515                                    username=None, 
    516                                    password=None, 
    517                                    oauth='false', 
    518                                    oauth_url=None, 
    519                                    oauth_token=None, 
    520                                    oauth_token_secret=None)) 
    521     fh = file(path) 
    522     parser.readfp(fh) 
    523     fh.close() 
    524     section = 'main' 
    525     assert parser.sections() == [section], 'Only expected a [main] section' 
    526  
    527     try: 
    528         port = parser.getint(section, 'port') 
    529     except NoOptionError: 
    530         port = None 
    531          
    532     return Bunch( 
    533         host=parser.get(section, 'host'), 
    534         port=port, 
    535         ssl=parser.getboolean(section, 'ssl'), 
    536         username=parser.get(section, 'username'), 
    537         password=parser.get(section, 'password'), 
    538         oauth=parser.getboolean(section, 'oauth'), 
    539         oauth_url=parser.get(section, 'oauth_url'), 
    540         oauth_token=parser.get(section, 'oauth_token'), 
    541         oauth_token_secret=parser.get(section, 'oauth_token_secret'), 
    542     ) 
    543  
    544502def argv_error(msg): 
    545503    print >> sys.stderr, msg 
     
    558516    return host_config 
    559517 
    560 def create_client_from_config(conf): 
    561     client = imapclient.IMAPClient(conf.host, port=conf.port, ssl=conf.ssl) 
    562     if conf.oauth: 
    563         client.oauth_login(conf.oauth_url, 
    564                            conf.oauth_token, 
    565                            conf.oauth_token_secret) 
    566     else: 
    567         client.login(conf.username, conf.password) 
    568     return client 
    569518     
    570519