IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 378be4d6 authored by Remy Prechelt's avatar Remy Prechelt
Browse files

Fix bug in loading of library from Python.

parent 95724831
No related branches found
No related tags found
1 merge request!317Output infrastructure and Python analysis library.
...@@ -11,7 +11,7 @@ import logging ...@@ -11,7 +11,7 @@ import logging
import os import os
import os.path as op import os.path as op
import re import re
from typing import Any, Dict, Optional from typing import Any, Dict, Optional, List
import yaml import yaml
...@@ -51,6 +51,20 @@ class Library(object): ...@@ -51,6 +51,20 @@ class Library(object):
# build the list of outputs # build the list of outputs
self.__outputs = self.__build_outputs(path) self.__outputs = self.__build_outputs(path)
@property
def names(self) -> List[str]:
"""
Return the list of registered outputs.
"""
return list(self.__outputs.keys())
@property
def modules(self) -> Dict[str, str]:
"""
Return the list of registered outputs.
"""
pass
def get(self, name: str) -> Optional[outputs.Output]: def get(self, name: str) -> Optional[outputs.Output]:
""" """
Return the output with a given name. Return the output with a given name.
...@@ -138,7 +152,7 @@ class Library(object): ...@@ -138,7 +152,7 @@ class Library(object):
_, dirs, _ = next(os.walk(path)) _, dirs, _ = next(os.walk(path))
# this is the dictionary where we store our components # this is the dictionary where we store our components
outputs: Dict[str, Any] = {} components: Dict[str, Any] = {}
# loop over the subdirectories # loop over the subdirectories
for subdir in dirs: for subdir in dirs:
...@@ -166,13 +180,10 @@ class Library(object): ...@@ -166,13 +180,10 @@ class Library(object):
try: try:
# create the name of the module containing this output class # create the name of the module containing this output class
module_name = re.sub(r"(?<!^)(?=[A-Z])", "_", out_type).lower() # module_name = re.sub(r"(?<!^)(?=[A-Z])", "_", out_type).lower()
# instantiate the output and store it in our dict # instantiate the output and store it in our dict
# we use a regex to go from CamelCase to snake_case component = getattr(outputs, out_type)(op.join(path, subdir))
component = getattr(getattr(outputs, module_name), out_type)(
op.join(path, subdir)
)
# check if the read failed # check if the read failed
if not component.is_good(): if not component.is_good():
...@@ -182,15 +193,16 @@ class Library(object): ...@@ -182,15 +193,16 @@ class Library(object):
) )
logging.getLogger("corsika").warn(msg) logging.getLogger("corsika").warn(msg)
else: else:
outputs[name] = component components[name] = component
except AttributeError: except AttributeError as e:
msg = ( msg = (
f"Unable to instantiate an instance of '{out_type}' " f"Unable to instantiate an instance of '{out_type}' "
"for a process called '{name}'" f"for a process called '{name}'"
) )
logging.getLogger("corsika").warn(msg) logging.getLogger("corsika").warn(msg)
logging.getLogger("corsika").warn(e)
continue # skip to the next output, don't error continue # skip to the next output, don't error
# and we are done building - return the constructed outputs # and we are done building - return the constructed outputs
return outputs return components
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment