util: Move module loader functions to a new dynload module.

This commit is contained in:
Brett Smith 2017-12-18 09:26:22 -05:00
parent f56571219b
commit e249049fc1
5 changed files with 35 additions and 35 deletions

View file

@ -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
---------

30
import2ledger/dynload.py Normal file
View file

@ -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

View file

@ -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'))

View file

@ -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'))

View file

@ -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()