diff --git a/src/rougail/annotator/check.py b/src/rougail/annotator/check.py index 11074ff7..8ec0d6dd 100644 --- a/src/rougail/annotator/check.py +++ b/src/rougail/annotator/check.py @@ -1,10 +1,10 @@ """Annotate check """ -from importlib.machinery import SourceFileLoader from typing import List, Any from .variable import CONVERT_OPTION +from ..utils import load_modules from ..i18n import _ from ..error import DictConsistencyError @@ -21,8 +21,7 @@ class CheckAnnotator: not hasattr(objectspace.space.constraints, 'check'): return self.objectspace = objectspace - eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() - self.functions = dir(eosfunc) + self.functions = dir(load_modules(eosfunc_file)) self.functions.extend(INTERNAL_FUNCTIONS) self.check_check() self.check_valid_enum() diff --git a/src/rougail/annotator/fill.py b/src/rougail/annotator/fill.py index a11f15db..19de8db2 100644 --- a/src/rougail/annotator/fill.py +++ b/src/rougail/annotator/fill.py @@ -1,9 +1,7 @@ """Fill annotator """ -from importlib.machinery import SourceFileLoader - +from ..utils import load_modules from ..i18n import _ - from ..error import DictConsistencyError @@ -18,8 +16,7 @@ class FillAnnotator: if not hasattr(objectspace.space, 'constraints') or \ not hasattr(self.objectspace.space.constraints, 'fill'): return - eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() - self.functions = dir(eosfunc) + self.functions = dir(load_modules(eosfunc_file)) self.convert_fill() def convert_fill(self) -> None: diff --git a/src/rougail/template.py b/src/rougail/template.py index 99d4ea4d..3238ac3f 100644 --- a/src/rougail/template.py +++ b/src/rougail/template.py @@ -4,7 +4,6 @@ Gestion du mini-langage de template On travaille sur les fichiers cibles """ -from importlib.machinery import SourceFileLoader from shutil import copy import logging from typing import Dict, Any @@ -25,7 +24,7 @@ except ModuleNotFoundError: # pragma: no cover from .config import Config from .error import FileNotFound, TemplateError from .i18n import _ -from .utils import normalize_family +from .utils import normalize_family, load_modules log = logging.getLogger(__name__) @@ -214,7 +213,7 @@ class CreoleTemplateEngine: self.distrib_dir = distrib_dir eos = {} if eosfunc_file is not None: - eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() + eosfunc = load_modules(eosfunc_file) for func in dir(eosfunc): if not func.startswith('_'): eos[func] = getattr(eosfunc, func) diff --git a/src/rougail/utils.py b/src/rougail/utils.py index 0bb53d1e..9c70326a 100644 --- a/src/rougail/utils.py +++ b/src/rougail/utils.py @@ -1,8 +1,12 @@ """ utilitaires créole """ +from typing import List from unicodedata import normalize, combining +from importlib.machinery import SourceFileLoader +from importlib.util import spec_from_loader, module_from_spec + def normalize_family(family_name: str) -> str: """replace space, accent, uppercase, ... by valid character @@ -11,3 +15,13 @@ def normalize_family(family_name: str) -> str: nfkd_form = normalize('NFKD', family_name) family_name = ''.join([c for c in nfkd_form if not combining(c)]) return family_name.lower() + + +def load_modules(eosfunc_file) -> List[str]: + """list all functions in eosfunc + """ + loader = SourceFileLoader('eosfunc', eosfunc_file) + spec = spec_from_loader(loader.name, loader) + eosfunc = module_from_spec(spec) + loader.exec_module(eosfunc) + return eosfunc