12 """An argparse.Argument parse slightly changed such
13 that it always prints an extended help message incase of a parsing error."""
16 """Method invoked when a parsing error occured.
17 Writes an extended help over the base ArgumentParser.
20 sys.stderr.write(
'error: %s\n' % message)
26 """Class that instances can be given to an argparse.ArgumentParser.add_argument as choices keyword argument.
28 The explicit choices stated during construction of this object are just suggestions but all other values are
33 """Test for correctness of the choices.
34 Always returns true since all choices should be valid not only the ones stated at construction of this object.
39 """Displays all explicit values and a final "..." to indicate more choices might be possible."""
46 """Displays all explicit values and a final "..." to indicate more choices might be possible."""
53 def find_file(file_path):
55 belle2_local_dir = os.environ.get(
"BELLE2_LOCAL_DIR",
None)
57 local_file_path = os.path.join(belle2_local_dir, file_path)
58 if os.path.exists(local_file_path)
and os.path.isfile(local_file_path):
59 return local_file_path
61 belle2_central_dir = os.environ.get(
"BELLE2_RELEASE_DIR",
None)
62 if belle2_central_dir:
63 central_file_path = os.path.join(belle2_central_dir, file_path)
64 if os.path.exists(central_file_path)
and os.path.isfile(central_file_path):
65 return central_file_path
67 if os.path.exists(file_path)
and os.path.isfile(file_path):
75 module_by_short_name={},
76 allow_function_import=False):
77 """Convenience adder function that can resolve addtional short hand module names from a dictionary"""
78 if isinstance(module, basf2.Module):
80 path.add_module(module)
82 elif isinstance(module, str):
84 if allow_function_import:
87 sys.path.append(os.getcwd())
89 py_module_name, function_name = module.rsplit(
".", 1)
92 py_module = importlib.import_module(py_module_name)
96 py_function = getattr(py_module, function_name)
100 if module
in module_by_short_name:
102 module = module_by_short_name[short_name]
105 extend_path(path, module, module_by_short_name)
108 path.add_module(module)
110 elif callable(module):
113 if issubclass(module, basf2.Module):
116 module_instance = module()
117 path.add_module(module_instance)
126 elif isinstance(module, collections.Iterable):
129 for module
in modules:
130 extend_path(path, module, module_by_short_name)
132 message_template =
"""
133 '%s of type %s is neither
135 * a module (python) class
138 * a short name resolveable from %s.'
139 * an iterable of the above (e.g. basf2.Path)
141 raise ValueError(message_template % (module,
143 module_by_short_name.keys()))
146 def get_module_param(module, name):
147 parameters = module.available_params()
148 for parameter
in parameters:
149 if name == parameter.name:
150 return parameter.values
152 raise AttributeError(
'%s module does not have a parameter named %s' % (module, name))