--- /dev/null
+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
+
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()
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
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'
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
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):