From 686a218ed03a86f9e430be0b82c14bba3890d6e2 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 19 Jan 2021 19:05:07 +0100 Subject: [PATCH] mutualise load_modules --- src/rougail/annotator/check.py | 5 ++--- src/rougail/annotator/fill.py | 7 ++----- src/rougail/template.py | 5 ++--- src/rougail/utils.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/rougail/annotator/check.py b/src/rougail/annotator/check.py index 11074ff7c..8ec0d6dde 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 a11f15db7..19de8db2d 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 99d4ea4d8..3238ac3f9 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 0bb53d1ea..9c70326a7 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