-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnaoqi.py
414 lines (348 loc) · 12.4 KB
/
naoqi.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
import sys
import ctypes
import weakref
import logging
import inspect
import qi
def load_inaoqi_deps():
""" Helper to laod _inaoqi.so deps on linux """
deps = [
"libboost_python.so",
"libboost_system.so",
"libboost_chrono.so",
"libboost_program_options.so",
"libboost_thread.so",
"libboost_filesystem.so",
"libboost_regex.so",
"libboost_locale.so",
"libboost_signals.so",
"libqi.so",
"libqitype.so",
"libalerror.so",
"libalthread.so",
"libalvalue.so",
"libqimessaging.so",
"libalcommon.so",
"libalproxies.so",
"libalpythontools.so",
"libalbehavior.so",
"libqipython.so",
]
this_dir = os.path.abspath(os.path.dirname(__file__))
for dep in deps:
full_path = os.path.join(this_dir, dep)
try:
ctypes.cdll.LoadLibrary(full_path)
except Exception:
pass
if sys.platform.startswith("linux"):
load_inaoqi_deps()
import inaoqi
import motion
import allog
def _getMethodParamCount(func):
r = inspect.getargspec(func)
#normal args
np = len(r[0])
#*args (none if non existent)
if r[1] is not None:
np = np + 1
#**kargs (none if non existent)
if r[2] is not None:
np = np + 1
return np
def autoBind(myClass, bindIfnoDocumented):
"""Show documentation for each
method of the class"""
# dir(myClass) is a list of the names of
# everything in class
myClass.setModuleDescription(myClass.__doc__)
for thing in dir(myClass):
# getattr(x, "y") is exactly: x.y
function = getattr(myClass, thing)
if callable(function):
if (type(function) == type(myClass.__init__)):
if (bindIfnoDocumented or function.__doc__ != ""):
if (thing[0] != "_"): # private method
if (function.__doc__):
myClass.functionName(thing, myClass.getName(), function.__doc__)
else:
myClass.functionName(thing, myClass.getName(), "")
for param in function.func_code.co_varnames:
if (param != "self"):
myClass.addParam(param)
myClass._bindWithParam(myClass.getName(),thing, _getMethodParamCount(function)-1)
class ALDocable():
def __init__(self, bindIfnoDocumented):
autoBind(self,bindIfnoDocumented)
# define the log handler to be used by the logging module
class ALLogHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
level_to_function = {
logging.DEBUG: allog.debug,
logging.INFO: allog.info,
logging.WARNING: allog.warning,
logging.ERROR: allog.error,
logging.CRITICAL: allog.fatal,
}
function = level_to_function.get(record.levelno, allog.debug)
function(record.getMessage(),
record.name,
record.filename,
record.funcName,
record.lineno)
# Same as above, but we force the category to be behavior.box
# *AND* we prefix the message with the module name
# look at errorInBox in choregraphe for explanation
class ALBehaviorLogHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
level_to_function = {
logging.DEBUG: allog.debug,
logging.INFO: allog.info,
logging.WARNING: allog.warning,
logging.ERROR: allog.error,
logging.CRITICAL: allog.fatal,
}
function = level_to_function.get(record.levelno, allog.debug)
function(record.name + ": " + record.getMessage(),
"behavior.box",
"", # record.filename in this case is simply '<string>'
record.funcName,
record.lineno)
# define a class that will be inherited by both ALModule and ALBehavior, to store instances of modules, so a bound method can be called on them.
class NaoQiModule():
_modules = dict()
@classmethod
def getModule(cls, name):
# returns a reference a module, giving its string, if it exists !
if(name not in cls._modules):
raise RuntimeError("Module " + str(name) + " does not exist")
return cls._modules[name]()
def __init__(self, name, logger=True):
# keep a weak reference to ourself, so a proxy can be called on this module easily
self._modules[name] = weakref.ref(self)
self.loghandler = None
if logger:
self.logger = logging.getLogger(name)
self.loghandler = ALLogHandler()
self.logger.addHandler(self.loghandler)
self.logger.setLevel(logging.DEBUG)
def __del__(self):
# when object is deleted, clean up dictionnary so we do not keep a weak reference to it
del self._modules[self.getName()]
if(self.loghandler != None):
self.logger.removeHandler(self.loghandler)
class ALBroker(inaoqi.broker):
def init(self):
pass
class ALModule(inaoqi.module, ALDocable, NaoQiModule):
def __init__(self,param):
inaoqi.module.__init__(self, param)
ALDocable.__init__(self, False)
NaoQiModule.__init__(self, param)
self.registerToBroker()
def __del__(self):
NaoQiModule.__del__(self)
def methodtest(self):
pass
def pythonChanged(self, param1, param2, param3):
pass
class ALBehavior(inaoqi.behavior, NaoQiModule):
# class var in order not to build it each time
_noNeedToBind = set(dir(inaoqi.behavior))
_noNeedToBind.add("getModule")
_noNeedToBind.add("onLoad")
_noNeedToBind.add("onUnload")
# deprecated since 1.14 methods
_noNeedToBind.add("log")
_noNeedToBind.add("playTimeline")
_noNeedToBind.add("stopTimeline")
_noNeedToBind.add("exitBehavior")
_noNeedToBind.add("gotoAndStop")
_noNeedToBind.add("gotoAndPlay")
_noNeedToBind.add("playTimelineParent")
_noNeedToBind.add("stopTimelineParent")
_noNeedToBind.add("exitBehaviorParent")
_noNeedToBind.add("gotoAndPlayParent")
_noNeedToBind.add("gotoAndStopParent")
# but we want to bind setParameter to listen runtime changes
_noNeedToBind.remove("setParameter")
def __init__(self, param, autoBind, brokerRegister=True):
inaoqi.behavior.__init__(self, param)
NaoQiModule.__init__(self, param, logger=False)
self.logger = logging.getLogger(param)
self.behaviorloghandler = ALBehaviorLogHandler()
self.logger.addHandler(self.behaviorloghandler)
self.logger.setLevel(logging.DEBUG)
self.resource = False
self.BIND_PYTHON(self.getName(), "__onLoad__", 0)
self.BIND_PYTHON(self.getName(), "__onUnload__", 0)
#always set autobind to true for the compatibility layer.
#sometime BIND_PYTHON do not specify the number of arguments
#that cant be guessed because the class is not provided to the method.
autoBind = True
if(autoBind):
behName = self.getName()
userMethList = set(dir(self)) - self._noNeedToBind
for methName in userMethList:
function = getattr(self, methName)
if callable(function) and type(function) == type(self.__init__):
if (methName[0] != "_"): # private method
self.functionName(methName, behName, "")
for param in function.func_code.co_varnames:
if (param != "self"):
self.addParam(param)
self._bindWithParam(behName, methName, _getMethodParamCount(function)-1)
if brokerRegister:
self.registerToBroker()
def setParameterInternal(self, a, b):
""" internal method used to call the good override for setParameter
which can be overrided in user class. legacy hell.
"""
self.setParameter(a, b)
def session(self):
return inaoqi.behavior.session(self)
def __del__(self):
NaoQiModule.__del__(self)
self.logger.removeHandler(self.behaviorloghandler)
self.behaviorloghandler.close()
def __onLoad__(self):
self._safeCallOfUserMethod("onLoad",None)
def __onUnload__(self):
if(self.resource):
self.releaseResource()
self._safeCallOfUserMethod("onUnload",None)
def setParameter(self, parameterName, newValue):
inaoqi.behavior.setParameter(self, parameterName, newValue)
def _safeCallOfUserMethod(self, functionName, functionArg):
import traceback
try:
if(functionName in dir(self)):
func = getattr(self, functionName)
if(func.im_func.func_code.co_argcount == 2):
func(functionArg)
else:
func()
return True
except BaseException, err:
if("onError" in dir(self)):
try:
self.onError(self.getName() + ':' + str(err))
except BaseException, err2:
self.logger.error(traceback.format_exc())
self._reportError(self.behaviorId, self.__class__.__name__, traceback.format_exc())
else:
self.logger.error(traceback.format_exc())
self._reportError(self.behaviorId, self.__class__.__name__, traceback.format_exc())
return False
# Depreciate this!!! Same as self.logger.info(), but function is always "log"
def log(self, p):
self.logger.info(p)
class MethodMissingMixin(object):
""" A Mixin' to implement the 'method_missing' Ruby-like protocol. """
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except:
class MethodMissing(object):
def __init__(self, wrapped, method):
self.__wrapped__ = wrapped
self.__method__ = method
def __call__(self, *args, **kwargs):
return self.__wrapped__.method_missing(self.__method__, *args, **kwargs)
return MethodMissing(self, attr)
def method_missing(self, *args, **kwargs):
""" This method should be overridden in the derived class. """
raise NotImplementedError(str(self.__wrapped__) + " 'method_missing' method has not been implemented.")
class postType(MethodMissingMixin):
def __init__(self):
""
def setProxy(self, proxy):
self.proxy = weakref.ref(proxy)
# print name
def method_missing(self, method, *args, **kwargs):
list = []
list.append(method)
for arg in args:
list.append(arg)
result = 0
try:
p = self.proxy()
result = p.pythonPCall(list)
except RuntimeError,e:
raise e
return result
class ALProxy(inaoqi.proxy,MethodMissingMixin):
def __init__(self, *args):
self.post = postType()
self.post.setProxy(self)
if (len (args) == 1):
inaoqi.proxy.__init__(self, args[0])
elif (len (args) == 2):
inaoqi.proxy.__init__(self, args[0], args[1])
else:
inaoqi.proxy.__init__(self, args[0], args[1], args[2])
def call(self, *args):
list = []
for arg in args:
list.append(arg)
return self.pythonCall(list)
def pCall(self, *args):
list = []
for arg in args:
list.append(arg)
return self.pythonPCall(list)
def method_missing(self, method, *args, **kwargs):
list = []
list.append(method)
for arg in args:
list.append(arg)
result = 0
try:
result = self.pythonCall(list)
except RuntimeError,e:
raise e
#print e.args[0]
return result
@staticmethod
def initProxies():
#Warning: The use of these default proxies is deprecated.
global ALMemory
global ALMotion
global ALFrameManager
global ALLeds
global ALLogger
global ALSensors
try:
ALMemory = inaoqi.getMemoryProxy()
except:
ALMemory = ALProxy("ALMemory")
try:
ALFrameManager = ALProxy("ALFrameManager")
except:
print "No proxy to ALFrameManager"
try:
ALMotion = ALProxy("ALMotion")
except:
print "No proxy to ALMotion"
try:
ALLeds = ALProxy("ALLeds")
except:
pass
try:
ALLogger = ALProxy("ALLogger")
except:
print "No proxy to ALLogger"
try:
ALSensors = ALProxy("ALSensors")
except:
pass
def createModule(name):
global moduleList
str = "moduleList.append("+ "module(\"" + name + "\"))"
exec(str)