Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/reportengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from reportengine.configparser import ConfigError, Config
from reportengine.environment import Environment, EnvironmentError_
from reportengine.baseexceptions import ErrorWithAlternatives
from reportengine.utils import get_providers
from reportengine.utils import get_providers, import_path
from reportengine import colors
from reportengine import helputils

Expand Down Expand Up @@ -175,8 +175,7 @@ def argparser(self):
default=('png', 'pdf',))

parser.add_argument('-x', '--extra-providers', nargs='+',
help="additional providers from which to "
"load actions. Must be an importable specifiaction.")
help="Python files to load additional providers from")

parallel = parser.add_mutually_exclusive_group()
parallel.add_argument('--parallel', action='store_true',
Expand All @@ -190,10 +189,20 @@ def argparser(self):
return parser

def init_providers(self, args):
extra_providers = args['extra_providers']
if extra_providers is None:
extra_providers = []
extra_provider_names = args['extra_providers']
try:
extra_providers = [import_path(p) for p in extra_provider_names]
except FileNotFoundError as e:
log.error(f"Could not import extra provider: No such file '{e}'.")
sys.exit(1)
except BaseException as e:
log.error("Error importing extra provider")

print(colors.color_exception(type(e), e, e.__traceback__), file=sys.stderr)
sys.exit(1)

maybe_names = reversed(self.default_providers + extra_providers)

providers = self.load_providers(maybe_names)
self.providers = providers

Expand All @@ -207,6 +216,7 @@ def load_providers(self, maybe_names=None):
mod = importlib.import_module(mod)
except ImportError as e:
log.error("Could not import module %s", mod)
#This *should* be a critical error.
raise
providers.append(mod)
return providers
Expand Down
16 changes: 15 additions & 1 deletion src/reportengine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import collections
import pickle
import inspect
import pathlib
import re
import importlib

def normalize_name(name):
"""Remove characters not suitable for filenames from the string"""
Expand Down Expand Up @@ -87,4 +89,16 @@ def ordinal(n):
suffix = 'th'
else:
suffix = ('st', 'nd', 'rd')[residual - 1]
return '%d%s' % (n,suffix)
return '%d%s' % (n,suffix)

def import_path(file_path):
"""Load the module corresponding to a given path"""
#https://docs.python.org/3/library/importlib.html?highlight=import_module#importing-a-source-file-directly
file_path = pathlib.Path(file_path)
if not file_path.exists():
raise FileNotFoundError(file_path)
module_name = file_path.stem
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module