]> khamidou — GitHub mirrors - imapclient.git/commitdiff
#7: added new script for interactive sessions (interact.py)
authorMenno Smits <menno@freshfoo.com>
Fri, 23 Apr 2010 12:27:44 +0000 (13:27 +0100)
committerMenno Smits <menno@freshfoo.com>
Fri, 23 Apr 2010 12:27:44 +0000 (13:27 +0100)
Uses IPython if available.
Removed --interact from livetest.py

NEWS
README
interact.py [new file with mode: 0755]
livetest.py

diff --git a/NEWS b/NEWS
index 06d2351656d7edda61c51ce1478a8cc99dfd5241..53fbdacc8052ab2444367dbf8304fbcff2a78041 100644 (file)
--- 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 269b18e142fb31667b399cccbddd01940f23b73e..2288a8e09816e15a2a720aff662825a275051638 100644 (file)
--- 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 (executable)
index 0000000..0a2b966
--- /dev/null
@@ -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))
index 1e34691eb4b213f2220e67d31500fae8b6851f85..3d2d87c5e2c9a728699f2e1cbb88e35618407401 100755 (executable)
@@ -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()