Spaces:
Running
Running
File size: 2,702 Bytes
51ba5d6 0f2d9f6 51ba5d6 0f2d9f6 51ba5d6 0f2d9f6 51ba5d6 0f2d9f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import os
import importlib
from os import path as osp
def scandir(dir_path, suffix=None, recursive=False, full_path=False):
"""Scan a directory to find the interested files.
Args:
dir_path (str): Path of the directory.
suffix (str | tuple(str), optional): File suffix that we are
interested in. Default: None.
recursive (bool, optional): If set to True, recursively scan the
directory. Default: False.
full_path (bool, optional): If set to True, include the dir_path.
Default: False.
Returns:
A generator for all the interested files with relative paths.
"""
if (suffix is not None) and not isinstance(suffix, (str, tuple)):
raise TypeError('"suffix" must be a string or tuple of strings')
root = dir_path
def _scandir(dir_path, suffix, recursive):
for entry in os.scandir(dir_path):
if not entry.name.startswith(".") and entry.is_file():
if full_path:
return_path = entry.path
else:
return_path = osp.relpath(entry.path, root)
if suffix is None:
yield return_path
elif return_path.endswith(suffix):
yield return_path
else:
if recursive:
yield from _scandir(entry.path, suffix=suffix, recursive=recursive)
else:
continue
return _scandir(dir_path, suffix=suffix, recursive=recursive)
def import_registered_modules(registration_folder="registrations"):
"""
Import all registered modules from the specified folder.
This function automatically scans all the files under the specified folder and imports all the required modules for registry.
Parameters:
registration_folder (str, optional): Path to the folder containing registration modules. Default is "registrations".
Returns:
list: List of imported modules.
"""
# print("\n")
registration_modules_folder = (
osp.dirname(osp.abspath(__file__)) + f"/{registration_folder}"
)
# print("registration_modules_folder = ", registration_modules_folder)
registration_modules_file_names = [
osp.splitext(osp.basename(v))[0]
for v in scandir(dir_path=registration_modules_folder)
]
# print("registration_modules_file_names = ", registration_modules_file_names)
imported_modules = [
importlib.import_module(f"{registration_folder}.{file_name}")
for file_name in registration_modules_file_names
]
# print("imported_modules = ", imported_modules)
# print("\n")
|