Exemple #1
0
    def registerMetaClasses(cls, paths):
        """This function is helper function to register a list of paths.

        :param paths: A list of module or package paths, see registerByModule() and registerByPackage() for the path format.
        :type paths: list(str)
        """
        for p in paths:
            if len(p.split(".")) > 1:
                importedModule = modules.importModule(p)

                if importedModule is None:
                    continue
                p = os.path.realpath(importedModule.__file__)
                if os.path.basename(p).startswith("__"):
                    p = os.path.dirname(p)
                elif p.endswith(".pyc"):
                    p = p[:-1]
            if os.path.isdir(p):
                cls.registerByPackage(p)
                continue
            elif os.path.isfile(p):
                importedModule = modules.importModule(p)
                if importedModule:
                    cls.registerByModule(importedModule)
                    continue
Exemple #2
0
    def registerPaths(self, paths):
        """This function is helper function to register a list of paths.

        :param paths: A list of module or package paths, see registerByModule() and registerByPackage() for the path format.
        :type paths: list(str)
        """
        self.basePaths.extend(paths)
        for p in paths:
            if not p or p.endswith(".pyc"):
                continue
            importedModule = None
            if os.path.exists(p) and os.path.isdir(p):
                self.registerByPackage(p)
                continue
            elif os.path.isfile(p):
                try:
                    importedModule = modules.importModule(modules.asDottedPath(os.path.normpath(p)))
                except ImportError:
                    logger.error("Failed to import Plugin module: {}, skipped Load".format(p), exc_info=True)
                    continue
            elif modules.isDottedPath(p):
                try:
                    importedModule = modules.importModule(os.path.normpath(p))
                except ImportError:
                    logger.error("Failed to import Plugin module: {}, skipped Load".format(p), exc_info=True)
                    continue
            if importedModule:
                self.registerByModule(importedModule)
                continue
Exemple #3
0
    def registerByModule(cls, module):
        """ This function registry a module by search all class members of the module and registers any class that is an
        instance of the plugin class

        :param module: the module path to registry
        :type module: str
        """
        if isinstance(module, basestring):
            module = modules.importModule(module)
        if inspect.ismodule(module):
            for member in modules.iterMembers(module,
                                              predicate=inspect.isclass):
                cls.registerMetaClass(member[1])
Exemple #4
0
    def registerPaths(self, paths):
        """This function is helper function to register a list of paths.

        :param paths: A list of module or package paths, see registerByModule() and registerByPackage() for the path format.
        :type paths: list(str)
        """
        self.basePaths.extend(paths)
        for p in paths:
            if not p or p.endswith(".pyc"):
                continue
            importedModule = None
            if os.path.exists(p) and os.path.isdir(p):
                self.registerByPackage(p)
                continue
            elif os.path.isfile(p):
                importedModule = modules.importModule(
                    modules.asDottedPath(os.path.normpath(p)))
            elif modules.isDottedPath(p):
                importedModule = modules.importModule(os.path.normpath(p))
            if importedModule:
                self.registerByModule(importedModule)
                continue
Exemple #5
0
    def registerMetaClasses(cls, paths):
        """This function is helper function to register a list of paths.

        :param paths: A list of module or package paths, see registerByModule() and registerByPackage() for the path format.
        :type paths: list(str)
        """
        for p in paths:
            if os.path.isdir(p):
                cls.registerByPackage(p)
                continue
            elif os.path.isfile(p):
                importedModule = modules.importModule(modules.asDottedPath(os.path.normpath(p)))
                if importedModule:
                    cls.registerByModule(importedModule)
                    continue
Exemple #6
0
    def registerByPackage(cls, pkg):
        """This function is similar to registerByModule() but works on packages, this is an expensive operation as it
        requires a recursive search by importing all sub modules and and searching them.

        :param pkg: The package path to register eg. zoo.libs.apps
        :type pkg: str
        """
        visited = set()
        for subModule in modules.iterModules(pkg):
            filename = os.path.splitext(os.path.basename(subModule))[0]
            if filename.startswith("__") or filename in visited:
                continue
            visited.add(filename)
            subModuleObj = modules.importModule(subModule)
            for member in modules.iterMembers(subModuleObj, predicate=inspect.isclass):
                cls.registerMetaClass(member[1])
Exemple #7
0
    def registerByPackage(self, pkg):
        """This function is similar to registerByModule() but works on packages, this is an expensive operation as it
        requires a recursive search by importing all sub modules and and searching them.

        :param pkg: The package path to register eg. zoo.libs.apps
        :type pkg: str
        """
        for subModule in modules.iterModules(pkg):
            filename = os.path.splitext(os.path.basename(subModule))[0]
            if filename.startswith("__") or subModule.endswith(".pyc"):
                continue
            subModuleObj = modules.importModule(
                modules.asDottedPath(os.path.normpath(subModule)))
            for member in modules.iterMembers(subModuleObj,
                                              predicate=inspect.isclass):
                self.registerPlugin(member[1])