libLoader.py
Go to the documentation of this file.
00001 # MIT License
00002 #
00003 # Copyright (c) <2015> <Ikergune, Etxetar>
00004 #
00005 # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
00006 # (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
00007 # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
00008 # subject to the following conditions:
00009 #
00010 # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 #
00012 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00013 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
00014 # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00015 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016 
00017 import os
00018 import re
00019 import imp
00020 
00021 regex = re.compile(ur'^(.*)(\b.msg\b)(.*)$')
00022 
00023 
00024 class LibLoader:
00025     @staticmethod
00026     def loadFromFile(filepath):
00027         ## \brief Load file from path
00028         # \param File path
00029         mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
00030 
00031         if file_ext.lower() == '.py':
00032             py_mod = imp.load_source(mod_name, filepath)
00033 
00034         elif file_ext.lower() == '.pyc':
00035             py_mod = imp.load_compiled(mod_name, filepath)
00036 
00037         return py_mod
00038 
00039     @staticmethod
00040     def load3rdParty(filepath, className):
00041         ## \brief Load class from file
00042         # \param File path
00043         # \param Class name
00044         module = LibLoader.loadFromFile(filepath)
00045         return getattr(module, className)
00046 
00047     @staticmethod
00048     def loadFromSystem(lib):
00049         ## \brief Load library
00050         # \param library name
00051         module = None
00052         matches = re.search(regex, lib)
00053         if matches is not None:
00054             module_name = str(matches.group(1)) + str(matches.group(2))
00055             modules = str(matches.group(3)).split(".")
00056             modules = modules[1: len(modules)]
00057             module = __import__(module_name, globals(), locals(), [module_name.split('.')[-1]])
00058             for name in modules:
00059                 module = getattr(module, name)
00060         return module
00061 
00062 
00063 def generateRosDependencies():
00064     directories = os.environ["ROS_PACKAGE_PATH"].split(":")
00065     imports = """from include.logger import Log\n\n"""
00066     imports = """unloaded = 0\nlibs=[]\n"""
00067     for directory in directories:
00068         if os.path.isdir(directory):
00069             for folder in os.listdir(directory):
00070                 if "msg" in folder:
00071                     imports += """try:\n    import {}.msg\nexcept Exception:\n    unloaded += 1\n    libs.append('{}')\n""".format(folder, folder)
00072     imports += "Log('WARNING', str(unloaded) + ' libraries not loaded: ' + str(libs))"
00073     path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ros", "dependencies", "generated.py")
00074     f = open(path, 'w')
00075     f.write(imports)
00076     f.close()


firos
Author(s): IƱigo Gonzalez, igonzalez@ikergune.com
autogenerated on Thu Jun 6 2019 17:51:04