3 import os, sys, time, readline, atexit, re
11 # -------------------------------------------------------------------
13 # -------------------------------------------------------------------
18 #line = raw_input("srfsh% ")
19 line = raw_input("\033[01;32msrfsh\033[01;34m% \033[00m")
22 if str.lower(line) == 'exit' or str.lower(line) == 'quit':
24 parts = str.split(line)
28 if command == 'request':
33 if command == 'math_bench':
35 handle_math_bench(parts)
52 except KeyboardInterrupt:
60 # -------------------------------------------------------------------
61 # Set env variables to control behavior
62 # -------------------------------------------------------------------
63 def handle_set(parts):
64 pattern = re.compile('(.*)=(.*)').match(parts[0])
65 key = pattern.group(1)
66 val = pattern.group(2)
68 print "%s = %s" % (key, val)
70 def handle_get(parts):
72 print get_var(parts[0])
77 # -------------------------------------------------------------------
79 # -------------------------------------------------------------------
86 - runs <count> opensrf.math requests and reports the average time
88 request <service> <method> [<param1>, <param2>, ...]
89 - performs an opensrf request
92 - sets an environment variable
94 Environment variables:
95 SRFSH_OUTPUT = pretty - print pretty JSON and key/value pairs for network objects
96 = raw - print formatted JSON
102 # -------------------------------------------------------------------
103 # performs an opensrf request
104 # -------------------------------------------------------------------
105 def handle_request(parts):
106 service = parts.pop(0)
107 method = parts.pop(0)
108 jstr = '[%s]' % "".join(parts)
112 params = osrf.json.to_object(jstr)
114 print "Error parsing JSON: %s" % jstr
117 ses = osrf.ses.ClientSession(service)
121 req = ses.request2(method, tuple(params))
125 resp = req.recv(timeout=120)
126 osrf.log.log_internal("Looping through receive request")
129 total = time.time() - start
131 otp = get_var('SRFSH_OUTPUT')
133 print "\n" + osrf.json.debug_net_object(resp.content())
135 print osrf.json.pprint(osrf.json.to_json(resp.content()))
141 print "Total request time: %f" % total
145 def handle_math_bench(parts):
147 count = int(parts.pop(0))
148 ses = osrf.ses.ClientSession('opensrf.math')
151 for cnt in range(100):
153 sys.stdout.write('.')
155 sys.stdout.write( str( cnt / 10 ) )
159 for cnt in range(count):
161 starttime = time.time()
162 req = ses.request('add', 1, 2)
163 resp = req.recv(timeout=2)
164 endtime = time.time()
166 if resp.content() == 3:
167 sys.stdout.write("+")
169 times.append( endtime - starttime )
171 print "What happened? %s" % str(resp.content())
174 if not ( (cnt + 1) % 100):
175 print ' [%d]' % (cnt + 1)
181 print "\naverage time %f" % (total / len(times))
186 # -------------------------------------------------------------------
187 # Defines the tab-completion handling and sets up the readline history
188 # -------------------------------------------------------------------
189 def setup_readline():
190 class SrfshCompleter(object):
191 def __init__(self, words):
195 def complete(self, prefix, index):
196 if prefix != self.prefix:
197 # find all words that start with this prefix
198 self.matching_words = [
199 w for w in self.words if w.startswith(prefix)
203 return self.matching_words[index]
207 words = 'request', 'help', 'exit', 'quit', 'opensrf.settings', 'opensrf.math', 'set'
208 completer = SrfshCompleter(words)
209 readline.parse_and_bind("tab: complete")
210 readline.set_completer(completer.complete)
212 histfile = os.path.join(get_var('HOME'), ".srfsh_history")
214 readline.read_history_file(histfile)
217 atexit.register(readline.write_history_file, histfile)
220 file = os.path.join(get_var('HOME'), ".srfsh.xml")
221 print_green("Connecting to opensrf...")
222 osrf.system.connect(file, 'srfsh')
226 # Load the user defined external plugins
227 # XXX Make this a real module interface, with tab-complete words, commands, etc.
229 plugins = osrf.conf.get('plugins')
232 # XXX standard srfsh.xml does not yet define <plugins> element
233 print_red("No plugins defined in /srfsh/plugins/plugin\n")
236 plugins = osrf.conf.get('plugins.plugin')
237 if not isinstance(plugins, list):
240 for module in plugins:
241 name = module['module']
242 init = module['init']
243 print_green("Loading module %s..." % name)
246 string = 'from %s import %s\n%s()' % (name, init, init)
251 sys.stderr.write("\nError importing plugin %s, with init symbol %s: \n%s\n" % (name, init, e))
254 if not get_var('SRFSH_OUTPUT'):
255 set_var('SRFSH_OUTPUT', 'pretty')
258 def set_var(key, val):
259 os.environ[key] = val
264 return os.environ[key]
269 def print_green(string):
270 sys.stdout.write("\033[01;32m")
271 sys.stdout.write(string)
272 sys.stdout.write("\033[00m")
275 def print_red(string):
276 sys.stdout.write("\033[01;31m")
277 sys.stdout.write(string)
278 sys.stdout.write("\033[00m")