Share cmdline argument parsing code between bbapplet and gbuildbotclient.
authorTim Potter <tpot@hp.com>
Thu, 10 Apr 2008 05:02:49 +0000 (15:02 +1000)
committerTim Potter <tpot@hp.com>
Thu, 10 Apr 2008 05:02:49 +0000 (15:02 +1000)
bbapplet
bbclient.py
gbuildbotclient

index 313cb4baaee235e48a2a93133f79a892a1dc9415..885625b3ab1d6ac6bc2777da1423c990898c659b 100755 (executable)
--- a/bbapplet
+++ b/bbapplet
@@ -10,12 +10,7 @@ from twisted.internet import reactor
 from twisted.spread import pb
 
 import sys, gtk, gnomeapplet, pynotify
-from bbclient import BuildbotClient
-
-HOST = 'localhost'
-PORT = 1234
-USER = 'statusClient'
-PASS = 'clientpw'
+from bbclient import BuildbotClient, CommandLineOptions
 
 class BuildbotApplet(pb.Referenceable):
       
@@ -26,11 +21,12 @@ class BuildbotApplet(pb.Referenceable):
         self.applet.add(icon)
         self.applet.show_all()
 
-        client = BuildbotClient()
+        self.client = BuildbotClient()
 
-        d = client.connect(HOST, PORT, USER, PASS)
+    def connect(self, host, port, user, password):
 
-        d.addCallback(lambda arg: client.subscribe('builds', 5, self))
+        d = self.client.connect(host, port, user, password)
+        d.addCallback(lambda arg: self.client.subscribe('builds', 5, self))
 
     # Callbacks for subscription mode >= MODE_BUILDERS
 
@@ -63,18 +59,23 @@ class BuildbotApplet(pb.Referenceable):
         n.attach_to_widget(self.applet)
         n.show()
 
-def BuildbotApplet_factory(applet, iid):
+def BuildbotApplet_factory(applet, iid, host, port, user, password):
 
     pynotify.init("bbapplet")
 
     icon = gtk.image_new_from_stock(gtk.STOCK_NEW, gtk.ICON_SIZE_MENU)
-    BuildbotApplet(applet, icon)
+    app = BuildbotApplet(applet, icon)
+    app.connect(host, port, user, password)
 
     return gtk.TRUE
 
 if __name__ == "__main__":
     
-    if len(sys.argv) == 2 and sys.argv[1] == "run-in-window":
+    if sys.argv[1] == "run-in-window":
+
+        del(sys.argv[1])
+
+        host, port, user, password = CommandLineOptions()
 
         # Run in a window, for debugging
 
@@ -83,8 +84,8 @@ if __name__ == "__main__":
         main_window.connect('destroy', gtk.main_quit)
 
         app = gnomeapplet.Applet()
-
-        BuildbotApplet_factory(app, None)
+        
+        BuildbotApplet_factory(app, None, host, port, user, password)
 
         app.reparent(main_window)
 
index 25e75b0e6675c83ae709ef21e374cd3f0ae09a07..7aeae67353095a2ad04837595d8f1a7d6a91c5c3 100644 (file)
@@ -1,3 +1,6 @@
+import sys, string
+from optparse import OptionParser
+
 from twisted.internet import reactor
 from twisted.cred import credentials
 from twisted.spread import pb
@@ -42,3 +45,37 @@ class BuildbotClient:
 
     def subscribe(self, mode, interval, target):
         return self.remote.callRemote('subscribe', mode, interval, target)
+
+def CommandLineOptions():
+    """Return hostname, port, user and password command line options."""
+
+    optparser = OptionParser(
+        usage = '%prog HOSTNAME:PORT -u USER -p PASS')
+
+    optparser.add_option('-u', '--user', dest = 'user',
+                         action = 'store', type = 'string',
+                         help = 'user to connect as')
+
+    optparser.add_option('-p', '--password', dest = 'password',
+                         action = 'store', type = 'string',
+                         help = 'password to connect user as')
+
+    (opts, argv) = optparser.parse_args()
+
+    if len(argv) != 1:
+        optparser.print_usage()
+        sys.exit(1)
+
+    if opts.user is None:
+        print 'Username required'
+        optparser.print_usage()
+        sys.exit(1)
+
+    if opts.password is None:
+        print 'Password required'
+        optparser.print_usage()
+        sys.exit(1)
+
+    host, port = string.split(argv[0], ':')
+
+    return (host, int(port), opts.user, opts.password)
index e65084cce37245fcd7c0edd9a4f90ffcef7cbd50..50575ca130f0a82191d19b4cb809d0c83809cc79 100755 (executable)
@@ -19,7 +19,7 @@ from twisted.internet import reactor
 from twisted.spread import pb
 from twisted.cred import credentials
 from twisted.python import log
-from bbclient import BuildbotClient
+from bbclient import BuildbotClient, CommandLineOptions
 
 # Application class
 
@@ -319,40 +319,11 @@ if __name__ == '__main__':
 
     # Command line parsing
 
-    from optparse import OptionParser
-
-    optparser = OptionParser(
-        usage = '%prog HOSTNAME:PORT -u USER -p PASS')
-
-    optparser.add_option('-u', '--user', dest = 'user',
-                         action = 'store', type = 'string',
-                         help = 'user to connect as')
-
-    optparser.add_option('-p', '--password', dest = 'password',
-                         action = 'store', type = 'string',
-                         help = 'password to connect user as')
-
-    (opts, argv) = optparser.parse_args()
-
-    if len(argv) != 1:
-        optparser.print_usage()
-        sys.exit(1)
-
-    if opts.user is None:
-        print 'Username required'
-        optparser.print_usage()
-        sys.exit(1)
-
-    if opts.password is None:
-        print 'Password required'
-        optparser.print_usage()
-        sys.exit(1)
-
-    host, port = string.split(argv[0], ':')
+    host, port, user, password = CommandLineOptions()
 
     # Start application
 
     log.startLogging(sys.stdout)
-    app = App(host, int(port), opts.user, opts.password)
+    app = App(host, port, user, password)
 
     reactor.run()