def set(cls, name, value, validate=True): """Stores values for a config name in memcache and datastore. Args: name: str, name of the config setting. value: str, int, bool, list value to set or change config setting. validate: bool, checks keys against config_defaults if enabled. Raises: KeyError: Error raised when name does not exist in config.py file. """ if validate: config_defaults = utils.load_config_from_yaml() if name not in config_defaults: raise KeyError(_CONFIG_NOT_FOUND_MSG % name) if isinstance(value, six.string_types): stored_config = cls.get_or_insert(name) stored_config.string_value = value stored_config.put() if isinstance(value, bool) and isinstance(value, int): stored_config = cls.get_or_insert(name) stored_config.bool_value = value stored_config.put() if isinstance(value, int) and not isinstance(value, bool): stored_config = cls.get_or_insert(name) stored_config.integer_value = value stored_config.put() if isinstance(value, list): stored_config = cls.get_or_insert(name) stored_config.list_value = value stored_config.put() memcache.set(name, value)
def list_configs(self, request): """Gets a list of all config values.""" self.check_xsrf_token(self.request_state) response_message = [] config_defaults = utils.load_config_from_yaml() for setting in config_defaults: setting_value = config_model.Config.get(setting) if isinstance(setting_value, basestring): response_message.append( config_messages.ConfigResponse(name=setting, string_value=setting_value)) if (isinstance(setting_value, int) and not isinstance(setting_value, bool) or isinstance(setting_value, float)): response_message.append( config_messages.ConfigResponse( name=setting, integer_value=setting_value)) if isinstance(setting_value, bool) and isinstance( setting_value, int): response_message.append( config_messages.ConfigResponse( name=setting, boolean_value=setting_value)) if isinstance(setting_value, list): response_message.append( config_messages.ConfigResponse(name=setting, list_value=setting_value)) return config_messages.ListConfigsResponse(configs=response_message)
def bootstrap_load_config_yaml(**kwargs): """Loads config_defaults.yaml into datastore. Args: **kwargs: Unused, but required for bootstrap tasks. """ del kwargs # Unused, but comes by default. config_defaults = utils.load_config_from_yaml() for name, value in config_defaults.iteritems(): config_model.Config.set(name, value, False)
def get(cls, name): """Checks memcache for name, if not available, check datastore. Args: name: str, name of config name. Returns: The config value from memcache, datastore, or config file. Raises: KeyError: An error occurred when name does not exist. """ memcache_config = memcache.get(name) cached_config = None if memcache_config: return memcache_config stored_config = cls.get_by_id(name, use_memcache=False) if stored_config: if stored_config.string_value: cached_config = stored_config.string_value elif stored_config.integer_value: cached_config = stored_config.integer_value elif stored_config.bool_value is not None: cached_config = stored_config.bool_value elif stored_config.list_value: cached_config = stored_config.list_value # Conversion from use_asset_tags to device_identifier_mode. if name == 'device_identifier_mode' and not cached_config: if cls.get('use_asset_tags'): cached_config = DeviceIdentifierMode.BOTH_REQUIRED cls.set(name, cached_config) memcache.set(name, cached_config) if cached_config is not None: memcache.set(name, cached_config) return cached_config config_defaults = utils.load_config_from_yaml() if name in config_defaults: value = config_defaults[name] cls.set(name, value) return value raise KeyError(_CONFIG_NOT_FOUND_MSG, name)