def validate(self, validate_value=True, context=None): """Validates the parameter. This method validates if the parameter's schema is valid, and if the default value - if present - or the user-provided value for the parameter comply with the schema. """ err_msg = _("Parameter '%(name)s' is invalid: %(exp)s") try: self.schema.validate(context) if not validate_value: return if self.user_value is not None: self._validate(self.user_value, context) elif self.has_default(): self._validate(self.default(), context) else: raise exception.UserParameterMissing(key=self.name) except exception.StackValidationFailed as ex: msg = err_msg % dict(name=self.name, exp=six.text_type(ex)) raise exception.StackValidationFailed(message=msg) except exception.InvalidSchemaError as ex: msg = err_msg % dict(name=self.name, exp=six.text_type(ex)) raise exception.InvalidSchemaError(message=msg)
def result(self): param_name = function.resolve(self.args) try: return self.stack.parameters[param_name] except (KeyError, ValueError): raise exception.UserParameterMissing(key=param_name)
def value(self): '''Get the parameter value, optionally sanitising it for output.''' if self.user_value is not None: return self.user_value if self.has_default(): return self.default() raise exception.UserParameterMissing(key=self.name)
def result(self): args = function.resolve(self.args) if not args: raise ValueError( _('Function "%s" must have arguments') % self.fn_name) if isinstance(args, six.string_types): param_name = args path_components = [] elif isinstance(args, collections.Sequence): param_name = args[0] path_components = args[1:] else: raise TypeError( _('Argument to "%s" must be string or list') % self.fn_name) if not isinstance(param_name, six.string_types): raise TypeError( _('Parameter name in "%s" must be string') % self.fn_name) try: parameter = self.parameters[param_name] except KeyError: raise exception.UserParameterMissing(key=param_name) def get_path_component(collection, key): if not isinstance(collection, (collections.Mapping, collections.Sequence)): raise TypeError(_('"%s" can\'t traverse path') % self.fn_name) if not isinstance(key, (six.string_types, int)): raise TypeError( _('Path components in "%s" ' 'must be strings') % self.fn_name) if isinstance(collection, collections.Sequence) and isinstance( key, six.string_types): try: key = int(key) except ValueError: raise TypeError( _("Path components in '%s' " "must be a string that can be " "parsed into an " "integer.") % self.fn_name) return collection[key] try: return six.moves.reduce(get_path_component, path_components, parameter) except (KeyError, IndexError, TypeError): return ''
def handle_param_ref(args): try: if not isinstance(args, list): args = [args] parameter = params[args[0]] try: for inner_param in args[1:]: parameter = parameter[inner_param] return parameter except (KeyError, IndexError, TypeError): return '' except (KeyError, ValueError): raise exception.UserParameterMissing(key=args[0])
def __init__(self, name, schema, value=None, validate_value=True): ''' Initialise the Parameter with a name, schema and optional user-supplied value. ''' self.name = name self.schema = schema self.user_value = value if validate_value: if self.has_default(): self.validate(self.default()) if self.user_value is not None: self.validate(self.user_value) elif not self.has_default(): raise exception.UserParameterMissing(key=self.name)
def validate(self, validate_value=True, context=None): ''' Validates the parameter. This method validates if the parameter's schema is valid, and if the default value - if present - or the user-provided value for the parameter comply with the schema. ''' self.schema.validate(context) if validate_value: if self.has_default(): self._validate(self.default(), context) if self.user_value is not None: self._validate(self.user_value, context) elif not self.has_default(): raise exception.UserParameterMissing(key=self.name)
def result(self): args = function.resolve(self.args) if not args: raise ValueError( _('Function "%s" must have arguments') % self.fn_name) if isinstance(args, basestring): param_name = args path_components = [] elif isinstance(args, collections.Sequence): param_name = args[0] path_components = args[1:] else: raise TypeError( _('Argument to "%s" must be string or list') % self.fn_name) if not isinstance(param_name, basestring): raise TypeError( _('Parameter name in "%s" must be string') % self.fn_name) try: parameter = self.stack.parameters[param_name] except KeyError: raise exception.UserParameterMissing(key=param_name) def get_path_component(collection, key): if not isinstance(collection, (collections.Mapping, collections.Sequence)): raise TypeError(_('"%s" can\'t traverse path') % self.fn_name) if not isinstance(key, (basestring, int)): raise TypeError( _('Path components in "%s" ' 'must be strings') % self.fn_name) return collection[key] try: return reduce(get_path_component, path_components, parameter) except (KeyError, IndexError, TypeError): return ''
def handle_param_ref(ref): try: return parameters[ref] except (KeyError, ValueError): raise exception.UserParameterMissing(key=ref)
def value(self): if self.has_value(): return self.parsed raise exception.UserParameterMissing(key=self.name)
def get_input_key_value(fn_arg, inputs, check_input_val='LAX'): if check_input_val == 'STRICT' and fn_arg not in inputs: raise exception.UserParameterMissing(key=fn_arg) return inputs.get(fn_arg)