WIP: Migration to async_pymongo
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from importlib.util import module_from_spec, spec_from_file_location
|
||||
from os import getcwd, path, walk
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
# =================================================================================
|
||||
|
||||
@@ -17,11 +18,15 @@ def get_py_files(src):
|
||||
return py_files
|
||||
|
||||
|
||||
def dynamic_import(module_name, py_path):
|
||||
def dynamic_import(module_name: str, py_path: str):
|
||||
try:
|
||||
module_spec = spec_from_file_location(module_name, py_path)
|
||||
module = module_from_spec(module_spec) # type: ignore
|
||||
module_spec.loader.exec_module(module) # type: ignore
|
||||
if module_spec is None:
|
||||
raise RuntimeError(
|
||||
f"Module spec from module name {module_name} and path {py_path} is None"
|
||||
)
|
||||
module = module_from_spec(module_spec)
|
||||
module_spec.loader.exec_module(module)
|
||||
return module
|
||||
except SyntaxError:
|
||||
print(
|
||||
@@ -29,12 +34,12 @@ def dynamic_import(module_name, py_path):
|
||||
flush=True,
|
||||
)
|
||||
return
|
||||
except Exception as exp:
|
||||
print(f"Could not load extension {module_name} due to {exp}", flush=True)
|
||||
except Exception as exc:
|
||||
print(f"Could not load extension {module_name} due to {exc}", flush=True)
|
||||
return
|
||||
|
||||
|
||||
def dynamic_import_from_src(src, star_import=False):
|
||||
def dynamic_import_from_src(src: Union[str, Path], star_import=False):
|
||||
my_py_files = get_py_files(src)
|
||||
for py_file in my_py_files:
|
||||
module_name = Path(py_file).stem
|
||||
|
Reference in New Issue
Block a user