def load_from_db(cls): """Loads dynamic properties from db.""" logging.info('Reloading properties.') overrides = {} for item in ConfigPropertyEntity.all().fetch(1000): name = item.key().name() if not name in cls.registered: logging.error('Property is not registered (skipped): %s', name) continue target = cls.registered[name] if target and not item.is_draft: # Enforce value type. try: value = transforms.string_to_value(item.value, target.value_type) except Exception: # pylint: disable-msg=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) continue # Don't allow disabling of update interval from a database. if name == UPDATE_INTERVAL_SEC.name: if value == 0 or value < 0 or value > MAX_UPDATE_INTERVAL: logging.error('Bad value %s for %s; discarded.', name, value) continue else: cls.update_interval = value overrides[name] = value cls.db_overrides = overrides
def _set_value(cls, item, overrides, drafts): name = item.key().name() target = cls.registered.get(name, None) if not target: logging.warning("Property is not registered (skipped): %s", name) return if item.is_draft: if name in overrides: del overrides[name] drafts.add(name) else: if name in drafts: drafts.remove(name) # Enforce value type. try: value = transforms.string_to_value(item.value, target.value_type) except Exception: # pylint: disable=broad-except logging.error("Property %s failed to cast to a type %s; removing.", target.name, target.value_type) return # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable=broad-except errors.append("Error validating property %s.\n%s", (target.name, e)) if errors: logging.error("Property %s has invalid value:\n%s", target.name, "\n".join(errors)) return overrides[name] = value
def _set_value(cls, item, overrides, drafts): name = item.key().name() target = cls.registered.get(name, None) if not target: if appengine_config.MODULE_REGISTRATION_IN_PROGRESS: log_level = logging.INFO else: log_level = logging.WARNING logging.log(log_level, 'Property is not registered (skipped): %s', name) return if item.is_draft: if name in overrides: del overrides[name] drafts.add(name) else: if name in drafts: drafts.remove(name) # Enforce value type. try: value = transforms.string_to_value( item.value, target.value_type) except Exception: # pylint: disable=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) return # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable=broad-except errors.append( 'Error validating property %s.\n%s', (target.name, e)) if errors: logging.error( 'Property %s has invalid value:\n%s', target.name, '\n'.join(errors)) return overrides[name] = value
def _load_from_db(cls): """Loads dynamic properties from db.""" logging.info('Reloading properties.') overrides = {} drafts = set() for item in ConfigPropertyEntity.all().fetch(1000): name = item.key().name() if not name in cls.registered: logging.error( 'Property is not registered (skipped): %s', name) continue target = cls.registered[name] if target and item.is_draft: drafts.add(name) if target and not item.is_draft: # Enforce value type. try: value = transforms.string_to_value( item.value, target.value_type) except Exception: # pylint: disable-msg=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) continue # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable-msg=broad-except errors.append( 'Error validating property %s.\n%s', (target.name, e)) if errors: logging.error( 'Property %s has invalid value:\n%s', target.name, '\n'.join(errors)) continue overrides[name] = value cls.db_overrides = overrides cls.names_with_draft = drafts
def _set_value(cls, item, overrides, drafts): name = item.key().name() target = cls.registered.get(name, None) if not target: if appengine_config.MODULE_REGISTRATION_IN_PROGRESS: log_level = logging.INFO else: log_level = cls.UNREGISTERED_PROPERTY_LOGGING_LEVEL logging.log(log_level, 'Property is not registered (skipped): %s', name) return if item.is_draft: if name in overrides: del overrides[name] drafts.add(name) else: if name in drafts: drafts.remove(name) # Enforce value type. try: value = transforms.string_to_value( item.value, target.value_type) except Exception: # pylint: disable=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) return # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable=broad-except errors.append( 'Error validating property %s.\n%s', (target.name, e)) if errors: logging.error( 'Property %s has invalid value:\n%s', target.name, '\n'.join(errors)) return overrides[name] = value
def _load_from_db(cls): """Loads dynamic properties from db.""" logging.info('Reloading properties.') overrides = {} drafts = set() for item in ConfigPropertyEntity.all().fetch(1000): name = item.key().name() if name not in cls.registered: logging.error( 'Property is not registered (skipped): %s', name) continue target = cls.registered[name] if target and item.is_draft: drafts.add(name) if target and not item.is_draft: # Enforce value type. try: value = transforms.string_to_value( item.value, target.value_type) except Exception: # pylint: disable-msg=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) continue # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable-msg=broad-except errors.append( 'Error validating property %s.\n%s', (target.name, e)) if errors: logging.error( 'Property %s has invalid value:\n%s', target.name, '\n'.join(errors)) continue overrides[name] = value cls.db_overrides = overrides cls.names_with_draft = drafts
def get_environ_value(self): """Tries to get value from the environment variables.""" # Look for a name in lower or upper case. name = None if self._name.lower() in os.environ: name = self._name.lower() else: if self._name.upper() in os.environ: name = self._name.upper() if name: try: return True, transforms.string_to_value(os.environ[name], self.value_type) except Exception: # pylint: disable=broad-except logging.error("Property %s failed to cast to type %s; removing.", self._name, self._type) del os.environ[name] return False, None
def get_environ_value(self): """Tries to get value from the environment variables.""" # Look for a name in lower or upper case. name = None if self._name.lower() in os.environ: name = self._name.lower() else: if self._name.upper() in os.environ: name = self._name.upper() if name: try: return True, transforms.string_to_value( os.environ[name], self.value_type) except Exception: # pylint: disable=broad-except logging.error( 'Property %s failed to cast to type %s; removing.', self._name, self._type) del os.environ[name] return False, None
def _set_value(cls, item, overrides, drafts): name = item.key().name() target = cls.registered.get(name, None) if not target: logging.warning('Property is not registered (skipped): %s', name) return if item.is_draft: if name in overrides: del overrides[name] drafts.add(name) else: if name in drafts: drafts.remove(name) # Enforce value type. try: value = transforms.string_to_value(item.value, target.value_type) except Exception: # pylint: disable=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) return # Enforce value validator. if target.validator: errors = [] try: target.validator(value, errors) except Exception as e: # pylint: disable=broad-except errors.append('Error validating property %s.\n%s', (target.name, e)) if errors: logging.error('Property %s has invalid value:\n%s', target.name, '\n'.join(errors)) return overrides[name] = value
def load_from_db(cls): """Loads dynamic properties from db.""" logging.info('Reloading properties.') overrides = {} for item in ConfigPropertyEntity.all().fetch(1000): name = item.key().name() if not name in cls.registered: logging.error( 'Property is not registered (skipped): %s', name) continue target = cls.registered[name] if target and not item.is_draft: # Enforce value type. try: value = transforms.string_to_value( item.value, target.value_type) except Exception: # pylint: disable-msg=broad-except logging.error( 'Property %s failed to cast to a type %s; removing.', target.name, target.value_type) continue # Don't allow disabling of update interval from a database. if name == UPDATE_INTERVAL_SEC.name: if value == 0 or value < 0 or value > MAX_UPDATE_INTERVAL: logging.error( 'Bad value %s for %s; discarded.', name, value) continue else: cls.update_interval = value overrides[name] = value cls.db_overrides = overrides