reorganize annotator loader

This commit is contained in:
egarette@silique.fr 2023-01-07 16:33:06 +01:00
parent 4765744953
commit b8e343c31d

View file

@ -58,9 +58,11 @@ def get_annotators(annotators, module_name):
annotators[module_name] = []
for pathobj in importlib.resources.files(module_name).iterdir():
path = str(pathobj)
if not path.endswith('__') and not path.endswith('__.py'):
if path.endswith('__') or path.endswith('__.py'):
continue
module = load_modules(path)
if 'Annotator' in dir(module):
if 'Annotator' not in dir(module):
continue
annotators[module_name].append(module.Annotator)
@ -69,25 +71,26 @@ class SpaceAnnotator: # pylint: disable=R0903
"""
def __init__(self,
objectspace,
eosfunc_files,
):
global ANNOTATORS
if ANNOTATORS is None:
ANNOTATORS = {}
get_annotators(ANNOTATORS, 'rougail.annotator')
for extra_annotator in objectspace.rougailconfig['extra_annotators']:
get_annotators(ANNOTATORS, extra_annotator)
for extra_annotator in objectspace.rougailconfig['extra_annotators']:
if extra_annotator not in ANNOTATORS:
if extra_annotator in ANNOTATORS:
continue
get_annotators(ANNOTATORS, extra_annotator)
annotators = ANNOTATORS['rougail.annotator'].copy()
for extra_annotator in objectspace.rougailconfig['extra_annotators']:
annotators.extend(ANNOTATORS[extra_annotator])
annotators = sorted(annotators, key=get_level)
functions = []
for eosfunc_file in eosfunc_files:
if isfile(eosfunc_file):
functions.extend(dir(load_modules(eosfunc_file)))
functions_files = objectspace.rougailconfig['functions_file']
if not isinstance(functions_files, list):
functions_files = [functions_files]
for functions_file in functions_files:
if isfile(functions_file):
functions.extend(dir(load_modules(functions_file)))
for annotator in annotators:
annotator(objectspace,
functions,