[Opensrf-commits] r2097 - branches/rel_2_0/src/python (erickson)

svn at svn.open-ils.org svn at svn.open-ils.org
Sat Nov 20 10:29:05 EST 2010


Author: erickson
Date: 2010-11-20 10:29:02 -0500 (Sat, 20 Nov 2010)
New Revision: 2097

Modified:
   branches/rel_2_0/src/python/srfsh.py
Log:
added support for paging through 'less' (like C); added 'router' query docs;  added separate env vars for pretty printing json and for print network object keys;   explicit opensrf disconnect on exit

Modified: branches/rel_2_0/src/python/srfsh.py
===================================================================
--- branches/rel_2_0/src/python/srfsh.py	2010-11-20 15:28:57 UTC (rev 2096)
+++ branches/rel_2_0/src/python/srfsh.py	2010-11-20 15:29:02 UTC (rev 2097)
@@ -12,17 +12,30 @@
   request <service> <method> [<param1>, <param2>, ...]
     - performs an opensrf request
 
+  router <query>
+    - Queries the router.  Query options: services service-stats service-nodes
+
   set VAR=<value>
     - sets an environment variable
 
+  get VAR
+    - Returns the value for the environment variable
+
   Environment variables:
-    SRFSH_OUTPUT = pretty - print pretty JSON and key/value pairs for network objects
-                 = raw - print formatted JSON 
+    SRFSH_OUTPUT_NET_OBJ_KEYS  = true - If a network object is array-encoded and key registry exists for the object type, annotate the object with field names
+                               = false - Print JSON
 
+    SRFSH_OUTPUT_FORMAT_JSON = true - Use JSON pretty printer
+                             = false - Print raw JSON
+
+    SRFSH_OUTPUT_PAGED = true - Paged output.  Uses "less -EX"
+                       = false - Output is not paged
+
     SRFSH_LOCALE = <locale> - request responses to be returned in locale <locale> if available
