Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Apr 6, 2024
1 parent 142c34a commit daf9a3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cookiecutter/cli.py
Expand Up @@ -60,7 +60,7 @@ def validate_extra_context(


def list_installed_templates(
default_config: bool | dict[str, Any], passed_config_file: str | None
default_config: bool | dict[str, Any], passed_config_file: Path | None
) -> None:
"""List installed (locally cloned) templates. Use cookiecutter --list-installed."""
config = get_user_config(passed_config_file, default_config)
Expand Down Expand Up @@ -175,7 +175,7 @@ def main(
replay: bool | str,
overwrite_if_exists: bool,
output_dir: str,
config_file: str | None,
config_file: Path | None,
default_config: bool,
debug_file: str | None,
directory: str,
Expand Down
30 changes: 13 additions & 17 deletions cookiecutter/config.py
Expand Up @@ -6,18 +6,16 @@
import copy
import logging
import os
from typing import TYPE_CHECKING, Any
from pathlib import Path
from typing import Any

import yaml

from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration

if TYPE_CHECKING:
from pathlib import Path

logger = logging.getLogger(__name__)

USER_CONFIG_PATH = os.path.expanduser('~/.cookiecutterrc')
USER_CONFIG_PATH = Path('~/.cookiecutterrc').expanduser()

BUILTIN_ABBREVIATIONS = {
'gh': 'https://github.com/{0}.git',
Expand All @@ -26,18 +24,16 @@
}

DEFAULT_CONFIG = {
'cookiecutters_dir': os.path.expanduser('~/.cookiecutters/'),
'replay_dir': os.path.expanduser('~/.cookiecutter_replay/'),
'cookiecutters_dir': Path('~/.cookiecutters/').expanduser(),
'replay_dir': Path('~/.cookiecutter_replay/').expanduser(),
'default_context': collections.OrderedDict([]),
'abbreviations': BUILTIN_ABBREVIATIONS,
}


def _expand_path(path: str) -> str:
def _expand_path(path: str) -> Path:
"""Expand both environment variables and user home in the given path."""
path = os.path.expandvars(path)
path = os.path.expanduser(path)
return path
return Path(os.path.expandvars(path)).expanduser()


def merge_configs(default: dict[str, Any], overwrite: dict[str, Any]) -> dict[str, Any]:
Expand All @@ -59,13 +55,13 @@ def merge_configs(default: dict[str, Any], overwrite: dict[str, Any]) -> dict[st
return new_config


def get_config(config_path: Path | str) -> dict[str, Any]:
def get_config(config_path: Path) -> dict[str, Any]:
"""Retrieve the config from the specified path, returning a config dict."""
if not os.path.exists(config_path):
if not config_path.exists():
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:
with config_path.open(encoding='utf-8') as file_handle:
try:
yaml_dict = yaml.safe_load(file_handle) or {}
except yaml.YAMLError as e:
Expand All @@ -89,7 +85,7 @@ def get_config(config_path: Path | str) -> dict[str, Any]:


def get_user_config(
config_file: str | None = None,
config_file: Path | None = None,
default_config: bool | dict[str, Any] = False,
) -> dict[str, Any]:
"""Return the user config as a dict.
Expand Down Expand Up @@ -126,11 +122,11 @@ def get_user_config(

try:
# Does the user set up a config environment variable?
env_config_file = os.environ['COOKIECUTTER_CONFIG']
env_config_file = Path(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):
if USER_CONFIG_PATH.exists():
logger.debug("Loading config from %s.", USER_CONFIG_PATH)
return get_config(USER_CONFIG_PATH)
else:
Expand Down

0 comments on commit daf9a3f

Please sign in to comment.