def extract_value(proxy): """Given a value proxy type, Retrieve a value from a namespace, raising exception if no value is found, or the value does not validate. """ value = proxy.namespace.get(proxy.config_key, proxy.default) if value is UndefToken: raise errors.ConfigurationError("%s is missing value for: %s" % (proxy.namespace, proxy.config_key)) try: return proxy.validator(value) except errors.ValidationError, e: raise errors.ConfigurationError("%s failed to validate %s: %s" % (proxy.namespace, proxy.config_key, e))
def validate_keys(self, config_data, error_on_unknown): unknown = remove_by_keys(config_data, self.get_known_keys()) if not unknown: return msg = "Unexpected value in %s configuration: %s" % (self.name, unknown) if error_on_unknown: raise errors.ConfigurationError(msg) log.info(msg)
def has_duplicate_keys(config_data, base_conf, raise_error): """Compare two dictionaries for duplicate keys. if raise_error is True then raise on exception, otherwise log return True.""" duplicate_keys = set(base_conf) & set(config_data) if not duplicate_keys: return msg = "Duplicate keys in config: %s" % duplicate_keys if raise_error: raise errors.ConfigurationError(msg) log.info(msg) return True
def parse_line(line): line = line.strip() if not line or line.startswith('#'): return try: key, value = split_pattern.split(line, 1) except ValueError: msg = "Invalid properties line: %s" % line raise errors.ConfigurationError(msg) return key.strip(), value.strip()
def validate_keys(self, config_data, error_on_unknown): """Raise an exception if error_on_unknown is true, and keys contains a key which is not defined in a registeredValueProxy. """ unknown = remove_by_keys(config_data, self.get_known_keys()) if not unknown: return msg = "Unexpected value in %s configuration: %s" % (self.name, unknown) if error_on_unknown: raise errors.ConfigurationError(msg) log.info(msg)
def build_from_element(element): items = dict(element.items()) child_items = dict( (child.tag, build_from_element(child)) for child in element) config.has_duplicate_keys(child_items, items, safe) items.update(child_items) if element.text: if 'value' in items and safe: msg = "%s has tag with child or attribute named value." raise errors.ConfigurationError(msg % filename) items['value'] = element.text return items
def auto_loader(base_dir='.', auto_configurations=None): auto_configurations = auto_configurations or [ (yaml_loader, 'config.yaml'), (json_loader, 'config.json'), (ini_file_loader, 'config.ini'), (xml_loader, 'config.xml'), (properties_loader, 'config.properties') ] for config_loader, config_arg in auto_configurations: path = os.path.join(base_dir, config_arg) if os.path.isfile(path): return config_loader(path) msg = "Failed to auto-load configuration. No configuration files found." raise errors.ConfigurationError(msg)
def validate_keys( self, config_data, error_on_unknown, log_keys_only=False, ): unknown = remove_by_keys(config_data, self.get_known_keys()) if not unknown: return if log_keys_only: unknown = [k for k, _ in unknown] msg = "Unexpected value in %s configuration: %s" % (self.name, unknown) if error_on_unknown: raise errors.ConfigurationError(msg) log.info(msg)
def _read_config(config_key, config_namespace, default): value = config_namespace.get(config_key, default=default) if value is UndefToken: msg = '%s missing value for %s' % (config_namespace, config_key) raise errors.ConfigurationError(msg) return value
def get_namespace(cls, attributes): if 'namespace' not in attributes: raise errors.ConfigurationError( "ConfigSchema requires a namespace.") return config.get_namespace(attributes['namespace'])