def test_nested_map_delete(): """Tests if nested dictionaries items can be deleted""" deletable_map = copy(nested_map) with pytest.raises(KeyError): nested_map_find(nested_map_update(deletable_map, None, key), key)
def set_parameter(ctx, parameter, value): """Set a configuration parameter of an instance""" log("Setting %s to %s" % (parameter, value)) instance_configuration = ctx.obj["instance_configuration"] defaults = instance_template converted_value = None path = parameter.split(".") try: default = nested_map_find(defaults, path) parameter_type = type(default) log(parameter_type, pretty=True, lvl=debug) if parameter_type == tomlkit.items.Integer: converted_value = int(value) elif parameter_type == bool: converted_value = value.upper() == "TRUE" else: converted_value = value except KeyError: log("Available parameters:", sorted(list(defaults.keys()))) abort(EXIT_INVALID_PARAMETER) if converted_value is None: log("Converted value was None! Recheck the new config!", lvl=warn) nested_map_update(instance_configuration, converted_value, path) #instance_configuration[parameter] = converted_value log("New config:", instance_configuration, pretty=True, lvl=debug) ctx.obj["instances"][ctx.obj["instance"]] = instance_configuration if valid_configuration(ctx): write_instance(instance_configuration) finish(ctx) else: log("New configuration would not be valid", lvl=critical) abort(EXIT_INVALID_CONFIGURATION)
def _validate(self, schema_name, model, client_data): """Validates and tries to fix up to 10 errors in client model data..""" # TODO: This should probably move to Formal. # Also i don't like artificially limiting this. # Alas, never giving it up is even worse :) give_up = 10 validated = False while give_up > 0 and validated is False: try: validated = model(client_data) except ValidationError as e: self.log("Validation Error:", e, e.__dict__, pretty=True) give_up -= 1 if e.validator == "type": schema_data = schemastore[schema_name]["schema"] if e.validator_value == "number": definition = nested_map_find(schema_data, list(e.schema_path)[:-1]) if "default" in definition: client_data = nested_map_update( client_data, definition["default"], list(e.path)) else: client_data = nested_map_update( client_data, None, list(e.path)) if (e.validator == "pattern" and "uuid" == e.path[0] and client_data["uuid"] == "create"): client_data["uuid"] = std_uuid() if validated is False: raise ValidationError("Could not validate object") return client_data
def test_nested_map_update(): """Tests if nested dictionaries can be updated""" assert nested_map_find(nested_map_update(nested_map, 'Tulip', key), key) == 'Tulip'
def test_nested_map_find(): """Tests if nested dictionaries can be traversed""" assert nested_map_find(nested_map, key) == 'Jasmine'