25 from robot.utils import (get_error_message, is_dict_like, is_list_like,
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',
'.yml')):
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)
90 return [self.
_dot_dict_dot_dict(v)
for v
in value]
97 importer =
Importer(
'variable file', LOGGER).import_class_or_module
98 var_file = importer(path, instantiate_with_args=())
103 variables = self.
_get_dynamic_get_dynamic(var_file, args)
109 return (hasattr(var_file,
'get_variables')
or
110 hasattr(var_file,
'getVariables'))
113 get_variables = (getattr(var_file,
'get_variables',
None)
or
114 getattr(var_file,
'getVariables'))
115 variables = get_variables(*args)
117 return variables.items()
118 raise DataError(
"Expected '%s' to return dict-like value, got %s."
119 % (get_variables.__name__,
type_name(variables)))
122 names = [attr
for attr
in dir(var_file)
if not attr.startswith(
'_')]
123 if hasattr(var_file,
'__all__'):
124 names = [name
for name
in names
if name
in var_file.__all__]
125 variables = [(name, getattr(var_file, name))
for name
in names]
126 if not inspect.ismodule(var_file):
127 variables = [(n, v)
for n, v
in variables
if not callable(v)]
131 for name, value
in variables:
137 if name.startswith(
'LIST__'):
138 return '@{%s}' % name[6:]
139 if name.startswith(
'DICT__'):
140 return '&{%s}' % name[6:]
141 return '${%s}' % name
145 raise DataError(
"Invalid variable '%s': Expected list-like value, "
148 raise DataError(
"Invalid variable '%s': Expected dict-like value, "
Utility that can import modules and classes based on names and paths.
def import_variables(self, path, args=None)
def _decorate_and_validate(self, variables)
def _get_dynamic(self, var_file, args)
def _validate(self, name, value)
def _is_dynamic(self, var_file)
def _get_variables(self, var_file, args)
def _get_static(self, var_file)
def _decorate(self, name)
def __init__(self, store)
def set(self, path_or_variables, args=None, overwrite=False)
def _set(self, variables, overwrite=False)
def _import_if_needed(self, path_or_variables, args=None)
def _load_yaml(self, stream)
def _dot_dict(self, value)
def import_variables(self, path, args=None)
def get_error_message()
Returns error message of the last occurred exception.
def seq2str2(sequence)
Returns sequence in format [ item 1 | item 2 | ...
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.