From e249049fc15e4b974766224fc851eb5011107a6d Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Mon, 18 Dec 2017 09:26:22 -0500 Subject: [PATCH] util: Move module loader functions to a new dynload module. --- CODE.rst | 2 +- import2ledger/dynload.py | 30 +++++++++++++++++++++++++++++ import2ledger/hooks/__init__.py | 4 ++-- import2ledger/importers/__init__.py | 4 ++-- import2ledger/util.py | 30 ----------------------------- 5 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 import2ledger/dynload.py diff --git a/CODE.rst b/CODE.rst index a660b0d..3100a22 100644 --- a/CODE.rst +++ b/CODE.rst @@ -73,7 +73,7 @@ import2ledger finds importers by looking at all ``.py`` files in the ``importers Hooks follow the same pattern, searching the ``hooks/`` directory and looking for things named ``*Hook``. -Technically this is done by ``importers.load_all()`` and ``hooks.load_all()`` functions, but most of the code to do this is in the ``util`` module. +Technically this is done by ``importers.load_all()`` and ``hooks.load_all()`` functions, but most of the code to do this is in the ``dynload`` module. Main loop --------- diff --git a/import2ledger/dynload.py b/import2ledger/dynload.py new file mode 100644 index 0000000..ca0622d --- /dev/null +++ b/import2ledger/dynload.py @@ -0,0 +1,30 @@ +import importlib +import logging +import pathlib + +logger = logging.getLogger('import2ledger') + +def load_modules(src_dir_path): + rel_path = src_dir_path.relative_to(pathlib.Path(__file__).parent) + import_prefix = 'import2ledger.{}.'.format('.'.join(rel_path.parts)) + for py_path in src_dir_path.glob('*.py'): + mod_name = py_path.name[:-3] + if mod_name.startswith(('.', '_')): + continue + try: + module = importlib.import_module(import_prefix + mod_name) + except ImportError as error: + logger.info("failed to import %s: %s", py_path, error, + exc_info=logger.isEnabledFor(logging.DEBUG)) + else: + yield module + +def module_contents(module): + for name in dir(module): + yield name, getattr(module, name) + +def submodule_items_named(file_path, name_test): + for module in load_modules(pathlib.Path(file_path).parent): + for name, item in module_contents(module): + if name_test(name): + yield item diff --git a/import2ledger/hooks/__init__.py b/import2ledger/hooks/__init__.py index c8d6a9a..8cfa5cb 100644 --- a/import2ledger/hooks/__init__.py +++ b/import2ledger/hooks/__init__.py @@ -1,6 +1,6 @@ import operator -from .. import util +from .. import dynload def load_all(): - return util.submodule_items_named(__file__, operator.methodcaller('endswith', 'Hook')) + return dynload.submodule_items_named(__file__, operator.methodcaller('endswith', 'Hook')) diff --git a/import2ledger/importers/__init__.py b/import2ledger/importers/__init__.py index b540e6d..68e49ac 100644 --- a/import2ledger/importers/__init__.py +++ b/import2ledger/importers/__init__.py @@ -1,6 +1,6 @@ import operator -from .. import util +from .. import dynload def load_all(): - return util.submodule_items_named(__file__, operator.methodcaller('endswith', 'Importer')) + return dynload.submodule_items_named(__file__, operator.methodcaller('endswith', 'Importer')) diff --git a/import2ledger/util.py b/import2ledger/util.py index 2c4d7de..2a859be 100644 --- a/import2ledger/util.py +++ b/import2ledger/util.py @@ -1,34 +1,4 @@ import datetime -import importlib -import logging -import pathlib - -logger = logging.getLogger('import2ledger') - -def load_modules(src_dir_path): - rel_path = src_dir_path.relative_to(pathlib.Path(__file__).parent) - import_prefix = 'import2ledger.{}.'.format('.'.join(rel_path.parts)) - for py_path in src_dir_path.glob('*.py'): - mod_name = py_path.name[:-3] - if mod_name.startswith(('.', '_')): - continue - try: - module = importlib.import_module(import_prefix + mod_name) - except ImportError as error: - logger.info("failed to import %s: %s", py_path, error, - exc_info=logger.isEnabledFor(logging.DEBUG)) - else: - yield module - -def module_contents(module): - for name in dir(module): - yield name, getattr(module, name) - -def submodule_items_named(file_path, name_test): - for module in load_modules(pathlib.Path(file_path).parent): - for name, item in module_contents(module): - if name_test(name): - yield item def strpdate(date_s, date_fmt): return datetime.datetime.strptime(date_s, date_fmt).date()