[Zope-CVS] CVS: Products/Scheduler - clock.py:1.2

Tres Seaver tseaver@zope.com
Mon, 12 Aug 2002 14:15:58 -0400


Update of /cvs-repository/Products/Scheduler
In directory cvs.zope.org:/tmp/cvs-serv3437

Modified Files:
	clock.py 
Log Message:



  - Reorganize around getopt command-line parsing, to permit
    parameterization of previously-hard-wired values.


=== Products/Scheduler/clock.py 1.1.1.1 => 1.2 ===
--- Products/Scheduler/clock.py:1.1.1.1	Mon Jun 10 13:03:03 2002
+++ Products/Scheduler/clock.py	Mon Aug 12 14:15:57 2002
@@ -19,26 +19,143 @@
 """
 __version__ = "$Revision$"[11:-2]
 
-import time, xmlrpclib, BasicAuthTransport
+import sys
+import time
+from xmlrpclib import Server
+from BasicAuthTransport import BasicAuthTransport
+import getopt
+import traceback
+
+class Clock:
+
+    def __init__( self
+                , scheduler_url
+                , period
+                , userid
+                , password
+                , logfile=sys.stdout
+                , errfile=sys.stderr
+                , verbosity=0
+                ):
+
+        self._scheduler_url = scheduler_url
+        self._period = period
+        self._userid = userid
+        self._password = password
+        self._logfile = logfile
+        self._errfile = errfile
+        self._verbosity = verbosity
+
+    def run(self):
+        """ Loop forever, popping the scheduler and then sleeping.
+        """
+        scheduler = self._makeScheduler()
+        while 1:
+            try:
+                scheduler.notify()
+            except:
+                traceback.print_exc(file=self._errfile)
+                self._errfile.write( '\n' )
+                self._logfile.write('Clock failure at %s\n'
+                                   % time.ctime(time.time()))
+            else:
+                if self._verbosity:
+                    self._logfile.write('Clock succeded at %s\n'
+                                       % time.ctime(time.time()))
+            time.sleep(self._period)
+
+    def _makeScheduler(self):
+        """ Create the XML-RPC connection to the scheduler.
+        """
+        #   XXX:  Hardwired credentials
+        transport = BasicAuthTransport(self._userid, self._password)
+        return Server(self._scheduler_url, transport=transport)
+
+
+def usage():
+    USAGE = """clock [options]
+
+Options:
+
+  -?, -h, --help        Print this usage message.
+
+  -q, --quiet           Don't blather about success (default).
+
+  -v, --verbose         Blather about success.
+
+  -n, --nethost         Supply the nethost part of the scheduler URL
+                        (defaults to 'http://localhost:8080').
+
+  -s, --scheduler_path  Supply the path part of the scheduler URL
+                        (defaults to '/workbench/portal_scheduler').
+
+  -a, --auth            Supply the authentication credentials as a
+                        'userid:password' token.
+
+  -p, --period          Supply the period interval, in seconds, at which
+                        the clock should notify the schedulre (default 20).
+"""
+    print USAGE
+    sys.exit(2)
 
 def main():
-    #LOGFILE=open('clock.log', 'a')
-    import sys
-    LOGFILE=sys.stdout
-    SCHEDULER = xmlrpclib.Server(
-        '%s%s' % ('http://localhost:8080', '/scheduler'),
-        transport=BasicAuthTransport.BasicAuthTransport('admin', '123')
-        )
-    PERIOD=20 # seconds
-    while 1:
-        try:
-            SCHEDULER.notify()
-        except:
-            import traceback; traceback.print_exc()
-            LOGFILE.write('Clock failure at %s\n' % time.ctime(time.time()))
-            print "\n"
 
-        time.sleep(PERIOD)
+    nethost = 'http://localhost:8080'
+    scheduler_path = '/workbench/portal_scheduler'
+    userid = 'admin'
+    password = '123'
+    period = 20
+    verbosity = 0
+
+    try:
+        opts, args = getopt.getopt( sys.argv[1:]
+                                , '?hqvn:s:a:p:'
+                                , [ 'help'
+                                  , 'quiet'
+                                  , 'verbose'
+                                  , 'nethost='
+                                  , 'scheduler_path='
+                                  , 'auth='
+                                  , 'period='
+                                  ]
+                                )
+    except getopt.GetoptError:
+        usage()
+
+    if args:
+        usage()
+
+    for k, v in opts:
+
+        if k == '-h' or k == '-?' or k == '--help':
+            usage()
+
+        if k == '-q' or k == '--quiet':
+            verbosity = 0
+
+        if k == '-v' or k == '--verbose':
+            verbosity += 1
+
+        if k == '-n' or k == '--nethost':
+            nethost = v
+
+        if k == '-s' or k == '--scheduler_path':
+            scheduler_path = v
+
+        if k == '-a' or k == '--auth':
+            userid, password = v.split( ':' )
+
+        if k == '-p' or k == '--period':
+            period = int( v )
+
+    Clock( scheduler_url='%s/%s' % (nethost, scheduler_path)
+         , period=period
+         , userid=userid
+         , password=password
+         , logfile=sys.stdout
+         , errfile=sys.stderr
+         , verbosity=verbosity
+         ).run()
+
 if __name__ == '__main__':
     main()
-