Ejemplo n.º 1
0
 def validate(self, value):
     original_value = value
     # So isinstance(True, int) -> True.
     # For a float parameter we should not allow a bool.
     if isinstance(value, bool):
         raise ValidationError(value=str(value), type_name='float',
                               param=self)
     elif not isinstance(value, float):
         # If the value is a float, that's good enough for a float
         # param.  Also you can't go directly from a float -> Decimal
         # in python2.6.
         # Otherwise the value has to look like a decimal,
         # so we just need to validate that it converts to a
         # decimal without issue and then run it through the min
         # max range validations.
         try:
             # We don't want to type convert here, but we need
             # to convert it to something we can use < and > against.
             value = decimal.Decimal(value)
         except (decimal.InvalidOperation, TypeError):
             raise ValidationError(value=str(value), type_name='float',
                                   param=self)
     if self.min:
         if value < self.min:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     if self.max:
         if value > self.max:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     return original_value
Ejemplo n.º 2
0
 def validate(self, value):
     allowed_types = six.string_types + (
         bytes,
         bytearray,
     )
     if self.payload and self.streaming:
         # Streaming blobs should be file-like objects or be strings.
         if not hasattr(value, 'read') and not isinstance(
                 value, allowed_types):
             raise ValidationError(value=str(value),
                                   type_name='blob',
                                   param=self)
     else:
         if not isinstance(value, allowed_types):
             raise ValidationError(value=str(value),
                                   type_name='string',
                                   param=self)
         if not hasattr(self, 'payload') or self.payload is False:
             # Blobs that are not in the payload should be base64-encoded
             if isinstance(value, six.text_type):
                 v = value.encode('utf-8')
             else:
                 v = value
             value = base64.b64encode(v).decode('utf-8')
     return value
Ejemplo n.º 3
0
 def identify_faceid(self,identity:str, face_id:str)->dict:
   if identity == None:
     raise ValidationError('identity')
   if face_id == None:
     raise ValidationError('face_id')
   
   self.dynamodb.put_item(
     TableName=self.face_table_name,
     Item={
       'PartitionKey': {'S': 'Identity::'+identity.lower()},
       'SortKey': {'S': face_id.lower()},
     })
Ejemplo n.º 4
0
 def validate(self, value):
     if not isinstance(value, (list, tuple)):
         raise ValidationError(value=value, type_name='list', param=self)
     for item in value:
         try:
             self.members.validate(item)
         except ValidationError as e:
             # ValidationError must provide a value
             # argument so we can safely access that key.
             raise ValidationError(value=e.kwargs['value'],
                                   param='element of %s' % self,
                                   type_name='list')
     return value
Ejemplo n.º 5
0
    def load_service_model(self, service_name, type_name, api_version=None):
        """Load a botocore service model

        This is the main method for loading botocore models (e.g. a service
        model, pagination configs, waiter configs, etc.).

        :type service_name: str
        :param service_name: The name of the service (e.g ``ec2``, ``s3``).

        :type type_name: str
        :param type_name: The model type.  Valid types include, but are not
            limited to: ``service-2``, ``paginators-1``, ``waiters-2``.

        :type api_version: str
        :param api_version: The API version to load.  If this is not
            provided, then the latest API version will be used.

        :return: The loaded data, or a DataNotFoundError if no data
            could be found.

        """
        # Wrapper around the load_data.  This will calculate the path
        # to call load_data with.
        if service_name not in self.list_available_services('service-2'):
            raise ValidationError(value=service_name, param='service_name',
                                  type_name='str')
        if api_version is None:
            api_version = self.determine_latest_version(
                service_name, type_name)
        full_path = os.path.join(service_name, api_version, type_name)
        return self.load_data(full_path)
Ejemplo n.º 6
0
 def validate(self, value):
     if not isinstance(value, dict):
         raise ValidationError(value=str(value), type_name='structure',
                               param=self)
     self._validate_known_keys(value)
     self._validate_required_keys(value)
     for member in self.members:
         sub_value = value.get(member.name)
         if sub_value is not None:
             member.validate(sub_value)
Ejemplo n.º 7
0
 def validate(self, value):
     try:
         return dateutil.parser.parse(value)
     except:
         pass
     try:
         # Might be specified as an epoch time
         return datetime.datetime.utcfromtimestamp(value)
     except:
         pass
     raise ValidationError(value=str(value), type_name='timestamp',
                           param=self)
Ejemplo n.º 8
0
 def validate(self, value):
     try:
         if not isinstance(value, bool):
             if isinstance(value, six.string_types):
                 if value.lower() == 'true':
                     value = True
                 elif value.lower() == 'false':
                     value = False
                 else:
                     raise ValueError
         return value
     except ValueError:
         raise ValidationError(value=str(value), type_name='boolean',
                               param=self)
Ejemplo n.º 9
0
 def validate(self, value):
     if not isinstance(value, float):
         raise ValidationError(value=str(value), type_name='double',
                               param=self)
     if self.min:
         if value < self.min:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     if self.max:
         if value > self.max:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     return value
Ejemplo n.º 10
0
 def validate(self, value):
     if not isinstance(value, six.integer_types):
         raise ValidationError(value=str(value), type_name='integer',
                               param=self)
     if self.min:
         if value < self.min:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     if self.max:
         if value > self.max:
             raise RangeError(value=value,
                              param=self,
                              min_value=self.min,
                              max_value=self.max)
     return value
Ejemplo n.º 11
0
    def validate(self, value):
        if not isinstance(value, six.string_types):
            raise ValidationError(value=str(value), type_name='string',
                                  param=self)

        if self.min:
            if len(value) < self.min:
                raise RangeError(value=len(value),
                                 param=self,
                                 min_value=self.min,
                                 max_value=self.max)
        if self.max:
            if len(value) > self.max:
                raise RangeError(value=len(value),
                                 param=self,
                                 min_value=self.min,
                                 max_value=self.max)
        return value
Ejemplo n.º 12
0
 def __init__(self, table, index_name):
     self.table = table
     self.index_name = index_name
     found = False
     for index_info in self.table.global_secondary_indexes:
         if index_info['IndexName'] == index_name:
             for k in index_info['KeySchema']:
                 if k['KeyType'] == 'HASH':
                     self.part_key = k['AttributeName']
                 elif k['KeyType'] == 'RANGE':
                     self.range_key = k['AttributeName']
             found = True
         break
     if not found:
         from botocore.exceptions import ValidationError
         raise ValidationError(param='IndexName',
                               value=index_name,
                               type_name='str')
Ejemplo n.º 13
0
 def validate(self, value):
     if not isinstance(value, dict):
         raise ValidationError(value=str(value),
                               type_name='map', param=self)