00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import string
00019 import os
00020
00021 import OpenRTM
00022
00023
00024 CONFIG_EXT = "manager.modules.config_ext"
00025 CONFIG_PATH = "manager.modules.config_path"
00026 DETECT_MOD = "manager.modules.detect_loadable"
00027 MOD_LOADPTH = "manager.modules.load_path"
00028 INITFUNC_SFX = "manager.modules.init_func_suffix"
00029 INITFUNC_PFX = "manager.modules.init_func_prefix"
00030 ALLOW_ABSPATH = "manager.modules.abs_path_allowed"
00031 ALLOW_URL = "manager.modules.download_allowed"
00032 MOD_DWNDIR = "manager.modules.download_dir"
00033 MOD_DELMOD = "manager.modules.download_cleanup"
00034 MOD_PRELOAD = "manager.modules.preload"
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 class ModuleManager:
00054 """
00055 """
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 def __init__(self, prop):
00076 self._properties = prop
00077
00078 self._configPath = prop.getProperty(CONFIG_PATH).split(",")
00079 for i in range(len(self._configPath)):
00080 tmp = [self._configPath[i]]
00081 OpenRTM.eraseHeadBlank(tmp)
00082 self._configPath[i] = tmp[0]
00083
00084 self._loadPath = prop.getProperty(MOD_LOADPTH).split(",")
00085 for i in range(len(self._loadPath)):
00086 tmp = [self._loadPath[i]]
00087 OpenRTM.eraseHeadBlank(tmp)
00088 self._loadPath[i] = tmp[0]
00089
00090 self._absoluteAllowed = OpenRTM.toBool(prop.getProperty(ALLOW_ABSPATH),
00091 "yes", "no", False)
00092
00093 self._downloadAllowed = OpenRTM.toBool(prop.getProperty(ALLOW_URL),
00094 "yes", "no", False)
00095
00096 self._initFuncSuffix = prop.getProperty(INITFUNC_SFX)
00097 self._initFuncPrefix = prop.getProperty(INITFUNC_PFX)
00098 self._modules = {}
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 def __del__(self):
00114
00115 pass
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 class Error:
00126 def __init__(self, reason_):
00127 self.reason = reason_
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 class NotFound:
00139 def __init__(self, name_):
00140 self.name = name_
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 class FileNotFound(NotFound):
00152 def __init__(self, name_):
00153 ModuleManager.NotFound.__init__(self, name_)
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 class ModuleNotFound(NotFound):
00165 def __init__(self, name_):
00166 ModuleManager.NotFound.__init__(self, name_)
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 class SymbolNotFound(NotFound):
00178 def __init__(self, name_):
00179 ModuleManager.NotFound.__init__(self, name_)
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 class NotAllowedOperation(Error):
00191 def __init__(self, reason_):
00192 ModuleManager.Error.__init__(self, reason_)
00193 ModuleManager.Error.__init__(self, reason_)
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 class InvalidArguments(Error):
00205 def __init__(self, reason_):
00206 ModuleManager.Error.__init__(self, reason_)
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 class InvalidOperation(Error):
00218 def __init__(self, reason_):
00219 ModuleManager.Error.__init__(self, reason_)
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 def load(self, file_name, init_func=None):
00244 if file_name == "":
00245 raise ModuleManager.InvalidArguments, "Invalid file name."
00246
00247 if OpenRTM.isURL(file_name):
00248 if not self._downloadAllowed:
00249 raise ModuleManager.NotAllowedOperation, "Downloading module is not allowed."
00250 else:
00251 raise ModuleManager.NotFound, "Not implemented."
00252
00253 if OpenRTM.isAbsolutePath(file_name):
00254 if not self._absoluteAllowed:
00255 raise ModuleManager.NotAllowedOperation, "Absolute path is not allowed"
00256 else:
00257 file_path = file_name
00258
00259 else:
00260 file_path = self.findFile(file_name, self._loadPath)
00261
00262 if file_path == "":
00263 raise ModuleManager.InvalidArguments, "Invalid file name."
00264
00265 if not self.fileExist(file_path+".py"):
00266 raise ModuleManager.FileNotFound, file_path
00267
00268 mo = __import__(str(file_path))
00269 self._modules[file_name] = self.DLL(mo)
00270
00271 if init_func is None:
00272 return file_path
00273
00274 if file_path == "":
00275 raise ModuleManager.InvalidOperation, "Invalid file name"
00276
00277 try:
00278 self._modules[file_name].dll.__getattribute__(init_func)()
00279 except:
00280 print "Could't call init_func: ", init_func
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 def unload(self, file_name):
00296 if not self._modules.has_key(file_name):
00297 raise ModuleManager.NotFound, file_name
00298
00299
00300 del self._modules[file_name]
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 def unloadAll(self):
00315 keys = self._modules.keys()
00316
00317 self._modules.clear()
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 def symbol(self, file_name, func_name):
00334 if not self._modules.has_key(file_name):
00335 raise ModuleManager.ModuleNotFound, file_name
00336
00337 func = self._modules[file_name].dll.symbol(func_name)
00338
00339 if not func:
00340 raise ModuleManager.SymbolNotFound, func_name
00341
00342 return func
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 def setLoadpath(self, load_path_list):
00358 self._loadPath = load_path_list
00359 return
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 def getLoadPath(self):
00376 return self._loadPath
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 def addLoadpath(self, load_path):
00392 for path in load_path:
00393 self._loadPath.append(path)
00394 return
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 def getLoadedModules(self):
00411 modules = []
00412 keys = self._modules.keys()
00413 for mod_name in keys:
00414 modules.append(mod_name)
00415
00416 return modules
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 def getLoadableModules(self):
00433 return []
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 def allowAbsolutePath(self):
00448 self._absoluteAllowed = True
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 def disallowAbsolutePath(self):
00463 self._absoluteAllowed = False
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 def allowModuleDownload(self):
00480 self._downloadAllowed = True
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 def disallowModuleDownload(self):
00495 self._downloadAllowed = False
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 def findFile(self, fname, load_path):
00514 file_name = fname
00515
00516 if len(load_path) == 1:
00517 load_path.append(".")
00518 for path in load_path:
00519 f = str(path)+"/"+str(file_name)+".py"
00520 if self.fileExist(f):
00521 return fname
00522 return ""
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 def fileExist(self, filename):
00540 try:
00541 infile = open(filename)
00542 except:
00543 return False
00544
00545 infile.close()
00546 return True
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 def getInitFuncName(self, file_path):
00564 base_name = os.path.basename(file_path)
00565 return str(self._initFuncPrefix)+str(base_name)+str(self._initFuncSuffix)
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 class DLL:
00577 def __init__(self, dll):
00578 self.dll = dll