Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _map(self, data, spec, context, path):
child_context = context.create_child_context()
if isinstance(spec, dsl_types.YaqlExpression):
child_context[''] = data
try:
result = spec(context=child_context)
return result
except exceptions.ContractViolationException as e:
e.path = path
raise
elif isinstance(spec, utils.MappingType):
return self._map_dict(data, spec, child_context, path)
elif utils.is_sequence(spec):
return self._map_list(data, spec, child_context, path)
else:
return self._map_scalar(data, spec)
def _merge_dicts(dict1, dict2, list_merge_func, item_merger, max_levels=0):
result = {}
for key, value1 in six.iteritems(dict1):
result[key] = value1
if key in dict2:
value2 = dict2[key]
if max_levels != 1 and isinstance(value2, utils.MappingType):
if not isinstance(value1, utils.MappingType):
raise TypeError(
'Cannot merge {0} with {1}'.format(
type(value1), type(value2)))
result[key] = _merge_dicts(
value1, value2, list_merge_func, item_merger,
0 if max_levels == 0 else max_levels - 1)
elif max_levels != 1 and utils.is_sequence(value2):
if not utils.is_sequence(value1):
raise TypeError(
'Cannot merge {0} with {1}'.format(
type(value1), type(value2)))
result[key] = list_merge_func(value1, value2)
else:
result[key] = item_merger(value1, value2)
for key2, value2 in six.iteritems(dict2):
if key2 not in result:
result[key2] = value2
return result
def _map(self, data, spec, context, path):
if helpers.is_passkey(data):
return data
child_context = context.create_child_context()
if isinstance(spec, dsl_types.YaqlExpression):
child_context[''] = data
try:
result = spec(context=child_context)
return result
except exceptions.ContractViolationException as e:
e.path = path
raise
elif isinstance(spec, utils.MappingType):
return self._map_dict(data, spec, child_context, path)
elif utils.is_sequence(spec):
return self._map_list(data, spec, child_context, path)
else:
return self._map_scalar(data, spec)
def evaluate(value, context, freeze=True):
list_type = tuple if freeze else list
dict_type = yaqlutils.FrozenDict if freeze else dict
set_type = frozenset if freeze else set
if isinstance(value, (dsl_types.YaqlExpression,
yaql.language.expressions.Statement)):
return value(context)
elif isinstance(value, yaqlutils.MappingType):
return dict_type(
(evaluate(d_key, context, freeze),
evaluate(d_value, context, freeze))
for d_key, d_value in value.items())
elif yaqlutils.is_sequence(value):
return list_type(evaluate(t, context, freeze) for t in value)
elif isinstance(value, yaqlutils.SetType):
return set_type(evaluate(t, context, freeze) for t in value)
elif yaqlutils.is_iterable(value):
return list_type(
evaluate(t, context, freeze)
for t in yaqlutils.limit_iterable(
value, CONF.murano.dsl_iterators_limit))
elif isinstance(value, dsl_types.MuranoObjectInterface):
return value.object
else:
return value
def setter(src_property, value):
src = src_property.get()
if utils.is_sequence(src):
src_property.set(src[:index] + (value,) + src[index + 1:])
elif isinstance(src, utils.MappingType):
attribution(context, src_property, index).set(value)
def _map_list(self, data, spec, context, path):
if utils.is_iterator(data):
data = list(data)
elif not utils.is_sequence(data):
if data is None or data is dsl.NO_VALUE:
data = []
else:
data = [data]
if len(spec) < 1:
return data
shift = 0
max_length = -1
min_length = 0
if isinstance(spec[-1], int):
min_length = spec[-1]
shift += 1
if len(spec) >= 2 and isinstance(spec[-2], int):
max_length = min_length
min_length = spec[-2]
shift += 1
def getter(src):
if utils.is_sequence(src):
return src[index]
else:
raise ValueError('indexation may only be applied to lists')
def _create_class(cls, name, ns_resolver, data, package, *args, **kwargs):
parent_class_names = data.get('Extends')
parent_classes = []
if parent_class_names:
if not utils.is_sequence(parent_class_names):
parent_class_names = [parent_class_names]
for parent_name in parent_class_names:
full_name = ns_resolver.resolve_name(str(parent_name))
parent_classes.append(package.find_class(full_name))
type_obj = cls(
ns_resolver, name, package, parent_classes, data.get('Meta'),
data.get('Import'), *args, **kwargs)
properties = data.get('Properties') or {}
for property_name, property_spec in properties.items():
spec = murano_property.MuranoProperty(
type_obj, property_name, property_spec)
type_obj.add_property(spec)
methods = data.get('Methods') or data.get('Workflow') or {}