22 """Module which defines ConfigOverrides class and related methods.
25 __all__ = [
"ConfigOverrides"]
34 OverrideTypes = Enum(
"OverrideTypes",
"Value File Python Instrument")
38 """Defines a set of overrides to be applied to a task config.
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).
47 addFileOverride(filename)
48 Add overrides from a specified file.
49 addValueOverride(field, value)
50 Add override for a specific field.
52 Apply all overrides to a `config` instance.
56 Serialization support for this class may be needed, will add later if
64 """Add overrides from a specified file.
69 Path to the override file.
74 """Add override for a specific field.
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.
85 Fully-qualified field name.
87 Value to be given to a filed.
92 """Add Overrides by running a snippit of python code against a config.
97 A string which is valid python code to be executed. This is done with
98 config as the only local accessible value.
103 """Apply any overrides that an instrument has for a task
108 A string containing the fully qualified name of an instrument from
109 which configs should be loaded and applied
111 The _DefaultName of a task associated with a config, used to look
112 up overrides from the instrument.
114 instrument_lib =
doImport(instrument)()
115 self.
_overrides.
append((OverrideTypes.Instrument, (instrument_lib, task_name)))
118 """Apply all overrides to a task configuration object.
122 config : `pex.Config`
126 `Exception` is raised if operations on configuration object fail.
129 if otype
is OverrideTypes.File:
130 config.load(override)
131 elif otype
is OverrideTypes.Value:
132 field, value = override
133 field = field.split(
'.')
136 for attr
in field[:-1]:
137 obj = getattr(obj, attr)
144 if isinstance(value, str)
and obj._fields[field[-1]].dtype
is not str:
147 value = ast.literal_eval(value)
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)