The condconfig submodule¶
Warning
The functions and methods listed in this section are implementation details of CondConfigParser. Do not use them in your programs if you rely on API stability, as they are likely to change in incompatible ways without prior notice.
RawConditionalConfig class for CondConfigParser.
This module defines the condconfigparser.RawConditionalConfig
class, which is the main application programming interface for using
CondConfigParser.
- class condconfigparser.condconfig.DummyContextManager(stream)[source]¶
Bases:
objectDo-nothing context manager.
When used in a
withstatement, instances of this class simply return the object that was passed to the constructor.- __firstlineno__ = 51¶
- __static_attributes__ = ('stream',)¶
- class condconfigparser.condconfig.RawConditionalConfig(input, extvars=())[source]¶
Bases:
objectClass for dealing with files conforming to CondConfigParser syntax.
This class provides the main part of the API for using CondConfigParser.
- __init__(input, extvars=())[source]¶
Initialize a
RawConditionalConfiginstance.- Parameters:
input (file-like or string) – input stream or string (“configuration file”)
extvars (sequence or set) – indicates the external variables
- Returns:
a
RawConditionalConfiginstance
Read the configuration from input and check that the variables used therein can all be computed, directly or indirectly from the variables whose names are listed in extvars.
- extvars¶
Names of variables defined externally, i.e., not in the configuration file
- _checkAssignments()[source]¶
Check that all variable assignments can be executed.
Check that all variable assignments can be executed, assuming that all variables whose names are listed in
extvarsare defined.If at least one variable assignment makes use of an undefined variable, raise
UndefinedVariablesInAssignment. Otherwise, return the set of all defined variables (well, their names).
- _checkPredicates(definedVars)[source]¶
Check that all predicates can be evaluated.
Raise
UndefinedVariablesInPredicateif, and only if at least one predicate uses an undefined variable.
- _checkUsesOfUndefinedVariables()[source]¶
Check that no variable assignment or predicate uses undefined variables.
Raise
UndefinedVariablesInAssignmentorUndefinedVariablesInPredicateappropriately, otherwise return the set of all defined variables (well, their names).
- computeVars(context)[source]¶
Perform all variable assignments.
- Parameters:
context (dict) – mapping giving the value to use for every external variable. More precisely, for each name of an external variable, it gives the value for initialization of this variable before starting to perform variable assignments.
- Returns:
a new dictionary giving the value of every variable, be it external or not.
- eval(context, *, flat=False)[source]¶
Compute the values of variables and evaluate predicates.
- Parameters:
context (dict) – mapping giving the value to use for every external variable. More precisely, for each name of an external variable, it gives the value for initialization of this variable before starting to perform variable assignments.
flat (bool) – if true, the second element of the return value will be a list of raw configuration lines; otherwise, it will be a list of lists of raw configuration lines, one for each section, starting with the default, unconditional section.
- Returns:
a
tupleof the form(variables, lines)where:variables is a new dictionary giving the value of every variable, be it external or not;
lines is a list as indicated above in the flat parameter description.
The following interactive session illustrates the effect of the flat argument:
>>> cfg = '''\ ... { var1 = (extvar1 == "foobar") # parentheses only for clarity ... var2 = var1 and "baz" in extvar2 } ... ... raw cfg default1 ... raw cfg default2 ... ... [ var2 or extvar1 == "quux" ] ... raw cfg a1 ... raw cfg a2 ... raw cfg a3 ... ... [ var1 ] ... raw cfg b1 ... raw cfg b2 ... ... [ not var1 ] ... raw cfg c1 ... raw cfg c2 ... ''' >>> config = RawConditionalConfig(cfg, extvars=("extvar1", "extvar2")) >>> context = {"extvar1": "quux", ... "extvar2": [12, "abc", [False, "def"]]} >>> [config.eval(context), config.eval(context, flat=True)] == \ ... [({'extvar1': 'quux', ... 'extvar2': [12, 'abc', [False, 'def']], ... 'var1': False, ... 'var2': False}, ... [['raw cfg default1', 'raw cfg default2'], ... ['raw cfg a1', 'raw cfg a2', 'raw cfg a3'], ... ['raw cfg c1', 'raw cfg c2']]), ... ({'extvar1': 'quux', ... 'extvar2': [12, 'abc', [False, 'def']], ... 'var1': False, ... 'var2': False}, ... ['raw cfg default1', ... 'raw cfg default2', ... 'raw cfg a1', ... 'raw cfg a2', ... 'raw cfg a3', ... 'raw cfg c1', ... 'raw cfg c2'])] True >>>
The default value for the flat parameter (
False) preserves as much information as possible, allowing user applications to implement things such as conditional sections overriding the default, unconditional section.
- __firstlineno__ = 68¶
- __static_attributes__ = ('_assignments', '_config', '_tree', 'extvars')¶