ModuleManager.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
16 
17 
18 import string
19 import sys,os
20 import glob
21 
22 import OpenRTM_aist
23 
24 
25 CONFIG_EXT = "manager.modules.config_ext"
26 CONFIG_PATH = "manager.modules.config_path"
27 DETECT_MOD = "manager.modules.detect_loadable"
28 MOD_LOADPTH = "manager.modules.load_path"
29 INITFUNC_SFX = "manager.modules.init_func_suffix"
30 INITFUNC_PFX = "manager.modules.init_func_prefix"
31 ALLOW_ABSPATH = "manager.modules.abs_path_allowed"
32 ALLOW_URL = "manager.modules.download_allowed"
33 MOD_DWNDIR = "manager.modules.download_dir"
34 MOD_DELMOD = "manager.modules.download_cleanup"
35 MOD_PRELOAD = "manager.modules.preload"
36 
37 
38 
39 
55  """
56  """
57 
58 
59 
60 
76  def __init__(self, prop):
77  self._properties = prop
78 
79  self._configPath = prop.getProperty(CONFIG_PATH).split(",")
80  for i in range(len(self._configPath)):
81  tmp = [self._configPath[i]]
82  OpenRTM_aist.eraseHeadBlank(tmp)
83  self._configPath[i] = tmp[0]
84  self._loadPath = prop.getProperty(MOD_LOADPTH,"./").split(",")
85  for i in range(len(self._loadPath)):
86  tmp = [self._loadPath[i]]
87  OpenRTM_aist.eraseHeadBlank(tmp)
88  self._loadPath[i] = tmp[0]
89 
90  self._absoluteAllowed = OpenRTM_aist.toBool(prop.getProperty(ALLOW_ABSPATH),
91  "yes", "no", False)
92 
93  self._downloadAllowed = OpenRTM_aist.toBool(prop.getProperty(ALLOW_URL),
94  "yes", "no", False)
95 
96  self._initFuncSuffix = prop.getProperty(INITFUNC_SFX)
97  self._initFuncPrefix = prop.getProperty(INITFUNC_PFX)
99  self._rtcout = None
100  self._mgr = OpenRTM_aist.Manager.instance()
101 
102 
114  def __del__(self):
115  self.unloadAll()
116 
117 
118 
125  class Error:
126  def __init__(self, reason_):
127  self.reason = reason_
128 
129 
130 
131 
138  class NotFound:
139  def __init__(self, name_):
140  self.name = name_
141 
142 
143 
144 
152  def __init__(self, name_):
153  ModuleManager.NotFound.__init__(self, name_)
154 
155 
156 
157 
165  def __init__(self, name_):
166  ModuleManager.NotFound.__init__(self, name_)
167 
168 
169 
170 
178  def __init__(self, name_):
179  ModuleManager.NotFound.__init__(self, name_)
180 
181 
182 
183 
191  def __init__(self, reason_):
192  ModuleManager.Error.__init__(self, reason_)
193  ModuleManager.Error.__init__(self, reason_)
194 
195 
196 
197 
205  def __init__(self, reason_):
206  ModuleManager.Error.__init__(self, reason_)
207 
208 
209 
210 
218  def __init__(self, reason_):
219  ModuleManager.Error.__init__(self, reason_)
220 
221 
222 
223 
245  def load(self, file_name, init_func=None):
246  if not self._rtcout:
247  self._rtcout = self._mgr.getLogbuf("ModuleManager")
248 
249  self._rtcout.RTC_TRACE("load(fname = %s)", file_name)
250  if file_name == "":
251  raise ModuleManager.InvalidArguments("Invalid file name.")
252 
253  if OpenRTM_aist.isURL(file_name):
254  if not self._downloadAllowed:
255  raise ModuleManager.NotAllowedOperation("Downloading module is not allowed.")
256  else:
257  raise ModuleManager.NotFound("Not implemented.")
258 
259  import_name = os.path.split(file_name)[-1]
260  pathChanged=False
261  file_path = None
262  if OpenRTM_aist.isAbsolutePath(file_name):
263  if not self._absoluteAllowed:
264  raise ModuleManager.NotAllowedOperation("Absolute path is not allowed")
265  else:
266  splitted_name = os.path.split(file_name)
267  save_path = sys.path[:]
268  sys.path.append(splitted_name[0])
269  pathChanged = True
270  import_name = splitted_name[-1]
271  file_path = file_name
272 
273  else:
274  file_path = self.findFile(file_name, self._loadPath)
275  if not file_path:
276  raise ModuleManager.InvalidArguments("Invalid file name.")
277 
278  if not self.fileExist(file_path):
279  raise ModuleManager.FileNotFound(file_name)
280 
281  if not pathChanged:
282  splitted_name = os.path.split(file_path)
283  sys.path.append(splitted_name[0])
284 
285  ext_pos = import_name.find(".py")
286  if ext_pos > 0:
287  import_name = import_name[:ext_pos]
288  mo = __import__(str(import_name))
289 
290  if pathChanged:
291  sys.path = save_path
292 
293  dll = self.DLLEntity(mo,OpenRTM_aist.Properties())
294  dll.properties.setProperty("file_path",file_path)
295  self._modules.registerObject(dll)
296 
297 
298  if init_func is None:
299  return file_name
300 
301  self.symbol(file_path,init_func)(self._mgr)
302 
303  return file_name
304 
305 
306 
318  def unload(self, file_name):
319  dll = self._modules.find(file_name)
320  if not dll:
321  raise ModuleManager.NotFound(file_name)
322 
323  self._modules.unregisterObject(file_name)
324  return
325 
326 
327 
338  def unloadAll(self):
339  dlls = self._modules.getObjects()
340 
341  for dll in dlls:
342  ident = dll.properties.getProperty("file_path")
343  self._modules.unregisterObject(ident)
344  return
345 
346 
347 
360  def symbol(self, file_name, func_name):
361  dll = self._modules.find(file_name)
362  if not dll:
363  raise ModuleManager.ModuleNotFound(file_name)
364 
365  func = getattr(dll.dll,func_name,None)
366 
367  if not func:
368  raise ModuleManager.SymbolNotFound(func_name)
369 
370  return func
371 
372 
373 
385  def setLoadpath(self, load_path_list):
386  self._loadPath = load_path_list
387  return
388 
389 
390 
403  def getLoadPath(self):
404  return self._loadPath
405 
406 
407 
419  def addLoadpath(self, load_path):
420  for path in load_path:
421  self._loadPath.append(path)
422  return
423 
424 
425 
439  def getLoadedModules(self):
440  dlls = self._modules.getObjects()
441  modules = []
442  for dll in dlls:
443  modules.append(dll.properties)
444 
445  return modules
446 
447 
448  def __getRtcProfile(self, fname):
449  # file name with full path
450  fullname = fname
451  # directory name
452  dirname = os.path.dirname(fname)
453  sys.path.append(dirname)
454  # basename
455  basename = os.path.basename(fname)
456  # classname
457  classname = basename.split(".")[0].lower()
458 
459  # loaded profile = old profiles - new profiles
460  # for old
461  oldp = self._mgr.getFactoryProfiles()
462 
463  # for new
464  comp_spec_name = classname+"_spec"
465 
466  try:
467  imp_file = __import__(basename.split(".")[0])
468  except:
469  return None
470  comp_spec = getattr(imp_file,comp_spec_name,None)
471  if not comp_spec:
472  return None
473  newp = OpenRTM_aist.Properties(defaults_str=comp_spec)
474 
475  profs = []
476 
477  exists = False
478  for i in range(len(oldp)):
479  if oldp[i].getProperty("implementation_id") == newp.getProperty("implementation_id") and \
480  oldp[i].getProperty("type_name") == newp.getProperty("type_name") and \
481  oldp[i].getProperty("description") == newp.getProperty("description") and \
482  oldp[i].getProperty("version") == newp.getProperty("version"):
483  exists = True
484  if not exists:
485  profs.append(newp)
486 
487 
488  # loaded component profile have to be one
489  if len(profs) == 0:
490  return OpenRTM_aist.Properties()
491 
492  if len(profs) > 1:
493  return None
494 
495  return profs[0]
496 
497 
498 
512  # getting loadable module file path list.
513  modules_ = []
514  for path in self._loadPath:
515  if path == "":
516  continue
517 
518  flist = glob.glob(path + os.sep + '*.py')
519  for file in flist:
520  if file.find("__init__.py") == -1:
521  modules_.append(file)
522 
523  props = []
524  # getting module properties from loadable modules
525  for mod_ in modules_:
526  prop = self.__getRtcProfile(mod_)
527  if prop:
528  prop.setProperty("module_file_name",os.path.basename(mod_))
529  prop.setProperty("module_file_path", mod_)
530  props.append(prop)
531 
532  return props
533 
534 
535 
536 
547  def allowAbsolutePath(self):
548  self._absoluteAllowed = True
549 
550 
551 
563  self._absoluteAllowed = False
564 
565 
566 
580  self._downloadAllowed = True
581 
582 
583 
595  self._downloadAllowed = False
596 
597 
598 
613  def findFile(self, fname, load_path):
614  file_name = fname
615  for path in load_path:
616  if fname.find(".py") == -1:
617  f = str(path) + os.sep + str(file_name)+".py"
618  else:
619  f = str(path)+ os.sep + str(file_name)
620  if self.fileExist(f):
621  return f
622  return ""
623 
624 
625 
639  def fileExist(self, filename):
640  fname = filename
641  if fname.find(".py") == -1:
642  fname = str(filename)+".py"
643 
644  if os.path.isfile(fname):
645  return True
646 
647  return False
648 
649 
650 
651 
665  def getInitFuncName(self, file_path):
666  base_name = os.path.basename(file_path)
667  return str(self._initFuncPrefix)+str(base_name)+str(self._initFuncSuffix)
668 
669 
670 
671 
678  class DLL:
679  def __init__(self, dll):
680  self.dll = dll
681  return
682 
683 
684  class DLLEntity:
685  def __init__(self,dll,prop):
686  self.dll = dll
687  self.properties = prop
688 
689 
690  class DLLPred:
691  def __init__(self, name=None, factory=None):
692  self._filepath = name or factory
693 
694  def __call__(self, dll):
695  return self._filepath == dll.properties.getProperty("file_path")
OpenRTM_aist.ModuleManager.ModuleManager.__init__
def __init__(self, prop)
constructor
Definition: ModuleManager.py:76
OpenRTM_aist.ModuleManager.ModuleManager.NotFound.name
name
Definition: ModuleManager.py:140
OpenRTM_aist.ModuleManager.ModuleManager.allowAbsolutePath
def allowAbsolutePath(self)
Allow absolute load path.
Definition: ModuleManager.py:547
OpenRTM_aist.ModuleManager.ModuleManager._rtcout
_rtcout
Definition: ModuleManager.py:99
OpenRTM_aist.ModuleManager.ModuleManager.DLLPred.__init__
def __init__(self, name=None, factory=None)
Definition: ModuleManager.py:691
OpenRTM_aist.ModuleManager.ModuleManager.InvalidOperation
Definition: ModuleManager.py:217
OpenRTM_aist.ModuleManager.ModuleManager.findFile
def findFile(self, fname, load_path)
Search file from load path.
Definition: ModuleManager.py:613
OpenRTM_aist.ModuleManager.ModuleManager._downloadAllowed
_downloadAllowed
Definition: ModuleManager.py:93
OpenRTM_aist.ModuleManager.ModuleManager.Error.__init__
def __init__(self, reason_)
Definition: ModuleManager.py:126
OpenRTM_aist.ModuleManager.ModuleManager.DLLPred.__call__
def __call__(self, dll)
Definition: ModuleManager.py:694
OpenRTM_aist.ModuleManager.ModuleManager.unload
def unload(self, file_name)
Unload module.
Definition: ModuleManager.py:318
OpenRTM_aist.ModuleManager.ModuleManager.DLLEntity.__init__
def __init__(self, dll, prop)
Definition: ModuleManager.py:685
OpenRTM_aist.ModuleManager.ModuleManager.__del__
def __del__(self)
destructor
Definition: ModuleManager.py:114
OpenRTM_aist.ModuleManager.ModuleManager._loadPath
_loadPath
Definition: ModuleManager.py:84
OpenRTM_aist.ObjectManager.ObjectManager
Definition: ObjectManager.py:38
OpenRTM_aist.ModuleManager.ModuleManager.getInitFuncName
def getInitFuncName(self, file_path)
Create initialize function symbol.
Definition: ModuleManager.py:665
OpenRTM_aist.ModuleManager.ModuleManager._properties
_properties
Definition: ModuleManager.py:77
OpenRTM_aist.ModuleManager.ModuleManager.disallowAbsolutePath
def disallowAbsolutePath(self)
Forbid absolute load path.
Definition: ModuleManager.py:562
OpenRTM_aist.ModuleManager.ModuleManager.InvalidArguments.__init__
def __init__(self, reason_)
Definition: ModuleManager.py:205
OpenRTM_aist.ModuleManager.ModuleManager._initFuncPrefix
_initFuncPrefix
Definition: ModuleManager.py:97
OpenRTM_aist.ModuleManager.ModuleManager.NotAllowedOperation.__init__
def __init__(self, reason_)
Definition: ModuleManager.py:191
OpenRTM_aist.ModuleManager.ModuleManager._configPath
_configPath
Definition: ModuleManager.py:79
OpenRTM_aist.ModuleManager.ModuleManager.addLoadpath
def addLoadpath(self, load_path)
Add module load path.
Definition: ModuleManager.py:419
OpenRTM_aist.ModuleManager.ModuleManager._modules
_modules
Definition: ModuleManager.py:98
OpenRTM_aist.ModuleManager.ModuleManager.InvalidOperation.__init__
def __init__(self, reason_)
Definition: ModuleManager.py:218
OpenRTM_aist.ModuleManager.ModuleManager.getLoadedModules
def getLoadedModules(self)
Get loaded module names std::vector<coil::Properties> getLoadedModules();.
Definition: ModuleManager.py:439
OpenRTM_aist.ModuleManager.ModuleManager.Error.reason
reason
Definition: ModuleManager.py:127
OpenRTM_aist.ModuleManager.ModuleManager.ModuleNotFound.__init__
def __init__(self, name_)
Definition: ModuleManager.py:165
OpenRTM_aist.ModuleManager.ModuleManager.DLLPred._filepath
_filepath
Definition: ModuleManager.py:692
OpenRTM_aist.ModuleManager.ModuleManager.DLLPred
Definition: ModuleManager.py:690
OpenRTM_aist.ModuleManager.ModuleManager.FileNotFound
Definition: ModuleManager.py:151
OpenRTM_aist.ModuleManager.ModuleManager._initFuncSuffix
_initFuncSuffix
Definition: ModuleManager.py:96
OpenRTM_aist.ModuleManager.ModuleManager.SymbolNotFound.__init__
def __init__(self, name_)
Definition: ModuleManager.py:178
OpenRTM_aist.ModuleManager.ModuleManager.symbol
def symbol(self, file_name, func_name)
Look up a named symbol in the module.
Definition: ModuleManager.py:360
OpenRTM_aist.ModuleManager.ModuleManager.SymbolNotFound
Definition: ModuleManager.py:177
OpenRTM_aist.ModuleManager.ModuleManager.Error
Definition: ModuleManager.py:125
OpenRTM_aist.ModuleManager.ModuleManager.NotAllowedOperation
Definition: ModuleManager.py:190
OpenRTM_aist.StringUtil.split
def split(input, delimiter)
Split string by delimiter.
Definition: StringUtil.py:323
OpenRTM_aist.ModuleManager.ModuleManager.NotFound.__init__
def __init__(self, name_)
Definition: ModuleManager.py:139
OpenRTM_aist.ModuleManager.ModuleManager._mgr
_mgr
Definition: ModuleManager.py:100
OpenRTM_aist.ModuleManager.ModuleManager.fileExist
def fileExist(self, filename)
Check file existance.
Definition: ModuleManager.py:639
OpenRTM_aist.ModuleManager.ModuleManager.InvalidArguments
Definition: ModuleManager.py:204
OpenRTM_aist.CORBA_SeqUtil.find
def find(seq, f)
Return the index of CORBA sequence element that functor matches.
Definition: CORBA_SeqUtil.py:84
OpenRTM_aist.ModuleManager.ModuleManager.load
def load(self, file_name, init_func=None)
Load module.
Definition: ModuleManager.py:245
OpenRTM_aist.ModuleManager.ModuleManager.DLL
Definition: ModuleManager.py:678
OpenRTM_aist.ModuleManager.ModuleManager._absoluteAllowed
_absoluteAllowed
Definition: ModuleManager.py:90
OpenRTM_aist.ModuleManager.ModuleManager.ModuleNotFound
Definition: ModuleManager.py:164
OpenRTM_aist.ModuleManager.ModuleManager.unloadAll
def unloadAll(self)
Unload all modules.
Definition: ModuleManager.py:338
OpenRTM_aist.ModuleManager.ModuleManager.NotFound
Definition: ModuleManager.py:138
OpenRTM_aist.Properties.Properties
Definition: Properties.py:83
OpenRTM_aist.ModuleManager.ModuleManager.DLL.__init__
def __init__(self, dll)
Definition: ModuleManager.py:679
OpenRTM_aist.ModuleManager.ModuleManager.FileNotFound.__init__
def __init__(self, name_)
Definition: ModuleManager.py:152
OpenRTM_aist.ModuleManager.ModuleManager.setLoadpath
def setLoadpath(self, load_path_list)
Set default module load path.
Definition: ModuleManager.py:385
OpenRTM_aist.ModuleManager.ModuleManager.getLoadableModules
def getLoadableModules(self)
Get loadable module names.
Definition: ModuleManager.py:511
OpenRTM_aist.ModuleManager.ModuleManager.allowModuleDownload
def allowModuleDownload(self)
Allow module download.
Definition: ModuleManager.py:579
OpenRTM_aist.ModuleManager.ModuleManager.DLLEntity
Definition: ModuleManager.py:684
OpenRTM_aist.ModuleManager.ModuleManager.DLL.dll
dll
Definition: ModuleManager.py:680
OpenRTM_aist.ModuleManager.ModuleManager.getLoadPath
def getLoadPath(self)
Get default module load path.
Definition: ModuleManager.py:403
OpenRTM_aist.ModuleManager.ModuleManager.DLLEntity.properties
properties
Definition: ModuleManager.py:687
OpenRTM_aist.ModuleManager.ModuleManager.DLLEntity.dll
dll
Definition: ModuleManager.py:686
OpenRTM_aist.ModuleManager.ModuleManager.__getRtcProfile
def __getRtcProfile(self, fname)
Definition: ModuleManager.py:448
OpenRTM_aist.ModuleManager.ModuleManager.disallowModuleDownload
def disallowModuleDownload(self)
Forbid module download.
Definition: ModuleManager.py:594
OpenRTM_aist.ModuleManager.ModuleManager
@biref ModuleManager class
Definition: ModuleManager.py:54
OpenRTM_aist.NVUtil.append
def append(dest, src)
Definition: NVUtil.py:386


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Apr 21 2025 02:45:06