def _expand_path(path): """Expand both environment variables and user home in the given path."""
path = os.path.expandvars(path)
path = os.path.expanduser(path) return path
def merge_configs(default, overwrite): """Recursively update a dict with the key/value pair of another.
Dict values that are dictionaries themselves will be updated, whilst
preserving existing keys. """
new_config = copy.deepcopy(default)
for k, v in overwrite.items(): # Make sure to preserve existing items in # nested dicts, for example `abbreviations` if isinstance(v, dict):
new_config[k] = merge_configs(default.get(k, {}), v) else:
new_config[k] = v
return new_config
def get_config(config_path): """Retrieve the config from the specified path, returning a config dict.""" ifnot os.path.exists(config_path): raise ConfigDoesNotExistException(f'Config file {config_path} does not exist.')
logger.debug('config_path is %s', config_path) with open(config_path, encoding='utf-8') as file_handle: try:
yaml_dict = yaml.safe_load(file_handle) or {} except yaml.YAMLError as e: raise InvalidConfiguration(
f'Unable to parse YAML file {config_path}.'
) from e ifnot isinstance(yaml_dict, dict): raise InvalidConfiguration(
f'Top-level element of YAML file {config_path} should be an object.'
)
def get_user_config(config_file=None, default_config=False): """Return the user config as a dict.
If ``default_config`` isTrue, ignore ``config_file`` andreturn default
values for the config parameters.
If ``default_config`` is a dict, merge values with default values andreturn them for the config parameters.
If a path to a ``config_file`` is given, that is different from the default
location, load the user config from that.
Otherwise look up the config file path in the ``COOKIECUTTER_CONFIG``
environment variable. If set, load the config from this path. This will raise an error if the specified path isnot valid.
If the environment variable isnot set, try the default config file path
before falling back to the default config values. """ # Do NOT load a config. Merge provided values with defaults and return them instead if default_config and isinstance(default_config, dict): return merge_configs(DEFAULT_CONFIG, default_config)
# Do NOT load a config. Return defaults instead. if default_config:
logger.debug("Force ignoring user config with default_config switch.") return copy.copy(DEFAULT_CONFIG)
# Load the given config file if config_file and config_file isnot USER_CONFIG_PATH:
logger.debug("Loading custom config from %s.", config_file) return get_config(config_file)
try: # Does the user set up a config environment variable?
env_config_file = os.environ['COOKIECUTTER_CONFIG'] except KeyError: # Load an optional user config if it exists # otherwise return the defaults if os.path.exists(USER_CONFIG_PATH):
logger.debug("Loading config from %s.", USER_CONFIG_PATH) return get_config(USER_CONFIG_PATH) else:
logger.debug("User config not found. Loading default config.") return copy.copy(DEFAULT_CONFIG) else: # There is a config environment variable. Try to load it. # Do not check for existence, so invalid file paths raise an error.
logger.debug("User config not found or not specified. Loading default config.") return get_config(env_config_file)
Messung V0.5
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.