Skip to content

Commit

Permalink
Added: Black formatting verification for whole project
Browse files Browse the repository at this point in the history
* Enable black formatting

* Fixid commas cookiecutter#1368

Co-authored-by: Andrey Shpak <ashpak@ashpak.ru>
  • Loading branch information
ssbarnea and insspb authored Apr 19, 2020
1 parent c067ab6 commit 5671b43
Show file tree
Hide file tree
Showing 54 changed files with 626 additions and 900 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---
repos:
- repo: https://github.com/python/black.git
rev: 19.10b0
hooks:
- id: black
language_version: python3
exclude: ^(tests\/hooks-abort-render\/hooks|docs\/HelloCookieCutter1)
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
Expand Down
103 changes: 60 additions & 43 deletions cookiecutter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
UnknownExtension,
InvalidZipRepository,
RepositoryNotFound,
RepositoryCloneFailed
RepositoryCloneFailed,
)


Expand Down Expand Up @@ -51,57 +51,77 @@ def validate_extra_context(ctx, param, value):
@click.argument(u'template')
@click.argument(u'extra_context', nargs=-1, callback=validate_extra_context)
@click.option(
u'--no-input', is_flag=True,
u'--no-input',
is_flag=True,
help=u'Do not prompt for parameters and only use cookiecutter.json '
u'file content',
u'file content',
)
@click.option(
u'-c', u'--checkout',
help=u'branch, tag or commit to checkout after git clone',
u'-c', u'--checkout', help=u'branch, tag or commit to checkout after git clone',
)
@click.option(
u'--directory',
help=u'Directory within repo that holds cookiecutter.json file '
u'for advanced repositories with multi templates in it',
u'for advanced repositories with multi templates in it',
)
@click.option(
'-v', '--verbose',
is_flag=True, help='Print debug information', default=False
'-v', '--verbose', is_flag=True, help='Print debug information', default=False
)
@click.option(
u'--replay', is_flag=True,
u'--replay',
is_flag=True,
help=u'Do not prompt for parameters and only use information entered '
u'previously',
u'previously',
)
@click.option(
u'-f', u'--overwrite-if-exists', is_flag=True,
help=u'Overwrite the contents of the output directory if it already exists'
u'-f',
u'--overwrite-if-exists',
is_flag=True,
help=u'Overwrite the contents of the output directory if it already exists',
)
@click.option(
u'-s', u'--skip-if-file-exists', is_flag=True,
help=u'Skip the files in the corresponding directories if they already '
u'exist', default=False
u'-s',
u'--skip-if-file-exists',
is_flag=True,
help=u'Skip the files in the corresponding directories if they already ' u'exist',
default=False,
)
@click.option(
u'-o', u'--output-dir', default='.', type=click.Path(),
help=u'Where to output the generated project dir into'
u'-o',
u'--output-dir',
default='.',
type=click.Path(),
help=u'Where to output the generated project dir into',
)
@click.option(
u'--config-file', type=click.Path(), default=None,
help=u'User configuration file'
u'--config-file', type=click.Path(), default=None, help=u'User configuration file'
)
@click.option(
u'--default-config', is_flag=True,
help=u'Do not load a config file. Use the defaults instead'
u'--default-config',
is_flag=True,
help=u'Do not load a config file. Use the defaults instead',
)
@click.option(
u'--debug-file', type=click.Path(), default=None,
u'--debug-file',
type=click.Path(),
default=None,
help=u'File to be used as a stream for DEBUG logging',
)
def main(
template, extra_context, no_input, checkout, verbose,
replay, overwrite_if_exists, output_dir, config_file,
default_config, debug_file, directory, skip_if_file_exists):
template,
extra_context,
no_input,
checkout,
verbose,
replay,
overwrite_if_exists,
output_dir,
config_file,
default_config,
debug_file,
directory,
skip_if_file_exists,
):
"""Create a project from a Cookiecutter project template (TEMPLATE).
Cookiecutter is free and open source software, developed and managed by
Expand All @@ -114,14 +134,13 @@ def main(
click.echo(click.get_current_context().get_help())
sys.exit(0)

configure_logger(
stream_level='DEBUG' if verbose else 'INFO',
debug_file=debug_file,
)
configure_logger(stream_level='DEBUG' if verbose else 'INFO', debug_file=debug_file)

try:
cookiecutter(
template, checkout, no_input,
template,
checkout,
no_input,
extra_context=extra_context,
replay=replay,
overwrite_if_exists=overwrite_if_exists,
Expand All @@ -130,26 +149,24 @@ def main(
default_config=default_config,
password=os.environ.get('COOKIECUTTER_REPO_PASSWORD'),
directory=directory,
skip_if_file_exists=skip_if_file_exists
skip_if_file_exists=skip_if_file_exists,
)
except (OutputDirExistsException,
InvalidModeException,
FailedHookException,
UnknownExtension,
InvalidZipRepository,
RepositoryNotFound,
RepositoryCloneFailed) as e:
except (
OutputDirExistsException,
InvalidModeException,
FailedHookException,
UnknownExtension,
InvalidZipRepository,
RepositoryNotFound,
RepositoryCloneFailed,
) as e:
click.echo(e)
sys.exit(1)
except UndefinedVariableInTemplate as undefined_err:
click.echo('{}'.format(undefined_err.message))
click.echo('Error message: {}'.format(undefined_err.error.message))

context_str = json.dumps(
undefined_err.context,
indent=4,
sort_keys=True
)
context_str = json.dumps(undefined_err.context, indent=4, sort_keys=True)
click.echo('Context: {}'.format(context_str))
sys.exit(1)

Expand Down
3 changes: 1 addition & 2 deletions cookiecutter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def get_config(config_path):
yaml_dict = poyo.parse_string(file_handle.read())
except poyo.exceptions.PoyoException as e:
raise InvalidConfiguration(
'Unable to parse YAML file {}. Error: {}'
''.format(config_path, e)
'Unable to parse YAML file {}. Error: {}' ''.format(config_path, e)
)

config_dict = merge_configs(DEFAULT_CONFIG, yaml_dict)
Expand Down
10 changes: 2 additions & 8 deletions cookiecutter/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def __init__(self, **kwargs):
extensions = default_extensions + self._read_extensions(context)

try:
super(ExtensionLoaderMixin, self).__init__(
extensions=extensions,
**kwargs
)
super(ExtensionLoaderMixin, self).__init__(extensions=extensions, **kwargs)
except ImportError as err:
raise UnknownExtension('Unable to load extension: {}'.format(err))

Expand Down Expand Up @@ -67,7 +64,4 @@ def __init__(self, **kwargs):
Also loading extensions defined in cookiecutter.json's _extensions key.
"""
super(StrictEnvironment, self).__init__(
undefined=StrictUndefined,
**kwargs
)
super(StrictEnvironment, self).__init__(undefined=StrictUndefined, **kwargs)
2 changes: 2 additions & 0 deletions cookiecutter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UnknownTemplateDirException(CookiecutterException):
Raised when Cookiecutter cannot determine which directory is the project
template, e.g. more than one dir appears to be a template dir.
"""

# unused locally


Expand All @@ -37,6 +38,7 @@ class MissingProjectDir(CookiecutterException):
Raised during cleanup when remove_repo() can't find a generated project
directory inside of a repo.
"""

# unused locally


Expand Down
2 changes: 2 additions & 0 deletions cookiecutter/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import string

try:
# Python 3.6 and above
from secrets import choice
Expand Down Expand Up @@ -40,6 +41,7 @@ def random_ascii_string(length, punctuation=False):
else:
corpus = string.ascii_letters
return "".join(choice(corpus) for _ in range(length))

environment.globals.update(random_ascii_string=random_ascii_string)


Expand Down
Loading

0 comments on commit 5671b43

Please sign in to comment.