LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
LSSTDataManagementBasePackage
configOverrides.py
Go to the documentation of this file.
1 # This file is part of pipe_base.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 """Module which defines ConfigOverrides class and related methods.
23 """
24 
25 __all__ = ["ConfigOverrides"]
26 
27 import ast
28 
29 import lsst.pex.exceptions as pexExceptions
30 from lsst.utils import doImport
31 
32 from enum import Enum
33 
34 OverrideTypes = Enum("OverrideTypes", "Value File Python Instrument")
35 
36 
38  """Defines a set of overrides to be applied to a task config.
39 
40  Overrides for task configuration need to be applied by activator when
41  creating task instances. This class represents an ordered set of such
42  overrides which activator receives from some source (e.g. command line
43  or some other configuration).
44 
45  Methods
46  ----------
47  addFileOverride(filename)
48  Add overrides from a specified file.
49  addValueOverride(field, value)
50  Add override for a specific field.
51  applyTo(config)
52  Apply all overrides to a `config` instance.
53 
54  Notes
55  -----
56  Serialization support for this class may be needed, will add later if
57  necessary.
58  """
59 
60  def __init__(self):
61  self._overrides = []
62 
63  def addFileOverride(self, filename):
64  """Add overrides from a specified file.
65 
66  Parameters
67  ----------
68  filename : str
69  Path to the override file.
70  """
71  self._overrides.append((OverrideTypes.File, filename))
72 
73  def addValueOverride(self, field, value):
74  """Add override for a specific field.
75 
76  This method is not very type-safe as it is designed to support
77  use cases where input is given as string, e.g. command line
78  activators. If `value` has a string type and setting of the field
79  fails with `TypeError` the we'll attempt `eval()` the value and
80  set the field with that value instead.
81 
82  Parameters
83  ----------
84  field : str
85  Fully-qualified field name.
86  value :
87  Value to be given to a filed.
88  """
89  self._overrides.append((OverrideTypes.Value, (field, value)))
90 
91  def addPythonOverride(self, python_snippet: str):
92  """Add Overrides by running a snippit of python code against a config.
93 
94  Parameters
95  ----------
96  python_snippet: str
97  A string which is valid python code to be executed. This is done with
98  config as the only local accessible value.
99  """
100  self._overrides.append((OverrideTypes.Python, python_snippet))
101 
102  def addInstrumentOverride(self, instrument: str, task_name: str):
103  """Apply any overrides that an instrument has for a task
104 
105  Parameters
106  ----------
107  instrument: str
108  A string containing the fully qualified name of an instrument from
109  which configs should be loaded and applied
110  task_name: str
111  The _DefaultName of a task associated with a config, used to look
112  up overrides from the instrument.
113  """
114  instrument_lib = doImport(instrument)()
115  self._overrides.append((OverrideTypes.Instrument, (instrument_lib, task_name)))
116 
117  def applyTo(self, config):
118  """Apply all overrides to a task configuration object.
119 
120  Parameters
121  ----------
122  config : `pex.Config`
123 
124  Raises
125  ------
126  `Exception` is raised if operations on configuration object fail.
127  """
128  for otype, override in self._overrides:
129  if otype is OverrideTypes.File:
130  config.load(override)
131  elif otype is OverrideTypes.Value:
132  field, value = override
133  field = field.split('.')
134  # find object with attribute to set, throws if we name is wrong
135  obj = config
136  for attr in field[:-1]:
137  obj = getattr(obj, attr)
138  # If input is a string and field type is not a string then we
139  # have to convert string to an expected type. Implementing
140  # full string parser is non-trivial so we take a shortcut here
141  # and `eval` the string and assign the resulting value to a
142  # field. Type erroes can happen during both `eval` and field
143  # assignment.
144  if isinstance(value, str) and obj._fields[field[-1]].dtype is not str:
145  try:
146  # use safer ast.literal_eval, it only supports literals
147  value = ast.literal_eval(value)
148  except Exception:
149  # eval failed, wrap exception with more user-friendly message
150  raise pexExceptions.RuntimeError(f"Unable to parse `{value}' into a Python object")
151 
152  # this can throw in case of type mismatch
153  setattr(obj, field[-1], value)
154  elif otype is OverrideTypes.Python:
155  exec(override, None, {"config": config})
156  elif otype is OverrideTypes.Instrument:
157  instrument, name = override
158  instrument.applyConfigOverrides(name, config)
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
def doImport(pythonType)
Definition: utils.py:106
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104