3 import os, sys, time, readline, atexit, re
10 # -------------------------------------------------------------------
12 # -------------------------------------------------------------------
17 #line = raw_input("srfsh% ")
18 line = raw_input("\033[01;32msrfsh\033[01;34m% \033[00m")
21 if str.lower(line) == 'exit' or str.lower(line) == 'quit':
23 parts = str.split(line)
27 if command == 'request':
32 if command == 'math_bench':
34 handle_math_bench(parts)
51 except KeyboardInterrupt:
59 # -------------------------------------------------------------------
60 # Set env variables to control behavior
61 # -------------------------------------------------------------------
62 def handle_set(parts):
63 pattern = re.compile('(.*)=(.*)').match(parts[0])
64 key = pattern.group(1)
65 val = pattern.group(2)
67 print "%s = %s" % (key, val)
69 def handle_get(parts):
71 print get_var(parts[0])
76 # -------------------------------------------------------------------
78 # -------------------------------------------------------------------
85 - runs <count> opensrf.math requests and reports the average time
87 request <service> <method> [<param1>, <param2>, ...]
88 - performs an opensrf request
91 - sets an environment variable
93 Environment variables:
94 SRFSH_OUTPUT = pretty - print pretty JSON and key/value pairs for network objects
95 = raw - print formatted JSON
101 # -------------------------------------------------------------------
102 # performs an opensrf request
103 # -------------------------------------------------------------------
104 def handle_request(parts):
105 service = parts.pop(0)
106 method = parts.pop(0)
107 jstr = '[%s]' % "".join(parts)
111 params = osrf.json.to_object(jstr)
113 print "Error parsing JSON: %s" % jstr
116 ses = osrf.ses.ClientSession(service)
121 req = ses.request2(method, tuple(params))
125 resp = req.recv(timeout=120)
127 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")