Exemple #1
0
def testGetPkgimport():
    # myp11.mkdir("/tmp/asdf")
    # FileFinder 应该是类似java的classload的东西,用来寻找可导入的类库
    for item in pkgutil.iter_importers():
        # print(item)
        for item2 in pkgutil.iter_importer_modules(item):
            # print(item2)
            pass
def find_and_run_plugins(plugin_prefix):
    plugins = {}

    # 探索並且載入外掛
    print(f'Discovering plugins with prefix: {plugin_prefix}')
    for _, name, _ in pkgutil.iter_importer_modules():
        if name.startswith(plugin_prefix):
            module = importlib.import_module(name)
            plugins[name] = module

    # 執行外掛
    for name, module in plugins.items():
        print(f'Running plugin {name}')
        module.run()
Exemple #3
0
def iter_modules():
    # apparently pkgutil had some issues in python 2.6. Accessing any root level directories
    # failed. and it got the entire process of importing fail. Since we only need any
    # aria_extension related loading, in the meantime we could try to import only those
    # (and assume they are not located at the root level.
    # [In python 2.7 it does actually ignore any OSError].
    yielded = {}
    for importer in pkgutil.iter_importers():
        try:
            for module_name, ispkg in pkgutil.iter_importer_modules(importer):
                if module_name not in yielded:
                    yielded[module_name] = True
                    yield importer, module_name, ispkg
        except OSError:
            pass
Exemple #4
0
def searchPaths(pattern):
    '''
    Finds all modules/packages available in the sys.path that respect the provided pattern. The search is done directly on the
    importers based on PEP 302. Basically this search guarantees that all modules that are defined (even if some might
    not be imported) respecting the pattern will be obtained.
    
    @param pattern: string
        The pattern is formed based on full module name, ex:
        
            __setup__
            Will return a map with the key setup and the values will contain all the paths that define a __setup__
            module.
            
            __setup__.*
            Will return all the modules that are found in __setup__ package.
            
            __setup__.**
            Will return all the modules and sub modules that are found in __setup__ package.
            
            __setup__.*_http
            Will return all the modules that are found in __setup__ package that end with _http.
    @return: dictionary{tuple(boolean, string), list[string]}
        A dictionary containing as a key a tuple with a flag indicating that the full name is a package and as a second value the package/module full path,
        and as a value a list of paths where this package/module is defined.
    '''
    assert isinstance(pattern, str), 'Invalid module pattern %s' % pattern
    modules, importers = {}, None
    k = pattern.rfind('.')
    if k >= 0:
        name = pattern[k + 1:]
        parent = searchPaths(pattern[:k])

        if name == '**':
            while parent:
                keyPack, pckgPaths = parent.popitem()
                isPackage, pckg = keyPack
                for path in pckgPaths:
                    if isPackage:
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                            keyPack = (isPkg, pckg + ('.' if pckg else '') + modulePath)
                            if isPkg:
                                paths = parent.get(keyPack)
                                if paths is None: paths = parent[keyPack] = []
                                if path not in paths: paths.append(path)

                            paths = modules.get(keyPack)
                            if paths is None: paths = modules[keyPack] = []
                            if path not in paths: paths.append(path)
            return modules
        elif name.find('*') >= 0:
            matcher = re.compile('[a-zA-Z0-9_]*'.join([re.escape(e) for e in name.split('*')]))
            for keyPack, pckgPaths in parent.items():
                isPackage, pckg = keyPack
                for path in pckgPaths:
                    if isPackage:
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            if matcher.match(modulePath):
                                path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                                keyPack = (isPkg, pckg + ('.' if pckg else '') + modulePath)
                                paths = modules.get(keyPack)
                                if paths is None: paths = modules[keyPack] = []
                                if path not in paths: paths.append(path)
            return modules
        else: importers = [(keyPack[0], keyPack[1], get_importer(path)) for keyPack, paths in parent.items() for path in paths]
    else:
        name = pattern
        importers = [(True, '', imp) for imp in iter_importers()]

    for isPackage, package, importer in importers:
        if isPackage:
            moduleLoader = importer.find_module(name)
            if moduleLoader:
                path = dirname(moduleLoader.get_filename(name))
                keyPack = (moduleLoader.is_package(name), package + ('.' if package else '') + name)
            else: keyPack = None
        elif package == name or package.endswith('.' + name):
            nameMod = name
            kk = nameMod.rfind('.')
            if kk >= 0: nameMod = nameMod[kk + 1:]
            moduleLoader = importer.find_module(nameMod)
            path = dirname(moduleLoader.get_filename(nameMod))
            keyPack = (False, package)
        else: keyPack = None

        if keyPack:
            paths = modules.get(keyPack)
            if paths is None: paths = modules[keyPack] = []
            if path not in paths: paths.append(path)

    return modules
