A command-line utility that creates projects from project templates, e.g. creating a Python package project from a Python package project template.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
User configuration handling with defaults, file-based config, and runtime overrides for customizing cookiecutter behavior. The configuration system allows users to set default values, template abbreviations, and preferences that persist across cookiecutter sessions.
Load and merge user configuration from various sources with fallback to defaults.
def get_user_config(config_file=None, default_config=False):
"""
Return user config as dict with various fallback options.
Parameters:
- config_file: str, optional - Path to user config file
- default_config: bool - Use default values rather than a config file
Returns:
dict - Configuration dictionary with merged settings
"""
def get_config(config_path):
"""
Retrieve config from specified path, returning config dict.
Parameters:
- config_path: str - Path to configuration file
Returns:
dict - Configuration dictionary from file
"""Merge configuration dictionaries with proper precedence handling.
def merge_configs(default, overwrite):
"""
Recursively merge configuration dictionaries.
Parameters:
- default: dict - Default configuration values
- overwrite: dict - Configuration values to override defaults
Returns:
dict - Merged configuration dictionary
"""USER_CONFIG_PATH: str # Default user config file path (~/.cookiecutterrc)
BUILTIN_ABBREVIATIONS: dict # Built-in repository abbreviations
# Contains: {'gh': 'https://github.com/{0}.git', 'gl': 'https://gitlab.com/{0}.git', 'bb': 'https://bitbucket.org/{0}'}
DEFAULT_CONFIG: dict # Default configuration dictionaryCookiecutter supports YAML configuration files with the following structure:
default_context:
full_name: "Your Name"
email: "you@example.com"
github_username: "yourusername"
cookiecutters_dir: "~/.cookiecutters"
abbreviations:
gh: https://github.com/{0}.git
gl: https://gitlab.com/{0}.git
bb: https://bitbucket.org/{0}
custom: https://mycustomrepo.com/{0}.git
replay_dir: "~/.cookiecutter_replay"from cookiecutter.config import get_user_config
# Load default user configuration
config = get_user_config()
# Load from specific config file
config = get_user_config(config_file='/path/to/config.yaml')
# Use built-in defaults only
config = get_user_config(default_config=True)The configuration dictionary contains these standard keys:
{
'default_context': {
# Default values for template variables
'full_name': 'User Name',
'email': 'user@example.com'
},
'cookiecutters_dir': '/path/to/cookiecutters', # Template cache directory
'abbreviations': {
# Repository shortcuts
'gh': 'https://github.com/{}.git',
'custom': 'https://myrepo.com/{}.git'
},
'replay_dir': '/path/to/replay' # Replay file storage
}from cookiecutter.config import get_user_config, merge_configs
# Load user config and merge with custom overrides
base_config = get_user_config()
custom_overrides = {
'default_context': {
'project_name': 'my-default-project',
'version': '0.1.0'
},
'cookiecutters_dir': '/custom/templates/path'
}
merged_config = merge_configs(base_config, custom_overrides)from cookiecutter.config import BUILTIN_ABBREVIATIONS
from cookiecutter.repository import expand_abbreviations
# Use built-in abbreviations
print(BUILTIN_ABBREVIATIONS)
# {'gh': 'https://github.com/{0}.git', 'gl': 'https://gitlab.com/{0}.git', 'bb': 'https://bitbucket.org/{0}'}
# Expand abbreviation to full URL
config = get_user_config()
full_url = expand_abbreviations('gh:audreyfeldroy/cookiecutter-pypackage', config['abbreviations'])
# Returns: 'https://github.com/audreyfeldroy/cookiecutter-pypackage.git'from cookiecutter.config import get_config
from cookiecutter.exceptions import ConfigDoesNotExistException
try:
# Load specific config file
config = get_config('/path/to/cookiecutter.yaml')
# Access configuration values
default_author = config.get('default_context', {}).get('full_name', 'Unknown')
template_dir = config.get('cookiecutters_dir', '~/.cookiecutters')
except ConfigDoesNotExistException:
print("Configuration file not found, using defaults")
config = get_user_config(default_config=True)import os
from cookiecutter.config import get_user_config
# Use different config for different environments
if os.environ.get('ENVIRONMENT') == 'development':
config = get_user_config(config_file='~/.cookiecutter-dev.yaml')
elif os.environ.get('ENVIRONMENT') == 'production':
config = get_user_config(config_file='~/.cookiecutter-prod.yaml')
else:
config = get_user_config() # Default config
# Override with environment variables
if 'COOKIECUTTER_AUTHOR' in os.environ:
if 'default_context' not in config:
config['default_context'] = {}
config['default_context']['full_name'] = os.environ['COOKIECUTTER_AUTHOR']Install with Tessl CLI
npx tessl i tessl/pypi-cookiecutter