lsst.pipe.base  16.0-22-g76b0903
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 lsst.pex.config as pexConfig
28 import lsst.pex.exceptions as pexExceptions
29 
30 
32  """Defines a set of overrides to be applied to a task config.
33 
34  Overrides for task configuration need to be applied by activator when
35  creating task instances. This class represents an ordered set of such
36  overrides which activator receives from some source (e.g. command line
37  or some other configuration).
38 
39  Methods
40  ----------
41  addFileOverride(filename)
42  Add overrides from a specified file.
43  addValueOverride(field, value)
44  Add override for a specific field.
45  applyTo(config)
46  Apply all overrides to a `config` instance.
47 
48  Notes
49  -----
50  Serialization support for this class may be needed, will add later if
51  necessary.
52  """
53 
54  def __init__(self):
55  self._overrides = []
56 
57  def addFileOverride(self, filename):
58  """Add overrides from a specified file.
59 
60  Parameters
61  ----------
62  filename : str
63  Path to the override file.
64  """
65  self._overrides += [('file', filename)]
66 
67  def addValueOverride(self, field, value):
68  """Add override for a specific field.
69 
70  This method is not very type-safe as it is designed to support
71  use cases where input is given as string, e.g. command line
72  activators. If `value` has a string type and setting of the field
73  fails with `TypeError` the we'll attempt `eval()` the value and
74  set the field with that value instead.
75 
76  Parameters
77  ----------
78  field : str
79  Fully-qualified field name.
80  value :
81  Value to be given to a filed.
82  """
83  self._overrides += [('value', (field, value))]
84 
85  def applyTo(self, config):
86  """Apply all overrides to a task configuration object.
87 
88  Parameters
89  ----------
90  config : `pex.Config`
91 
92  Raises
93  ------
94  `Exception` is raised if operations on configuration object fail.
95  """
96  for otype, override in self._overrides:
97  if otype == 'file':
98  config.load(override)
99  elif otype == 'value':
100  field, value = override
101  field = field.split('.')
102  # find object with attribute to set, throws if we name is wrong
103  obj = config
104  for attr in field[:-1]:
105  obj = getattr(obj, attr)
106  # If the type of the object to set is a list field, the value to assign
107  # is most likely a list, and we will eval it to get a python list object
108  # which will be used to set the objects value
109  # This must be done before the try, as it will otherwise set a string which
110  # is a valid iterable object when a list is the intended object
111  if isinstance(getattr(obj, field[-1]), pexConfig.listField.List) and isinstance(value, str):
112  try:
113  value = eval(value, {})
114  except Exception:
115  # Something weird happened here, try passing, and seeing if further
116  # code can handle this
117  raise pexExceptions.RuntimeError(f"Unable to parse {value} into a valid list")
118  try:
119  setattr(obj, field[-1], value)
120  except TypeError:
121  if not isinstance(value, str):
122  raise
123  # this can throw
124  value = eval(value, {})
125  setattr(obj, field[-1], value)