Exemple #5
0
def searchModules(pattern):
    '''
    Finds all modules available in the sys.path that respect the provided pattern. The search is done directly on the
    importers based on PEP 302. Basically this search guarantees that all modules that are defined (even if some might
    not be imported) respecting the pattern will be obtained.
    
    @param pattern: string
        The pattern is formed based on full module name, ex:
        
            __setup__
            Will return a map with the key setup and the values will contain all the paths that define a __setup__
            module.
            
            __setup__.*
            Will return all the modules that are found in __setup__ package.
            
            __setup__.**
            Will return all the modules and sub modules that are found in __setup__ package.
            
            __setup__.*_http
            Will return all the modules that are found in __setup__ package that end with _http.
    @return: dictionary{string, list[string]}
        A dictionary containing as a key the module full name, and as a value a list of paths where this module is
        defined.
    '''
    assert isinstance(pattern, str), 'Invalid module pattern %s' % pattern
    modules, importers = {}, None
    k = pattern.rfind('.')
    if k >= 0:
        name = pattern[k + 1:]
        parent = searchModules(pattern[:k])
        if name == '**':
            while parent:
                pckg, pckgPaths = parent.popitem()
                for path in pckgPaths:
                    moduleLoader = get_importer(dirname(path)).find_module(pckg)
                    if moduleLoader and moduleLoader.is_package(pckg):
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                            if isPkg:
                                paths = parent.setdefault(pckg + ('.' if pckg else '') + modulePath, [])
                                if path not in paths: paths.append(path)
                            paths = modules.setdefault(pckg + ('.' if pckg else '') + modulePath, [])
                            if path not in paths: paths.append(path)
            return modules
        elif name.find('*') >= 0:
            matcher = re.compile('[a-zA-Z0-9_]*'.join([re.escape(e) for e in name.split('*')]))
            for pckg, pckgPaths in parent.items():
                for path in pckgPaths:
                    moduleLoader = get_importer(dirname(path)).find_module(pckg)
                    if moduleLoader and moduleLoader.is_package(pckg):
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            if matcher.match(modulePath):
                                path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                                paths = modules.setdefault(pckg + ('.' if pckg else '') + modulePath, [])
                                if path not in paths: paths.append(path)
            return modules
        else: importers = [(pckg, get_importer(path)) for pckg, paths in parent.items() for path in paths ]
    else:
        name = pattern
        importers = [('', imp) for imp in iter_importers()]

    for package, importer in importers:
        moduleLoader = importer.find_module(name)
        if moduleLoader:
            path = dirname(moduleLoader.get_filename(name))
            paths = modules.setdefault(package + ('.' if package else '') + name, [])
            if path not in paths: paths.append(path)
    return modules
