From: Menno Smits Date: Fri, 23 Apr 2010 12:27:44 +0000 (+0100) Subject: #7: added new script for interactive sessions (interact.py) X-Git-Tag: 0.7~46 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=5e6ff02e998205a8f7f6c7d5f1e15092ff95e6e2;p=imapclient.git #7: added new script for interactive sessions (interact.py) Uses IPython if available. Removed --interact from livetest.py --- diff --git a/NEWS b/NEWS index 06d2351..53fbdac 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ problems when interacting with Gmail and MS Exchange (#1). [API CHANGE] select_folder now returns a dictionary with the full SELECT command response instead of just the message count (#24). +Added interact.py, a script for interactive IMAPClient +sessions. Useful for debugging and exploration. Uses IPython if +installed. + Version 0.5.2 ------------- diff --git a/README b/README index 269b18e..2288a8e 100644 --- a/README +++ b/README @@ -62,6 +62,18 @@ See examples/example.py for a simple example of how to use IMAPClient. The code is well documented. See the docstrings for more usage information. + +interact.py +----------- +This script connects an IMAPClient instance using the command line +args given and starts an interactive session. This is useful for +exploring the IMAPClient API and testing things out, avoiding the +steps required to set up an IMAPClient instance. + +The IPython shell is used if it is installed. Otherwise the +code.interact() function from the standard library is used. + + livetest.py ----------- This is a standalone Python script which exercises IMAPClient against a live diff --git a/interact.py b/interact.py new file mode 100755 index 0000000..0a2b966 --- /dev/null +++ b/interact.py @@ -0,0 +1,54 @@ +#!/usr/bin/python + +# Copyright (c) 2009, Menno Smits +# Released subject to the New BSD License +# Please see http://en.wikipedia.org/wiki/BSD_licenses + + +import code +from optparse import OptionParser +import imapclient +from getpass import getpass + + +def command_line(): + p = OptionParser() + p.add_option('-H', '--host', dest='host', action='store', + help='IMAP host connect to') + p.add_option('-u', '--username', dest='username', action='store', + help='Username to login with') + 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)') + p.add_option('-s', '--ssl', dest='ssl', action='store_true', default=False, + help='Use SSL connection') + + options, 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 __name__ == '__main__': + client = setup_client() + banner = '\nIMAPClient instance is "c"' + try: + from IPython.Shell import IPShellEmbed + c = client + IPShellEmbed('', banner=banner)() + except ImportError: + code.interact(banner, local=dict(c=client)) diff --git a/livetest.py b/livetest.py index 1e34691..3d2d87c 100755 --- a/livetest.py +++ b/livetest.py @@ -405,9 +405,6 @@ def command_line(): p.add_option('', '--clobber', dest='clobber', action='store_true', default=False, help='These tests are destructive. Use this ' 'option to bypass the confirmation prompt.') - p.add_option('', '--interact', dest='interact', action='store_true', - default=False, - help='Instead of running tests, set up the connection & drop into a shell') options, args = p.parse_args() @@ -445,13 +442,8 @@ def main(): print '-'*60 client = imapclient.IMAPClient(options.host, use_uid=use_uid, ssl=options.ssl) client.login(options.username, options.password) - if options.interact: - import code - code.interact('HAI! IMAPClient instance is "c"', local=dict(c=client)) - break - else: - runtests(client) - print 'SUCCESS' + runtests(client) + print 'SUCCESS' if __name__ == '__main__': main()