-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTapRunningInstallation.py
376 lines (305 loc) · 14 KB
/
TapRunningInstallation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/python
import socket
from select import select
#from xml.etree.ElementTree import ElementTree
import pdb, sys, time, thread
#import util.TimeOps as clock
#import util.Config as configGetter
#import util.ComponentRegistry as compReg
#import util.BehaviorQuerySystem as bqs
import json, re
class TapConnection(object):
""" This class handles connection to server """
def __init__(self,host,port):
self.host = host
self.port = port
def sendMsg(self,outData, maxretry = 5, interval = 1):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.bind((self.host, self.port+1))
# make sure buffer empty
while select([s],[],[], 0)[0]:
(inData,addy) = s.recvfrom(1024)
s.sendto(outData, (self.host, self.port))
time.sleep(.1)
#s.sendto(outData, (self.host, self.port))
string = ""
while string == "" and maxretry > 0:
maxretry -= 1
# on last attempt resend the message ? need to protect against json errors
#if maxretry == 0:
# interval *= 3
# s.sendto(outData, (self.host, self.port))
time.sleep(interval)
while select([s],[],[], 0)[0]:
(inData,addy) = s.recvfrom(2048)
string = string + inData
#if string == "":
# return ""
#return "["+string.split('][')[-1].strip('[]')+']'
return string
def close(self):
pass
class MenuTree(object):
""" TapRunning - maintains state for a running LightInstallation
commands:
p - print all renderable behaviors
b - print all behaviors
m - print all mappers
a - print all objects
(integer) - select number
q - go back / quit
t - toggle renderable flag
e - edit
c - create
d - delete
"""
ObjectTypes = {'p':'Renderables','a':'Objects','b':'Behaviors','m':'Mappers'}
commandDict = {'OperationArg':None}
componentList = None
def __init__(self, connection, msg =''):
self.connection = connection
self.nextAction = None
self.currentObject = None
self.lastcl = ""
self.lastindex = None
def executeSelection(self,command):
# self.commandDict['CurrentCommand'] = command
cl = command.strip().lower()
if cl.isdigit():
self.lastindex = int(cl)
if self.nextAction != None:
self.nextAction(int(cl))
else:
print "Got #{0} but don't know what to do with it. Try 'h'".format(cl)
return 0
cs = cl.split(' ')
cl = cs[0]
# should put processing for edit inputs here. if generalized callback exists, try using input
# all the following should be special case when input callback = None
if cl in ["q","x"]:
return -1
elif cl in self.ObjectTypes.keys():
self.lastindex = None
if self.lastcl != cl:
self.componentList = None
self.lastcl = cl
self.commandDict['OperationDetail'] = self.ObjectTypes[cl]
self.nextAction = lambda x: self.requestSpecificItem(x)
if len(cs) > 1 and cs[1].isdigit() and self.componentList != None:
self.requestSpecificItem(int(cs[1]))
else:
print "Getting {0}, please wait...".format(self.ObjectTypes[cl])
self.currentObject = None
self.commandDict['OperationArg'] = None
self.requestItems()
elif cl in ["e","t"]:
self.nextAction = lambda x: self.initiateEdit(x)
idx = None
if len(cs) > 1 and cs[1].isdigit():
# if digit known, call command to get details of object first, invisible mode
#if self.requestSpecificItem(int(cs[1]), True) < 0:
# return
idx=int(cs[1])
#print "do some prompting to figure out what needs to be changed"
self.initiateEdit(idx,(cl[0] == "t"))
elif cl in ["h","?"]:
print self.__doc__
self.nextAction = None
elif cl in ["c","d"]:
print "unimplemented"
self.nextAction = None
else:
if cl != "":
print "syntax error, use 'h' for help"
return 0
def showMenu(self):
pass
def acceptInput(self):
pass
def requestSpecificItem(self,index, is_quiet=False):
if self.componentList == None:
print "Specify whether you're talking about renderables (p) objects (a) or all behaviors (b)."
return -1
elif len(self.componentList)-1 < index:
print "No element with index {0}.".format(index)
self.commandDict['OperationArg'] = None
self.nextAction = None
return -1
else:
if not is_quiet:
print "Retrieving specific {0}...\n".format(self.commandDict['OperationDetail'][:-1])
self.commandDict['OperationArg'] = self.componentList[index][0]
self.requestItems(is_quiet)
self.lastindex = index
return 0
def requestItems(self, is_quiet=False):
self.commandDict['OperationType'] = 'Read'
# self.commandDict['OperationDetail'] = command
#if not(is_quiet):
# print self.commandDict
resp = self.connection.sendMsg(json.dumps(self.commandDict))
if resp == "":
print "No (timely) response. Is server running? Is SystemConfigMutator Behavior and tap input in the configuration?"
return 0
try:
resp = resp.replace('<lambda>','').replace('>',">'").replace('<',"'<")#bb=resp.find('>')
#if aa != -1 and bb != -1:
# resp = resp[:aa-1]+resp[bb+2:]
resp = json.loads(str(resp))
if type(resp) is str or type(resp) is unicode:
resp = eval(resp)
except:
print "couldn't parse response- not json? <",resp, ">"
return 0
if self.commandDict['OperationArg'] == None:
self.componentList = resp
self.printComponentList()
else:
self.currentObject = resp
self.printCurrentObject(is_quiet)
def printComponentList(self):
print "Available %s:\n"%self.commandDict['OperationDetail']
resp = self.componentList
for n in range(len(resp)):
if len(resp[n]) > 1:
print "{0:4} {1:15} {2:5}".format(n,resp[n][0], resp[n][1] and 'True' or 'False')
else:
print "{0:4} {1:10}".format(n,resp[n][0])
def printCurrentObject(self,is_quiet):
if not(is_quiet):
for k,v in self.currentObject.items():#re.split(''',(?=(?:[^\[\]]|\[[^\[]*\])*$)''', self.currentObject[1:-1]): # resp.split(','):
if k != 'parentScope':
print k,":",v
def initiateEdit(self, index=None, is_toggle = False):
#pdb.set_trace()
#print self.commandDict,"\n\n",index
if index == None and (self.currentObject == None or self.commandDict['OperationArg'] == None):
print "Need to specify which object."
return 0
#elif self.currentObject.find('RenderToScreen') == -1:
# print "Object not renderable."
# return 0
else:
if index != None:
if self.requestSpecificItem(index,False) < 0:
return
elif self.requestSpecificItem(self.lastindex,True) < 0:
return
#else:
# self.printCurrentObject(False)
#co=self.currentObject[1:-1]#.rstrip('}').lstrip('{')
# split on commas unless inside square brackets
#co = re.split(''',(?=(?:[^\[\]]|\[[^\[]*\])*$)''', co)
#co = re.split(''',(?=(?:[^{}]|\[[^{}]*})*$)''', co)
if is_toggle:
if not self.currentObject.has_key('RenderToScreen'):
print "Object not renderable."
return 0
currentRender = self.currentObject['RenderToScreen']
#pdb.set_trace()
print self.commandDict['OperationArg']+"'s RenderToScreen is "+str(currentRender)+" -- ",
if not currentRender:
value = False
confirm = raw_input("activate (y/N)? ")
else:
value = True
confirm = raw_input("deactivate (y/N)? ")
if confirm != "" and confirm[0] == 'y':
value = not value
self.commandDict['OperationType'] = 'Update'
self.commandDict['ComponentId'] = self.currentObject['Id'] #filter(lambda x: x.find('Id')!=-1,co)[0].split(":")[1].strip(" '")
self.commandDict['Value'] = value
self.commandDict['ParamName'] = "RenderToScreen"#selection[0].strip("'")
resp = self.connection.sendMsg(json.dumps(self.commandDict))
else:
if not self.currentObject.has_key('Mutable'):
print "Object not mutable."
return 0
print self.commandDict['OperationArg']+"'s API: "
n = -1
mkeys= self.currentObject['Mutable'].keys()
functions = []
attrs = []
for m in mkeys:
n+=1
if not self.currentObject.has_key(m):
print "{0:4} {1:12}".format(n,m)
if m[-2:] == '()':
functions.append(m)
else:
attrs.append(m)
else:
print "{0:4} {1:12} {2}".format(n,m,self.currentObject[m])
print "edit what?"
i = raw_input("> ")
if i.isdigit() and int(i) < (len(mkeys)):
selection = mkeys[int(i)]#map(unicode.strip,co[int(i)].split(':'))
if selection in functions:
value = raw_input("call function "+selection+ "? (y/N) " )
if len(value)>0 and value[0].lower() == 'y':
value = True
print "calling", selection
else:
value = False
else:
value = raw_input("set "+ selection+ " to: " )
print "setting ", selection," to '",value,"'"
self.commandDict['OperationType'] = 'Update'
self.commandDict['ComponentId'] = self.currentObject['Id']#filter(lambda x: x.find('Id')!=-1,co)[0].split(":")[1].strip(" '")
self.commandDict['Value'] = value
self.commandDict['ParamName'] = selection.strip("'")
resp = self.connection.sendMsg(json.dumps(self.commandDict))
print resp
else:
if i =="":
print "cancelled"
elif not i.isdigit():
print "syntax error"
else:
print "index out of range"
self.commandDict = {'OperationArg':self.commandDict['OperationArg'],
'OperationDetail':self.commandDict['OperationDetail']}
# co = [i for i in co if i.find(': <') == -1]
# for n in range(len(co)):
# print n, ": ", co[n].strip()
#
# print "edit what?"
# i = raw_input("> ")
#
# #argDict=eval('{%s}'%','.join(co))
# if i.isdigit() and int(i) < len(co):
# selection = map(unicode.strip,co[int(i)].split(':'))
# value = raw_input("set "+ selection[0]+ " to: " )
#
# print "setting ", selection[0]," to '",value,"'"
# self.commandDict['OperationType'] = 'Update'
# self.commandDict['ComponentId'] = filter(lambda x: x.find('Id')!=-1,co)[0].split(":")[1].strip(" '")
# self.commandDict['Value'] = value
# self.commandDict['ParamName'] = selection[0].strip("'")
# resp = self.connection.sendMsg(json.dumps(self.commandDict))
# print resp
# self.commandDict = {'OperationArg':None}
#
class TapRunningInstallation(object):
""" This is the main class responsible for console IO """
def __init__(self):
self.dieNow = False
def mainLoop(self):
m = MenuTree(TapConnection('localhost',22332))
while not self.dieNow:
m.showMenu()
m.acceptInput()
rin = raw_input("> ")
if m.executeSelection(rin) == -1:
self.dieNow = True
m.connection.close()
def handleDie(self, caller):
self.dieNow = True
def main(argv):
l = TapRunningInstallation()
l.mainLoop()
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
print('\nTerminated by keyboard.')