def set(self, data): # type: (SettingsData) -> None """ Initialize and validate the configuration data (contents) for this settings object. :param data: A mapping of unicode string keys to any values, which, together with the defined defaults in this class, should match the defined schema for this class, as merged with its parent classes. :raises: :class:`conformity.settings.Settings.ImproperlyConfigured` """ # Merged the class defaults with the supplied data to get the effective settings data settings = self._merge_mappings(copy.deepcopy(data), copy.deepcopy(self.defaults)) # Ensure that all keys required by the schema are present in the settings data unpopulated_keys = set(self.schema.keys()) - set(settings.keys()) if unpopulated_keys: raise self.ImproperlyConfigured( 'No value provided for required setting(s): {}'.format( ', '.join(unpopulated_keys))) # Ensure that all keys in the settings data are present in the schema unconsumed_keys = set(settings.keys()) - set(self.schema.keys()) if unconsumed_keys: raise self.ImproperlyConfigured('Unknown setting(s): {}'.format( ', '.join(unconsumed_keys))) # Ensure that all values in the settings data pass standard Conformity field validation for key, value in settings.items(): try: validate(self.schema[key], value, "setting '{}'".format(key)) except ValidationError as e: raise self.ImproperlyConfigured(*e.args) # Once all checks have passed, atomically set the internal settings data self._data = settings
def set(self, data): """ Sets the value of this settings object in its entirety. """ settings = copy.deepcopy(self.defaults) data = copy.deepcopy(data) self._merge_dicts(data, settings) # Make sure all values were populated unpopulated_keys = set(self.schema.keys()) - set(settings.keys()) if unpopulated_keys: raise ValueError('No value provided for required setting(s): {}'.format(', '.join(unpopulated_keys))) unconsumed_keys = set(settings.keys()) - set(self.schema.keys()) if unconsumed_keys: raise ValueError('Unknown setting(s): {}'.format(', '.join(unconsumed_keys))) for key, value in settings.items(): # Validate the value validate(self.schema[key], value, "setting '{}'".format(key)) self._data[key] = value self._convert_class_schemas(self, self._data, self.schema)
def test_validate(self): # type: () -> None schema = Dictionary({ 'name': UnicodeString(max_length=20), 'greeting': UnicodeString(), }, optional_keys=('greeting', )) validate(schema, {'name': 'Andrew'}) validate(schema, {'name': 'Andrew', 'greeting': 'Ahoy-hoy'}) with self.assertRaises(ValidationError): validate(schema, {'name': 'Andrewverylongnameperson'}) with self.assertRaises(ValidationError): validate(schema, {'name': 'Andrew', 'greeeeeeting': 'Ahoy-hoy'})
def set(self, data): """ Sets the value of this settings object in its entirety. """ settings = copy.deepcopy(self.defaults) data = copy.deepcopy(data) self._merge_dicts(data, settings) # Make sure all values were populated unpopulated_keys = set(self.schema.keys()) - set(settings.keys()) if unpopulated_keys: raise self.ImproperlyConfigured( 'No value provided for required setting(s): {}'.format( ', '.join(unpopulated_keys))) unconsumed_keys = set(settings.keys()) - set(self.schema.keys()) if unconsumed_keys: raise self.ImproperlyConfigured('Unknown setting(s): {}'.format( ', '.join(unconsumed_keys))) for key, value in settings.items(): # Validate the value try: validate(self.schema[key], value, "setting '{}'".format(key)) except ValidationError as e: raise self.ImproperlyConfigured(*e.args) self._data[key] = value