def add_from_file(filename, scope=None): """Add updates to a config from a filename """ import spack.environment as ev # Get file as config dict data = read_config_file(filename) if any(k in data for k in spack.schema.env.keys): data = ev.config_dict(data) # update all sections from config dict # We have to iterate on keys to keep overrides from the file for section in data.keys(): if section in section_schemas.keys(): # Special handling for compiler scope difference # Has to be handled after we choose a section if scope is None: scope = default_modify_scope(section) value = data[section] existing = get(section, scope=scope) new = merge_yaml(existing, value) # We cannot call config.set directly (set is a type) config.set(section, new, scope)
def add(fullpath, scope=None): """Add the given configuration to the specified config scope. Add accepts a path. If you want to add from a filename, use add_from_file""" components = process_config_path(fullpath) has_existing_value = True path = '' override = False for idx, name in enumerate(components[:-1]): # First handle double colons in constructing path colon = '::' if override else ':' if path else '' path += colon + name if getattr(name, 'override', False): override = True else: override = False # Test whether there is an existing value at this level existing = get(path, scope=scope) if existing is None: has_existing_value = False # We've nested further than existing config, so we need the # type information for validation to know how to handle bare # values appended to lists. existing = get_valid_type(path) # construct value from this point down value = syaml.load_config(components[-1]) for component in reversed(components[idx + 1:-1]): value = {component: value} break if has_existing_value: path, _, value = fullpath.rpartition(':') value = syaml.load_config(value) existing = get(path, scope=scope) # append values to lists if isinstance(existing, list) and not isinstance(value, list): value = [value] # merge value into existing new = merge_yaml(existing, value) config.set(path, new, scope)
def override(path_or_scope, value=None): """Simple way to override config settings within a context. Arguments: path_or_scope (ConfigScope or str): scope or single option to override value (object or None): value for the single option Temporarily push a scope on the current configuration, then remove it after the context completes. If a single option is provided, create an internal config scope for it and push/pop that scope. """ if isinstance(path_or_scope, ConfigScope): overrides = path_or_scope config.push_scope(path_or_scope) else: base_name = overrides_base_name # Ensure the new override gets a unique scope name current_overrides = [ s.name for s in config.matching_scopes(r'^{0}'.format(base_name)) ] num_overrides = len(current_overrides) while True: scope_name = '{0}{1}'.format(base_name, num_overrides) if scope_name in current_overrides: num_overrides += 1 else: break overrides = InternalConfigScope(scope_name) config.push_scope(overrides) config.set(path_or_scope, value, scope=scope_name) try: yield config finally: scope = config.remove_scope(overrides.name) assert scope is overrides
def override(path_or_scope, value=None): """Simple way to override config settings within a context. Arguments: path_or_scope (ConfigScope or str): scope or single option to override value (object, optional): value for the single option Temporarily push a scope on the current configuration, then remove it after the context completes. If a single option is provided, create an internal config scope for it and push/pop that scope. """ if isinstance(path_or_scope, ConfigScope): overrides = path_or_scope config.push_scope(path_or_scope) else: overrides = InternalConfigScope('overrides') config.push_scope(overrides) config.set(path_or_scope, value, scope='overrides') yield config scope = config.remove_scope(overrides.name) assert scope is overrides
def set(path, value, scope=None): """Convenience function for setting single values in config files. Accepts the path syntax described in ``get()``. """ return config.set(path, value, scope)
def set(path, value, scope=None): """Convenience function for getting single values in config files. Accepts the path syntax described in ``get()``. """ return config.set(path, value, scope)