Skip to content

Commit

Permalink
Merge pull request #1716 from cookiecutter/code-quality-fstrings-core
Browse files Browse the repository at this point in the history
Code quality: core files: Format replaced with f-strings
  • Loading branch information
insspb committed Jun 8, 2022
2 parents 24472a8 + 282cf58 commit fbf7d4c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
15 changes: 7 additions & 8 deletions cookiecutter/cli.py
Expand Up @@ -27,17 +27,16 @@ def version_msg():
"""Return the Cookiecutter version, location and Python powering it."""
python_version = sys.version
location = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
message = 'Cookiecutter %(version)s from {} (Python {})'
return message.format(location, python_version)
return f"Cookiecutter {__version__} from {location} (Python {python_version})"


def validate_extra_context(ctx, param, value):
"""Validate extra context."""
for s in value:
if '=' not in s:
for string in value:
if '=' not in string:
raise click.BadParameter(
'EXTRA_CONTEXT should contain items of the form key=value; '
"'{}' doesn't match that form".format(s)
f"EXTRA_CONTEXT should contain items of the form key=value; "
f"'{string}' doesn't match that form"
)

# Convert tuple -- e.g.: ('program_name=foobar', 'startsecs=66')
Expand All @@ -51,8 +50,8 @@ def list_installed_templates(default_config, passed_config_file):
cookiecutter_folder = config.get('cookiecutters_dir')
if not os.path.exists(cookiecutter_folder):
click.echo(
'Error: Cannot list installed templates. Folder does not exist: '
'{}'.format(cookiecutter_folder)
f"Error: Cannot list installed templates. "
f"Folder does not exist: {cookiecutter_folder}"
)
sys.exit(-1)

Expand Down
12 changes: 6 additions & 6 deletions cookiecutter/generate.py
Expand Up @@ -65,8 +65,8 @@ def apply_overwrites_to_context(context, overwrite_context):
context_value.insert(0, overwrite)
else:
raise ValueError(
"{} provided for choice variable {}, but the "
"choices are {}.".format(overwrite, variable, context_value)
f"{overwrite} provided for choice variable {variable}, "
f"but the choices are {context_value}."
)
elif isinstance(context_value, dict) and isinstance(overwrite, dict):
# Partially overwrite some keys in original dict
Expand Down Expand Up @@ -100,8 +100,8 @@ def generate_context(
full_fpath = os.path.abspath(context_file)
json_exc_message = str(e)
our_exc_message = (
'JSON decoding error while loading "{}". Decoding'
' error details: "{}"'.format(full_fpath, json_exc_message)
f"JSON decoding error while loading '{full_fpath}'. "
f"Decoding error details: '{json_exc_message}'"
)
raise ContextDecodingException(our_exc_message)

Expand All @@ -115,8 +115,8 @@ def generate_context(
if default_context:
try:
apply_overwrites_to_context(obj, default_context)
except ValueError as ex:
warnings.warn("Invalid default received: " + str(ex))
except ValueError as error:
warnings.warn(f"Invalid default received: {error}")
if extra_context:
apply_overwrites_to_context(obj, extra_context)

Expand Down
6 changes: 3 additions & 3 deletions cookiecutter/prompt.py
Expand Up @@ -66,9 +66,9 @@ def read_user_choice(var_name, options):
choice_lines = ['{} - {}'.format(*c) for c in choice_map.items()]
prompt = '\n'.join(
(
f'Select {var_name}:',
'\n'.join(choice_lines),
'Choose from {}'.format(', '.join(choices)),
f"Select {var_name}:",
"\n".join(choice_lines),
f"Choose from {', '.join(choices)}",
)
)

Expand Down
4 changes: 2 additions & 2 deletions cookiecutter/utils.py
Expand Up @@ -86,8 +86,8 @@ def prompt_and_delete(path, no_input=False):
ok_to_delete = True
else:
question = (
"You've downloaded {} before. Is it okay to delete and re-download it?"
).format(path)
f"You've downloaded {path} before. Is it okay to delete and re-download it?"
)

ok_to_delete = read_user_yes_no(question, 'yes')

Expand Down
3 changes: 1 addition & 2 deletions cookiecutter/zipfile.py
Expand Up @@ -63,8 +63,7 @@ def unzip(zip_uri, is_url, clone_to_dir='.', no_input=False, password=None):
first_filename = zip_file.namelist()[0]
if not first_filename.endswith('/'):
raise InvalidZipRepository(
'Zip repository {} does not include '
'a top-level directory'.format(zip_uri)
f"Zip repository {zip_uri} does not include a top-level directory"
)

# Construct the final target directory
Expand Down

0 comments on commit fbf7d4c

Please sign in to comment.