Exemplo n.º 1
0
 def parse(sq):
     r = keyword_list_parser(keyword, many=many)(sq)
     if r:
         if many:
             return filter(None, [[ah.enum_item_key(enum_cls, x) for x in y] for y in r]) or None
         return filter(None, [ah.enum_item_key(enum_cls, x) for x in r]) or None
     return None
Exemplo n.º 2
0
 def parse(sq):
     r = keyword_val_parser(keyword, many=many)(sq)
     if r:
         if many:
             return [ah.enum_item_key(enum_cls, x) for x in r]
         return ah.enum_item_key(enum_cls, r)
     return None
Exemplo n.º 3
0
    def convert(self, value, op):
        if eu.is_enum_type(self.type):
            # Fetch the `EnumItem` key for this `EnumItem` identifier
            converted_value = ah.enum_item_key(self.type, value) if value else None
        elif self.type is bool:
            # Convert truthy or falsey string values to a bool
            converted_value = vv.Boolean().to_python(value)
        else:
            # Otherwise, perform the default type conversion
            converted_value = super(Argument, self).convert(value, op)

        if isinstance(converted_value, (str, unicode)):
            if converted_value.strip() == '':
                converted_value = None

        # Raise an error if a required argument has no value post-conversion
        if self.required and converted_value is None:
            if isinstance(self.location, (str, unicode)):
                error_msg = u"Missing required parameter {0} in {1}".format(
                    self.name,
                    reqparse._friendly_location.get(self.location, self.location))
            else:
                friendly_locations = [reqparse._friendly_location.get(loc, loc) for loc in self.location]
                error_msg = u"Missing required parameter {0} in {1}".format(
                    self.name,
                    ' or '.join(friendly_locations))
            raise ValueError(error_msg)

        if hasattr(self.validator, '__call__'):
            self.validator(converted_value)

        return converted_value