Changeset 224:c4af68270085
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r204
|
r224
|
|
| 7 | 7 | |
| 8 | 8 | import code |
| | 9 | from getpass import getpass |
| 9 | 10 | from optparse import OptionParser |
| 10 | | import imapclient |
| 11 | | from getpass import getpass |
| 12 | 11 | |
| | 12 | from imapclient.config import parse_config_file, create_client_from_config |
| 13 | 13 | |
| 14 | 14 | def command_line(): |
| … |
… |
|
| 21 | 21 | help='Password to login with') |
| 22 | 22 | 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)') |
| 24 | 24 | p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False, |
| 25 | 25 | help='Use SSL connection') |
| | 26 | p.add_option('-f', '--file', dest='file', action='store', default=None, |
| | 27 | help='Config file (same as livetest.py)') |
| 26 | 28 | |
| 27 | | options, args = p.parse_args() |
| | 29 | opts, args = p.parse_args() |
| 28 | 30 | if args: |
| 29 | 31 | p.error('unexpected arguments %s' % ' '.join(args)) |
| 30 | 32 | |
| 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 |
| 44 | 47 | |
| 45 | 48 | |
| 46 | 49 | if __name__ == '__main__': |
| 47 | | client = setup_client() |
| | 50 | opts = command_line() |
| | 51 | client = create_client_from_config(opts) |
| 48 | 52 | banner = '\nIMAPClient instance is "c"' |
| 49 | 53 | try: |
-
|
r223
|
r224
|
|
| 11 | 11 | import time |
| 12 | 12 | from datetime import datetime |
| 13 | | from ConfigParser import SafeConfigParser, NoOptionError |
| 14 | 13 | |
| 15 | 14 | import imapclient |
| 16 | 15 | from imapclient.test.util import unittest |
| 17 | | |
| 18 | | # TODO cleaner verbose output: avoid "__main__" and separater between classes |
| | 16 | from imapclient.config import parse_config_file, create_client_from_config |
| | 17 | |
| | 18 | # TODO cleaner verbose output: avoid "__main__" and separator between classes |
| 19 | 19 | |
| 20 | 20 | |
| … |
… |
|
| 500 | 500 | return isinstance(b, type(a)) |
| 501 | 501 | |
| 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 | | |
| 544 | 502 | def argv_error(msg): |
| 545 | 503 | print >> sys.stderr, msg |
| … |
… |
|
| 558 | 516 | return host_config |
| 559 | 517 | |
| 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 |
| 569 | 518 | |
| 570 | 519 | |