26 is_string, seq2str2, type_name, DotDict, Importer)
34 def set(self, path_or_variables, args=None, overwrite=False):
36 self.
_set_set(variables, overwrite)
41 return path_or_variables
42 LOGGER.info(
"Importing variable file '%s' with args %s"
43 % (path_or_variables, args))
44 if path_or_variables.lower().endswith(
'.yaml'):
49 return importer.import_variables(path_or_variables, args)
51 args =
'with arguments %s ' %
seq2str2(args)
if args
else ''
52 raise DataError(
"Processing variable file '%s' %sfailed: %s"
55 def _set(self, variables, overwrite=False):
56 for name, value
in variables:
57 self.
_store_store.add(name, value, overwrite)
64 raise DataError(
'YAML variable files do not accept arguments.')
65 variables = self.
_import_import(path)
66 return [(
'${%s}' % name, self.
_dot_dict_dot_dict(value))
67 for name, value
in variables]
70 with io.open(path, encoding=
'UTF-8')
as stream:
73 raise DataError(
'YAML variable file must be a mapping, got %s.'
75 return variables.items()
79 raise DataError(
'Using YAML variable files requires PyYAML module '
80 'to be installed. Typically you can install it '
81 'by running `pip install pyyaml`.')
82 if yaml.__version__.split(
'.')[0] ==
'3':
83 return yaml.load(stream)
84 return yaml.full_load(stream)
96 importer =
Importer(
'variable file').import_class_or_module_by_path
97 var_file = importer(path, instantiate_with_args=())
102 variables = self.
_get_dynamic_get_dynamic(var_file, args)
108 return (hasattr(var_file,
'get_variables')
or
109 hasattr(var_file,
'getVariables'))
112 get_variables = (getattr(var_file,
'get_variables',
None)
or
113 getattr(var_file,
'getVariables'))
114 variables = get_variables(*args)
116 return variables.items()
117 raise DataError(
"Expected '%s' to return dict-like value, got %s."
118 % (get_variables.__name__,
type_name(variables)))
121 names = [attr
for attr
in dir(var_file)
if not attr.startswith(
'_')]
122 if hasattr(var_file,
'__all__'):
123 names = [name
for name
in names
if name
in var_file.__all__]
124 variables = [(name, getattr(var_file, name))
for name
in names]
125 if not inspect.ismodule(var_file):
126 variables = [(n, v)
for n, v
in variables
if not callable(v)]
130 for name, value
in variables:
136 if name.startswith(
'LIST__'):
137 return '@{%s}' % name[6:]
138 if name.startswith(
'DICT__'):
139 return '&{%s}' % name[6:]
140 return '${%s}' % name
144 raise DataError(
"Invalid variable '%s': Expected list-like value, "
147 raise DataError(
"Invalid variable '%s': Expected dict-like value, "
Used when variable does not exist.
def _is_dynamic(self, var_file)
def _get_static(self, var_file)
def _decorate_and_validate(self, variables)
def _get_dynamic(self, var_file, args)
def _validate(self, name, value)
def _decorate(self, name)
def import_variables(self, path, args=None)
def _get_variables(self, var_file, args)
def _import_if_needed(self, path_or_variables, args=None)
def __init__(self, store)
def _set(self, variables, overwrite=False)
def set(self, path_or_variables, args=None, overwrite=False)
def import_variables(self, path, args=None)
def _dot_dict(self, value)
def _load_yaml(self, stream)
def get_error_message()
Returns error message of the last occurred exception.
def seq2str2(sequence)
Returns sequence in format [ item 1 | item 2 | ...