mutualise load_modules

This commit is contained in:
Emmanuel Garette 2021-01-19 19:05:07 +01:00
parent 0497698203
commit 686a218ed0
4 changed files with 20 additions and 11 deletions

View File

@ -1,10 +1,10 @@
"""Annotate check """Annotate check
""" """
from importlib.machinery import SourceFileLoader
from typing import List, Any from typing import List, Any
from .variable import CONVERT_OPTION from .variable import CONVERT_OPTION
from ..utils import load_modules
from ..i18n import _ from ..i18n import _
from ..error import DictConsistencyError from ..error import DictConsistencyError
@ -21,8 +21,7 @@ class CheckAnnotator:
not hasattr(objectspace.space.constraints, 'check'): not hasattr(objectspace.space.constraints, 'check'):
return return
self.objectspace = objectspace self.objectspace = objectspace
eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() self.functions = dir(load_modules(eosfunc_file))
self.functions = dir(eosfunc)
self.functions.extend(INTERNAL_FUNCTIONS) self.functions.extend(INTERNAL_FUNCTIONS)
self.check_check() self.check_check()
self.check_valid_enum() self.check_valid_enum()

View File

@ -1,9 +1,7 @@
"""Fill annotator """Fill annotator
""" """
from importlib.machinery import SourceFileLoader from ..utils import load_modules
from ..i18n import _ from ..i18n import _
from ..error import DictConsistencyError from ..error import DictConsistencyError
@ -18,8 +16,7 @@ class FillAnnotator:
if not hasattr(objectspace.space, 'constraints') or \ if not hasattr(objectspace.space, 'constraints') or \
not hasattr(self.objectspace.space.constraints, 'fill'): not hasattr(self.objectspace.space.constraints, 'fill'):
return return
eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() self.functions = dir(load_modules(eosfunc_file))
self.functions = dir(eosfunc)
self.convert_fill() self.convert_fill()
def convert_fill(self) -> None: def convert_fill(self) -> None:

View File

@ -4,7 +4,6 @@ Gestion du mini-langage de template
On travaille sur les fichiers cibles On travaille sur les fichiers cibles
""" """
from importlib.machinery import SourceFileLoader
from shutil import copy from shutil import copy
import logging import logging
from typing import Dict, Any from typing import Dict, Any
@ -25,7 +24,7 @@ except ModuleNotFoundError: # pragma: no cover
from .config import Config from .config import Config
from .error import FileNotFound, TemplateError from .error import FileNotFound, TemplateError
from .i18n import _ from .i18n import _
from .utils import normalize_family from .utils import normalize_family, load_modules
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -214,7 +213,7 @@ class CreoleTemplateEngine:
self.distrib_dir = distrib_dir self.distrib_dir = distrib_dir
eos = {} eos = {}
if eosfunc_file is not None: if eosfunc_file is not None:
eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module() eosfunc = load_modules(eosfunc_file)
for func in dir(eosfunc): for func in dir(eosfunc):
if not func.startswith('_'): if not func.startswith('_'):
eos[func] = getattr(eosfunc, func) eos[func] = getattr(eosfunc, func)

View File

@ -1,8 +1,12 @@
""" """
utilitaires créole utilitaires créole
""" """
from typing import List
from unicodedata import normalize, combining 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: def normalize_family(family_name: str) -> str:
"""replace space, accent, uppercase, ... by valid character """replace space, accent, uppercase, ... by valid character
@ -11,3 +15,13 @@ def normalize_family(family_name: str) -> str:
nfkd_form = normalize('NFKD', family_name) nfkd_form = normalize('NFKD', family_name)
family_name = ''.join([c for c in nfkd_form if not combining(c)]) family_name = ''.join([c for c in nfkd_form if not combining(c)])
return family_name.lower() 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