LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.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
98  with 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
150  # message
151  raise pexExceptions.RuntimeError(f"Unable to parse `{value}' into a Python object")
152 
153  # this can throw in case of type mismatch
154  setattr(obj, field[-1], value)
155  elif otype is OverrideTypes.Python:
156  exec(override, None, {"config": config})
157  elif otype is OverrideTypes.Instrument:
158  instrument, name = override
159  instrument.applyConfigOverrides(name, config)
lsst.pipe.base.configOverrides.ConfigOverrides
Definition: configOverrides.py:37
lsst.pipe.base.configOverrides.ConfigOverrides.addInstrumentOverride
def addInstrumentOverride(self, str instrument, str task_name)
Definition: configOverrides.py:102
ast::append
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
lsst.pipe.base.configOverrides.ConfigOverrides.applyTo
def applyTo(self, config)
Definition: configOverrides.py:117
lsst.pex.config.config.doImport
doImport
Definition: config.py:48
lsst::utils
Definition: Backtrace.h:29
lsst.pipe.base.configOverrides.ConfigOverrides.addValueOverride
def addValueOverride(self, field, value)
Definition: configOverrides.py:73
lsst.pipe.base.configOverrides.ConfigOverrides._overrides
_overrides
Definition: configOverrides.py:61
lsst.pipe.base.configOverrides.ConfigOverrides.__init__
def __init__(self)
Definition: configOverrides.py:60
lsst.pipe.base.configOverrides.ConfigOverrides.addPythonOverride
def addPythonOverride(self, str python_snippet)
Definition: configOverrides.py:91
lsst.pex::exceptions
Definition: Exception.h:37
lsst.pex::exceptions::RuntimeError
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
lsst.pipe.base.configOverrides.ConfigOverrides.addFileOverride
def addFileOverride(self, filename)
Definition: configOverrides.py:63