| 1 | from ConfigParser import SafeConfigParser, NoOptionError |
|---|
| 2 | import imapclient |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | def parse_config_file(path): |
|---|
| 6 | """Parse INI files containing IMAP connection details. |
|---|
| 7 | |
|---|
| 8 | Used by livetest.py and interact.py |
|---|
| 9 | """ |
|---|
| 10 | parser = SafeConfigParser(dict(ssl='false', |
|---|
| 11 | username=None, |
|---|
| 12 | password=None, |
|---|
| 13 | oauth='false', |
|---|
| 14 | oauth_url=None, |
|---|
| 15 | oauth_token=None, |
|---|
| 16 | oauth_token_secret=None)) |
|---|
| 17 | fh = file(path) |
|---|
| 18 | parser.readfp(fh) |
|---|
| 19 | fh.close() |
|---|
| 20 | section = 'main' |
|---|
| 21 | assert parser.sections() == [section], 'Only expected a [main] section' |
|---|
| 22 | |
|---|
| 23 | try: |
|---|
| 24 | port = parser.getint(section, 'port') |
|---|
| 25 | except NoOptionError: |
|---|
| 26 | port = None |
|---|
| 27 | |
|---|
| 28 | return Bunch( |
|---|
| 29 | host=parser.get(section, 'host'), |
|---|
| 30 | port=port, |
|---|
| 31 | ssl=parser.getboolean(section, 'ssl'), |
|---|
| 32 | username=parser.get(section, 'username'), |
|---|
| 33 | password=parser.get(section, 'password'), |
|---|
| 34 | oauth=parser.getboolean(section, 'oauth'), |
|---|
| 35 | oauth_url=parser.get(section, 'oauth_url'), |
|---|
| 36 | oauth_token=parser.get(section, 'oauth_token'), |
|---|
| 37 | oauth_token_secret=parser.get(section, 'oauth_token_secret'), |
|---|
| 38 | ) |
|---|
| 39 | |
|---|
| 40 | def create_client_from_config(conf): |
|---|
| 41 | client = imapclient.IMAPClient(conf.host, port=conf.port, ssl=conf.ssl) |
|---|
| 42 | if conf.oauth: |
|---|
| 43 | client.oauth_login(conf.oauth_url, |
|---|
| 44 | conf.oauth_token, |
|---|
| 45 | conf.oauth_token_secret) |
|---|
| 46 | else: |
|---|
| 47 | client.login(conf.username, conf.password) |
|---|
| 48 | return client |
|---|
| 49 | |
|---|
| 50 | class Bunch(dict): |
|---|
| 51 | |
|---|
| 52 | def __getattr__(self, k): |
|---|
| 53 | try: |
|---|
| 54 | return self[k] |
|---|
| 55 | except KeyError: |
|---|
| 56 | raise AttributeError |
|---|
| 57 | |
|---|
| 58 | def __setattr__(self, k, v): |
|---|
| 59 | self[k] = v |
|---|