コード例 #1
0
ファイル: __init__.py プロジェクト: madron/brick-iot
 def validate_hardware(self, hardware, delay=None):
     if self.hardware_list:
         if not hardware:
             msg = 'Missing hardware parameter.'
             raise ValidationError(msg)
         hardware = copy(hardware)
         if 'type' not in hardware:
             raise ValidationError('Missing hardware type.')
         hardware_type = hardware.pop('type')
         if hardware_type not in self.hardware_list:
             msg = "Hardware type '{}' not supported. Choices: {}".format(
                 hardware_type, self.hardware_list.keys())
             raise ValidationError(msg)
         hardware_class = self.hardware_list[hardware_type]
         if 'device' in hardware:
             hardware_name = hardware['device']
             device = self.hardware_manager.hardware[hardware_name]
             hardware['device'] = device
         hardware.update(self.hardware_config_extra)
         return hardware_class(**hardware)
     else:
         if hardware:
             msg = 'Hardware parameter not supported.'
             raise ValidationError(msg)
         else:
             return None
コード例 #2
0
ファイル: __init__.py プロジェクト: madron/brick-iot
def validate_device(config, hardware_manager):
    re_name = re.compile('^[a-z][a-z0-9_]*$')
    import_device_modules()
    devices = dict()
    errors = dict()
    for device_name, device_config in config.items():
        try:
            device_config = device_config.copy()
            if not re_name.match(device_name):
                raise ValidationError(
                    'Name must contains lowercase characters, numbers and _ only.'
                )
            if 'type' not in device_config:
                raise ValidationError('type parameter is required.')
            device_type = device_config.pop('type')
            if device_type not in _device_registry:
                raise ValidationError(
                    "type '{}' does not exist.".format(device_type))
            device_class = _device_registry[device_type]
            device_config['hardware_manager'] = hardware_manager
            devices[device_name] = device_class(**device_config)
        except ValidationError as error:
            errors[device_name] = error
        except Exception as error:
            errors[device_name] = ValidationError(str(error))
    if errors:
        raise ValidationError(errors)
    return devices
コード例 #3
0
ファイル: base.py プロジェクト: madron/brick-iot
 def validate_contact(self, contact):
     if contact is False:
         contact = 'no'
     if contact not in ('no', 'nc'):
         msg = "contact '{}' not supported. Choices: 'no' (normally open) or 'nc' (normally closed)".format(contact)
         raise ValidationError(msg)
     return contact
コード例 #4
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def __call__(self, value):
     if self.null and value is None:
         return None
     if value == 'on' or value is True:
         return 'on'
     if value == 'off' or value is False:
         return 'off'
     msg = "Ensure {} is 'on' or 'off'.".format(self.name)
     raise ValidationError(msg)
コード例 #5
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def __call__(self, value):
     if not isinstance(value, bool):
         value = str(value).lower()
         if value in ['true', 'yes']:
             return True
         elif value in ['false', 'no']:
             return False
         msg = 'Ensure {} is true or false.'.format(self.name)
         raise ValidationError(msg)
     return value
コード例 #6
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def __call__(self, value):
     if not isinstance(value, int):
         try:
             value = int(value)
         except ValueError:
             msg = 'Ensure {} is an integer number.'.format(self.name)
             raise ValidationError(msg)
         if value == 0:
             value = abs(value)
     self.check_min_value(value)
     self.check_max_value(value)
     return value
コード例 #7
0
 def channel_config(self, port, channel, name='', direction='out'):
     if port not in self.ports:
         raise ValidationError('Port should be one of {}'.format(
             self.ports))
     if channel not in self.channels:
         raise ValidationError('Channel should be one of {}'.format(
             self.channels))
     if channel in self.name[port]:
         if name:
             msg = "Port '{}' channel {} already used by '{}'".format(
                 port, channel, name)
         else:
             msg = "Port '{}' channel {} already in use".format(
                 port, channel, name)
         raise ValidationError(msg)
     if direction not in self.directions:
         raise ValidationError('Direction should be one of {}'.format(
             self.directions))
     self.name[port][channel] = name
     self.direction[port][channel] = '0' if direction == 'out' else '1'
     self.pullup[port][channel] = '1' if direction == 'in' else '0'
コード例 #8
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def __call__(self, value):
     if isinstance(value, Decimal):
         value = value.quantize(self.precision_quantize)
     else:
         try:
             value = round(float(value), self.precision)
         except ValueError:
             msg = 'Ensure {} is a number.'.format(self.name)
             raise ValidationError(msg)
         value = Decimal(value).quantize(self.precision_quantize)
     self.check_min_value(value)
     self.check_max_value(value)
     if value == 0:
         value = abs(value)
     return value
コード例 #9
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def check_max_value(self, value):
     if self.max_value is not None and value > self.max_value:
         msg = 'Ensure {} is less than or equal to {}'.format(
             self.name, self.max_value)
         raise ValidationError(msg)
コード例 #10
0
ファイル: validators.py プロジェクト: madron/brick-iot
 def check_min_value(self, value):
     if self.min_value is not None and value < self.min_value:
         msg = 'Ensure {} is greater than or equal to {}'.format(
             self.name, self.min_value)
         raise ValidationError(msg)