def _lookup_path(import_name_or_path): """ Detects the root of the given name. This function is an iterable generator. Parameters ---------- import_name_or_path : `str` An extension's import name, or it's absolute path. Yields ------ import_name : `None`, `str` Import name to an extension file. path : `str` Path of the file. Raise ----- ImportError If `import_name_or_path` name could not be detected as en extension. """ if is_absolute_path_name(import_name_or_path): if exists(import_name_or_path): if is_folder(import_name_or_path): yield from _iter_folder(None, import_name_or_path) return if is_file(import_name_or_path): yield None, import_name_or_path return else: path_end = join_paths(*import_name_or_path.split('.')) for base_path in route_paths: path = join_paths(base_path, path_end) if exists(path) and is_folder(path): yield from _iter_folder(import_name_or_path, path) return for python_extension_name in PYTHON_EXTENSION_NAMES: file_path = path + python_extension_name if exists(file_path) and is_file(file_path): yield import_name_or_path, file_path return raise ImportError( f'The given `import_name_or_path` could not be detected as an extension nor an absolute path, ' f'got {import_name_or_path!r}.')
def _iter_folder(import_name, folder_path): """ Iterates over a folder's import names. Parameters ---------- import_name : `None`, `str` The name of the extension if we would import it. folder_path : `str` Path to the folder Yields ------ import_name : `None`, `str` Detected import names for each applicable file in the folder. path : `str` Path of the file. """ for python_extension_name in PYTHON_EXTENSION_NAMES: file_path = join_paths(folder_path, f'__init__{python_extension_name}') if exists(file_path) and is_file(file_path): yield import_name, file_path return for file_name in list_directory(folder_path): if file_name.startswith('.') or (file_name == '__pycache__'): continue path = join_paths(folder_path, file_name) if is_file(path): for python_extension_name in PYTHON_EXTENSION_NAMES: if file_name.endswith(python_extension_name): if import_name is None: import_name_value = None else: import_name_value = f'{import_name}.{file_name[:-len(python_extension_name)]}' yield import_name_value, path break continue if is_folder(path): if import_name is None: import_name_value = None else: import_name_value = f'{import_name}.{file_name}' yield from _iter_folder(import_name_value, path) continue # no more cases continue
def _lookup_path(import_name): """ Detects the root of the given name. This function is an iterable generator. Parameters ---------- import_name : `str` An extension's import name. Yields ------ import_name : `str` Import name to an extension file. Raise ----- ImportError If `name` name could not be detected as en extension. """ path_end = join_path(*import_name.split('.')) for base_path in route_paths: path = join_path(base_path, path_end) if exists(path) and is_folder(path): yield from _iter_folder(import_name, path) return for python_extension_name in PYTHON_EXTENSION_NAMES: file_path = path + python_extension_name if exists(file_path) and is_file(file_path): yield import_name return raise TypeError( f'The given `import_name` could not be detected as an extension, got {import_name!r}.' )
EXTENSION_LOADER.load_extension('bots.satori', locked=True) EXTENSION_LOADER.load_extension('bots.flan', locked=True) EXTENSION_LOADER.load_extension('bots.nitori', locked=True) MODULE_NAMES = set() path = None for path in list_directory(join_path(PATH__KOISHI, 'bots', 'modules')): full_path = join_path(PATH__KOISHI, 'bots', 'modules', path) if is_file(full_path): if not path.endswith('.py'): continue path = path[:-3] elif is_folder(full_path): if path.startswith('_'): continue else: continue MODULE_NAMES.add(path) if MARISA_MODE: MARISA_ALLOWED_MODULES = set() if config.ALLOW_MARISA_SNEKBOX: MARISA_ALLOWED_MODULES.add('snekbox') MARISA_ALLOWED_MODULES.add('voice') MARISA_ALLOWED_MODULES.add('extensions')