Skip to content

Commit ce10c4b

Browse files
lazy loading import function
Signed-off-by: thiswillbeyourgithub <[email protected]>
1 parent 984a8d3 commit ce10c4b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

wdoc/utils/loaders/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
from .anki import load_anki
8484

8585
# Mapping of filetypes to their corresponding loader function names
86-
FILETYPE_TO_LOADER = {
86+
LOADABLE_FILETYPE = {
8787
"url": "load_url",
8888
"youtube": "load_youtube_video",
8989
"pdf": "load_pdf",
@@ -220,13 +220,13 @@ def load_one_doc(
220220
assert temp_dir.exists(), temp_dir
221221

222222
# Check if filetype is supported
223-
if filetype not in FILETYPE_TO_LOADER:
223+
if filetype not in LOADABLE_FILETYPE:
224224
logger.warning(f"Unsupported filetype: '{filetype}'")
225225
raise Exception(f"Unsupported filetype: '{filetype}'")
226226

227-
# Get the loader function name and retrieve the actual function
228-
loader_func_name = FILETYPE_TO_LOADER[filetype]
229-
loader_func = locals().get(loader_func_name) or globals().get(loader_func_name)
227+
# Lazy loading the document loader function
228+
exec(f"from .{filetype} import load_{filetype}")
229+
loader_func = locals()[f"load_{filetype}"] or globals()[f"load_{filetype}"] or None
230230

231231
if loader_func is None:
232232
raise Exception(
@@ -2205,9 +2205,9 @@ def dl_audio_from_url(trial: int, url: str) -> Path:
22052205

22062206
# Validation: Check that all loader functions exist
22072207
def _validate_loader_functions():
2208-
"""Validate that all loader functions referenced in FILETYPE_TO_LOADER exist."""
2208+
"""Validate that all loader functions referenced in LOADABLE_FILETYPE exist."""
22092209
current_module = sys.modules[__name__]
2210-
for filetype, func_name in FILETYPE_TO_LOADER.items():
2210+
for filetype, func_name in LOADABLE_FILETYPE.items():
22112211
if not hasattr(current_module, func_name):
22122212
raise Exception(
22132213
f"Loader function '{func_name}' for filetype '{filetype}' not found in module"

0 commit comments

Comments
 (0)