From 51ac1c7e1badba79046cf7746704d6a34a52599a Mon Sep 17 00:00:00 2001 From: erickson Date: Sat, 20 Nov 2010 15:24:55 +0000 Subject: [PATCH] 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 git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@2094 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/srfsh.py | 104 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 29 deletions(-) diff --git a/src/python/srfsh.py b/src/python/srfsh.py index 3c4f1bf..a8aec1f 100755 --- a/src/python/srfsh.py +++ b/src/python/srfsh.py @@ -12,17 +12,30 @@ srfsh.py - provides a basic shell for issuing OpenSRF requests request [, , ...] - performs an opensrf request + router + - Queries the router. Query options: services service-stats service-nodes + set VAR= - 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 = - request responses to be returned in 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 @@ def do_loop(): while True: try: + report("", True) line = raw_input("srfsh# ") if not len(line): @@ -76,7 +90,7 @@ def do_loop(): 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 @@ def do_loop(): 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 " + report("usage: router \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 @@ def handle_set(parts): 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_help(foo): def handle_request(parts): if len(parts) < 2: - print "usage: request [, , ...]" + report("usage: request [, , ...]\n") return service = parts.pop(0) @@ -147,7 +162,7 @@ def handle_request(parts): 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 @@ def handle_request(parts): 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 @@ def handle_math_bench(parts): sys.stdout.write( str( cnt / 10 ) ) print "" - for cnt in range(count): starttime = time.time() @@ -305,9 +323,37 @@ def load_plugins(): 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'): -- 2.11.0