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

View file

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

View file

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

View file

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