20 """An argparse.Argument parse slightly changed such
21 that it always prints an extended help message increase of a parsing error."""
24 """Method invoked when a parsing error occurred.
25 Writes an extended help over the base ArgumentParser.
28 sys.stderr.write(f'error: {message}\n')
34 """Class that instances can be given to an argparse.ArgumentParser.add_argument as choices keyword argument.
36 The explicit choices stated during construction of this object are just suggestions but all other values are
41 """Test for correctness of the choices.
42 Always returns true since all choices should be valid not only the ones stated at construction of this object.
47 """Displays all explicit values and a final "..." to indicate more choices might be possible."""
54 """Displays all explicit values and a final "..." to indicate more choices might be possible."""
61def find_file(file_path):
63 belle2_local_dir = os.environ.get(
"BELLE2_LOCAL_DIR",
None)
65 local_file_path = os.path.join(belle2_local_dir, file_path)
66 if os.path.exists(local_file_path)
and os.path.isfile(local_file_path):
67 return local_file_path
69 belle2_central_dir = os.environ.get(
"BELLE2_RELEASE_DIR",
None)
70 if belle2_central_dir:
71 central_file_path = os.path.join(belle2_central_dir, file_path)
72 if os.path.exists(central_file_path)
and os.path.isfile(central_file_path):
73 return central_file_path
75 if os.path.exists(file_path)
and os.path.isfile(file_path):
83 module_by_short_name={},
84 allow_function_import=False):
85 """Convenience adder function that can resolve additional short hand module names from a dictionary"""
86 if isinstance(module, basf2.Module):
88 path.add_module(module)
90 elif isinstance(module, str):
92 if allow_function_import:
95 sys.path.append(os.getcwd())
97 py_module_name, function_name = module.rsplit(
".", 1)
100 py_module = importlib.import_module(py_module_name)
104 py_function = getattr(py_module, function_name)
108 if module
in module_by_short_name:
110 module = module_by_short_name[short_name]
113 extend_path(path, module, module_by_short_name)
116 path.add_module(module)
118 elif callable(module):
121 if issubclass(module, basf2.Module):
124 module_instance = module()
125 path.add_module(module_instance)
134 elif isinstance(module, collections.abc.Iterable):
137 for module
in modules:
138 extend_path(path, module, module_by_short_name)
140 message_template =
"""
141'%s of type %s is neither
143* a module (python) class
146* a short name resolvable from %s.'
147* an iterable of the above (e.g. basf2.Path)
149 raise ValueError(message_template % (module,
151 module_by_short_name.keys()))
154def get_module_param(module, name):
155 parameters = module.available_params()
156 for parameter
in parameters:
157 if name == parameter.name:
158 return parameter.values
160 raise AttributeError(f
'{module} module does not have a parameter named {name}')
def __contains__(self, value)