From f7ea7c90b59fbc92c65e2b33c9764904553656fa Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 17 Nov 2024 11:30:37 +0100 Subject: [PATCH] Enforce loading of fetchers --- personal_data/fetchers/__init__.py | 11 ++++++++--- personal_data/main.py | 3 ++- test/test_init.py | 3 +++ 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 test/test_init.py diff --git a/personal_data/fetchers/__init__.py b/personal_data/fetchers/__init__.py index bdce392..255ebf2 100644 --- a/personal_data/fetchers/__init__.py +++ b/personal_data/fetchers/__init__.py @@ -21,11 +21,16 @@ def get_modules(backend_dir: Path) -> Iterator[str]: if name != '__init__': yield name +FETCHER_MODULES_LOADED = False + def load_fetcher_modules() -> None: - backend_dir = Path(__name__.replace(r'.', '/')) - for module in get_modules(backend_dir): - load_backend(module) + global FETCHER_MODULES_LOADED + if not FETCHER_MODULES_LOADED: + backend_dir = Path(__name__.replace(r'.', '/')) + for module in get_modules(backend_dir): + load_backend(module) + FETCHER_MODULES_LOADED = True load_fetcher_modules() diff --git a/personal_data/main.py b/personal_data/main.py index 5c623af..ce2fc04 100644 --- a/personal_data/main.py +++ b/personal_data/main.py @@ -7,7 +7,7 @@ from pathlib import Path import requests import requests_cache -from . import data, notification, util +from . import data, notification, util, fetchers logger = logging.getLogger(__name__) @@ -73,6 +73,7 @@ def get_session( def available_scrapers() -> list[type[data.Scraper]]: + fetchers.load_fetcher_modules() subclasses = [] class_queue = [data.Scraper] while class_queue: diff --git a/test/test_init.py b/test/test_init.py new file mode 100644 index 0000000..19b8563 --- /dev/null +++ b/test/test_init.py @@ -0,0 +1,3 @@ +def test_init(): + import personal_data + assert personal_data.__version__ is not None