Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpython_bridge.py
More file actions
Latest commit
161 lines (141 loc) · 5.39 KB
/
Copy pathpython_bridge.py
File metadata and controls
161 lines (141 loc) · 5.39 KB
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
importargparse
importthreading
importsys
fromPythonBridgeimportbridge_globals, bridge_hooks, flask_platform, msgpack_socket_platform
fromPythonBridge.bridge_hooksimport*
fromPythonBridge.object_registryimportregistry
classEvalCommand:
statements=""
binding= {}
commandId=0
def__init__(self, commandId, statements, bindings):
self.statements=statements
self.commandId=commandId
self.bindings=bindings
defexecute_using_env(self, env):
try:
env.update(self.bindings)
exec(self.statements, globals(), env)
exceptExceptionaserr:
self.perform_proceed_action(notify_error(err,self))
defperform_proceed_action(self, actionDict):
actionSymbol=actionDict['action']
ifactionSymbol=="IGNORE":
pass
ifactionSymbol=="DROP_QUEUE":
bridge_globals.globalCommandList.drop_queue()
ifactionSymbol=="REPLACE_COMMAND":
commandDict=actionDict["command"]
bridge_globals.globalCommandList.push_command_at_first(EvalCommand(commandDict["commandId"], commandDict["statements"], commandDict["bindings"]))
defcommand_id(self):
returnself.commandId
classLogger():
deflog(self, msg):
print(str(msg), file=sys.stderr, flush=True)
classNoLogger():
deflog(self, msg):
pass
# This List is thought to be multi-producer and single-consumer. For optimal results wait for push_command return value to push another command that depends on the previous one.
classPythonCommandList:
currentCommandIndex=0
commandList= []
listLock=threading.Lock()
consumeSemaphore=threading.Semaphore(value=0)
# This method locks the thread until the command has been succesfully appended to the list. Even though that it has a lock inside, we do not expect long waiting time.
defpush_command(self, aCommand):
self.listLock.acquire()
self.commandList.append(aCommand)
commandIndex=len(self.commandList) -1
self.listLock.release()
self.consumeSemaphore.release()
returncommandIndex
defpush_command_at_first(self, aCommand):
self.listLock.acquire()
self.commandList.insert(self.currentCommandIndex, aCommand)
self.listLock.release()
self.consumeSemaphore.release()
returnself.currentCommandIndex
defdrop_queue(self):
self.listLock.acquire()
self.consumeSemaphore=threading.Semaphore(value=0)
self.currentCommandIndex=len(self.commandList)
self.listLock.release()
defconsume_command(self):
repeatMonitorFlag=True
whilerepeatMonitorFlag:
self.consumeSemaphore.acquire()
self.listLock.acquire()
repeatMonitorFlag=False
if(self.currentCommandIndex>=len(self.commandList)):
repeatMonitorFlag=True
self.listLock.release()
command=self.commandList[self.currentCommandIndex]
self.currentCommandIndex+=1
self.listLock.release()
returncommand
defget_current_command(self):
ifself.currentCommandIndex==0:
returnNone
self.listLock.acquire()
command=self.commandList[self.currentCommandIndex-1]
self.listLock.release()
returncommand
defget_command_list(self):
self.listLock.acquire()
listCopy=self.commandList.copy()
self.listLock.release()
returnlistCopy
#### UTILS FUNCTIONS
defclean_locals_env():
returnlocals()
defdeserialize(text):
returnbridge_globals.msg_service.serializer.deserialize(text)
defenqueue_command(data):
bridge_globals.globalCommandList.push_command(EvalCommand(
data["commandId"],
data["statements"],
{k: deserialize(v) fork, vindata["bindings"].items()}))
defrun_bridge():
ap=argparse.ArgumentParser()
ap.add_argument("-p", "--port", required=False,
help="port to be used for receiving instructions")
ap.add_argument("-o", "--pharo", required=True,
help="port to be used for sending notifications back to pharo")
ap.add_argument("-m", "--method", required=False,
help="identifier for communication protocol strategy http or msgpack")
ap.add_argument("--log", required=False, const=True, nargs="?",
help="enable logging")
args=vars(ap.parse_args())
bridge_globals.pharoPort=args["pharo"]
ifargs["log"]:
bridge_globals.logger=Logger()
else:
bridge_globals.logger=NoLogger()
bridge_globals.pyPort=args["port"]
bridge_globals.globalCommandList=PythonCommandList()
globalCommandList=bridge_globals.globalCommandList
env=clean_locals_env()
msg_service=None
ifargs["port"] ==None:
args["port"] ='0'
ifargs["method"] ==None:
args["method"] ='http'
ifargs["method"] =='http':
fromPythonBridgeimportflask_platform
msg_service=flask_platform.build_service(int(args["port"]), int(args["pharo"]), enqueue_command)
elifargs["method"] =='msgpack':
fromPythonBridgeimportmsgpack_socket_platform
msg_service=msgpack_socket_platform.build_service(int(args["port"]), int(args["pharo"]), enqueue_command)
else:
raiseException("Invalid communication strategy.")
bridge_globals.msg_service=msg_service
msg_service.start()
bridge_globals.logger.log("PYTHON: Start consuming commands")
whileTrue:
command=globalCommandList.consume_command()
bridge_globals.logger.log("PYTHON: Executing command "+command.command_id())
bridge_globals.logger.log("PYTHON: "+command.statements)
command.execute_using_env(env)
bridge_globals.logger.log("PYTHON: Finished command execution")
if__name__=="__main__":
run_bridge()