Exemple #6
0
def searchPaths(pattern):
    '''
    Finds all modules/packages available in the sys.path that respect the provided pattern. The search is done directly on the
    importers based on PEP 302. Basically this search guarantees that all modules that are defined (even if some might
    not be imported) respecting the pattern will be obtained.
    
    @param pattern: string
        The pattern is formed based on full module name, ex:
        
            __setup__
            Will return a map with the key setup and the values will contain all the paths that define a __setup__
            module.
            
            __setup__.*
            Will return all the modules that are found in __setup__ package.
            
            __setup__.**
            Will return all the modules and sub modules that are found in __setup__ package.
            
            __setup__.*_http
            Will return all the modules that are found in __setup__ package that end with _http.
    @return: dictionary{tuple(boolean, string), list[string]}
        A dictionary containing as a key a tuple with a flag indicating that the full name is a package and as a second value the package/module full path,
        and as a value a list of paths where this package/module is defined.
    '''
    assert isinstance(pattern, str), 'Invalid module pattern %s' % pattern
    modules, importers = {}, None
    k = pattern.rfind('.')
    if k >= 0:
        name = pattern[k + 1:]
        parent = searchPaths(pattern[:k])

        if name == '**':
            while parent:
                keyPack, pckgPaths = parent.popitem()
                isPackage, pckg = keyPack
                for path in pckgPaths:
                    if isPackage:
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                            keyPack = (isPkg, pckg + ('.' if pckg else '') + modulePath)
                            if isPkg:
                                paths = parent.get(keyPack)
                                if paths is None: paths = parent[keyPack] = []
                                if path not in paths: paths.append(path)

                            paths = modules.get(keyPack)
                            if paths is None: paths = modules[keyPack] = []
                            if path not in paths: paths.append(path)
            return modules
        elif name.find('*') >= 0:
            matcher = re.compile('[a-zA-Z0-9_]*'.join([re.escape(e) for e in name.split('*')]))
            for keyPack, pckgPaths in parent.items():
                isPackage, pckg = keyPack
                for path in pckgPaths:
                    if isPackage:
                        moduleImporter = get_importer(path)
                        for modulePath, isPkg in iter_importer_modules(moduleImporter):
                            if matcher.match(modulePath):
                                path = dirname(moduleImporter.find_module(modulePath).get_filename(modulePath))
                                keyPack = (isPkg, pckg + ('.' if pckg else '') + modulePath)
                                paths = modules.get(keyPack)
                                if paths is None: paths = modules[keyPack] = []
                                if path not in paths: paths.append(path)
            return modules
        else: importers = [(keyPack[0], keyPack[1], get_importer(path)) for keyPack, paths in parent.items() for path in paths]
    else:
        name = pattern
        importers = [(True, '', imp) for imp in iter_importers()]

    for isPackage, package, importer in importers:
        if isPackage:
            moduleLoader = importer.find_module(name)
            if moduleLoader:
                path = dirname(moduleLoader.get_filename(name))
                keyPack = (moduleLoader.is_package(name), package + ('.' if package else '') + name)
            else: keyPack = None
        elif package == name or package.endswith('.' + name):
            nameMod = name
            kk = nameMod.rfind('.')
            if kk >= 0: nameMod = nameMod[kk + 1:]
            moduleLoader = importer.find_module(nameMod)
            path = dirname(moduleLoader.get_filename(nameMod))
            keyPack = (False, package)
        else: keyPack = None

        if keyPack:
            paths = modules.get(keyPack)
            if paths is None: paths = modules[keyPack] = []
            if path not in paths: paths.append(path)

    return modules
Exemple #7
0
 def update_event(self, inp=-1):
     self.set_output_val(0, pkgutil.iter_importer_modules())
def magic_router(*args, module):
    module_name = module.__name__
    importer = pkgutil.get_importer(module.__path__[0])
    iter = pkgutil.iter_importer_modules(importer)
    return {name: bot(module_name, name) for name, _ in iter}
 def find_modules_names(self, name):
     p = importlib.import_module(name)
     return [
         name for _, name, _ in pkgutil.iter_importer_modules(
             [os.path.dirname(p.__file__)])
     ]