+
 """
 
-import os, sys, time, readline, atexit, re
+import os, sys, time, readline, atexit, re, pydoc
 import osrf.json, osrf.system, osrf.ses, osrf.conf, osrf.log, osrf.net
 
 router_command_map = {
@@ -64,6 +77,7 @@
     while True:
 
         try:
+            report("", True)
             line = raw_input("srfsh# ")
 
             if not len(line): 
@@ -76,7 +90,7 @@
             command = parts.pop(0)
 
             if command not in command_map:
-                print "unknown command: '%s'" % command
+                report("unknown command: '%s'\n" % command)
                 continue
 
             command_map[command](parts)
@@ -85,22 +99,23 @@
             sys.exit(0)
 
         except KeyboardInterrupt: # ^-c
-            print ""
+            report("\n")
 
         except Exception, e:
-            print e
+            report("%s\n" % e)
 
+    cleanup()
 
 def handle_router(parts):
 
     if len(parts) == 0:
-        print "usage: router <query>"
+        report("usage: router <query>\n")
         return
 
     query = parts[0]
 
     if query not in router_command_map:
-        print "router query options: %s" % ','.join(router_command_map.keys())
+        report("router query options: %s\n" % ','.join(router_command_map.keys()))
         return
 
     return handle_request(['router', router_command_map[query]])
@@ -114,20 +129,20 @@
     key = pattern.group(1)
     val = pattern.group(2)
     set_var(key, val)
-    print "%s = %s" % (key, val)
+    report("%s = %s\n" % (key, val))
 
 def handle_get(parts):
     try:
-        print get_var(parts[0])
+        report("%s=%s\n" % (parts[0], get_var(parts[0])))
     except:
-        print ""
+        report("\n")
 
 
 # -------------------------------------------------------------------
 # Prints help info
 # -------------------------------------------------------------------
 def handle_help(foo):
-    print __doc__
+    report(__doc__)
 
 # -------------------------------------------------------------------
 # performs an opensrf request
@@ -135,7 +150,7 @@
 def handle_request(parts):
 
     if len(parts) < 2:
-        print "usage: request <service> <api_name> [<param1>, <param2>, ...]"
+        report("usage: request <service> <api_name> [<param1>, <param2>, ...]\n")
         return
 
     service = parts.pop(0)
@@ -147,7 +162,7 @@
     try:
         params = osrf.json.to_object(jstr)
     except:
-        print "Error parsing JSON: %s" % jstr
+        report("Error parsing JSON: %s\n" % jstr)
         return
 
     ses = osrf.ses.ClientSession(service, locale=locale)
@@ -159,30 +174,34 @@
 
     while True:
         resp = None
+
         try:
             resp = req.recv(timeout=120)
         except osrf.net.XMPPNoRecipient:
-            print "Unable to communicate with %s" % service
+            report("Unable to communicate with %s\n" % service)
             total = 0
             break
 
-        osrf.log.log_internal("Looping through receive request")
-        if not resp:
-            break
+        if not resp: break
+
         total = time.time() - start
+        content = resp.content()
 
-        otp = get_var('SRFSH_OUTPUT')
-        if otp == 'pretty':
-            print "Received Data: %s\n" % osrf.json.debug_net_object(resp.content())
-        else:
-            print "Received Data: %s\n" % osrf.json.pprint(osrf.json.to_json(resp.content()))
+        if content is not None:
+            if get_var('SRFSH_OUTPUT_NET_OBJ_KEYS') == 'true':
+                report("Received Data: %s\n" % osrf.json.debug_net_object(content))
+            else:
+                if get_var('SRFSH_OUTPUT_FORMAT_JSON') == 'true':
+                    report("Received Data: %s\n" % osrf.json.pprint(osrf.json.to_json(content)))
+                else:
+                    report("Received Data: %s\n" % osrf.json.to_json(content))
 
     req.cleanup()
     ses.cleanup()
 
-    print '-'*60
-    print "Total request time: %f" % total
-    print '-'*60
+    report('-'*60 + "\n")
+    report("Total request time: %f\n" % total)
+    report('-'*60 + "\n")
 
 
 def handle_math_bench(parts):
@@ -198,7 +217,6 @@
             sys.stdout.write( str( cnt / 10 ) )
     print ""
 
-
     for cnt in range(count):
     
         starttime = time.time()
@@ -305,10 +323,38 @@
         except Exception, e:
             sys.stderr.write("\nError importing plugin %s, with init symbol %s: \n%s\n" % (name, init, e))
 
+def cleanup():
+    osrf.system.System.net_disconnect()
+    
+_output_buffer = '' # collect output for pager
+def report(text, flush=False):
+    global _output_buffer
+
+    if get_var('SRFSH_OUTPUT_PAGED') == 'true':
+        _output_buffer += text
+
+        if flush and _output_buffer != '':
+            pipe = os.popen('less -EX', 'w') 
+            pipe.write(_output_buffer)
+            pipe.close()
+            _output_buffer = ''
+
+    else:
+        sys.stdout.write(text)
+        if flush:
+            sys.stdout.flush()
+
 def set_vars():
-    if not get_var('SRFSH_OUTPUT'):
-        set_var('SRFSH_OUTPUT', 'raw')
 
+    if not get_var('SRFSH_OUTPUT_NET_OBJ_KEYS'):
+        set_var('SRFSH_OUTPUT_NET_OBJ_KEYS', 'false')
+
+    if not get_var('SRFSH_OUTPUT_FORMAT_JSON'):
+        set_var('SRFSH_OUTPUT_FORMAT_JSON', 'true')
+
+    if not get_var('SRFSH_OUTPUT_PAGED'):
+        set_var('SRFSH_OUTPUT_PAGED', 'true')
+
     # XXX Do we need to differ between LANG and LC_MESSAGES?
     if not get_var('SRFSH_LOCALE'):
         set_var('SRFSH_LOCALE', get_var('LC_ALL'))



More information about the opensrf-commits mailing list