From 64b644a490444d1e4fe13ddeaacba2d069631705 Mon Sep 17 00:00:00 2001 From: James Curtin Date: Sat, 8 Jun 2019 12:48:10 -0400 Subject: [PATCH] Add black formatting --- .travis.yml | 1 - cookiecutter.json | 2 +- tasks.py | 11 ++++++----- {{cookiecutter.app_name}}/.isort.cfg | 2 -- {{cookiecutter.app_name}}/.travis.yml | 6 ++---- {{cookiecutter.app_name}}/Pipfile | 3 +-- {{cookiecutter.app_name}}/README.rst | 9 +++++++-- {{cookiecutter.app_name}}/requirements/dev.txt | 4 ++-- {{cookiecutter.app_name}}/setup.cfg | 10 +++++++++- .../{{cookiecutter.app_name}}/commands.py | 17 ++++++++++++----- 10 files changed, 40 insertions(+), 25 deletions(-) delete mode 100644 {{cookiecutter.app_name}}/.isort.cfg diff --git a/.travis.yml b/.travis.yml index f4e9fa4..96b9cef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ dist: xenial language: python python: - - 3.5 - 3.6 - 3.7 diff --git a/cookiecutter.json b/cookiecutter.json index a0da127..f43940c 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -6,6 +6,6 @@ "app_name": "{{cookiecutter.project_name.lower().replace('-', '_').replace(' ', '_')}}", "project_short_description": "A flasky app.", "use_pipenv": ["no", "yes"], - "python_version": ["3.7", "3.6", "3.5"], + "python_version": ["3.7", "3.6"], "node_version": ["12", "10", "8"] } diff --git a/tasks.py b/tasks.py index ec347b6..b43973d 100644 --- a/tasks.py +++ b/tasks.py @@ -41,9 +41,12 @@ def clean(ctx): print('App directory does not exist. Skipping.') -def _run_flask_command(ctx, command): +def _run_flask_command(ctx, command, *args): os.chdir(COOKIE) - ctx.run('flask {0}'.format(command), echo=True) + flask_command = 'flask {0}'.format(command) + if args: + flask_command = '{0} {1}'.format(flask_command, ' '.join(args)) + ctx.run(flask_command, echo=True) @task(pre=[clean, build]) @@ -55,9 +58,7 @@ def test(ctx): os.chdir(COOKIE) shutil.copyfile(os.path.join(COOKIE, '.env.example'), os.path.join(COOKIE, '.env')) - os.environ["FLASK_ENV"] = "production" - os.environ["FLASK_DEBUG"] = "0" - _run_flask_command(ctx, 'lint') + _run_flask_command(ctx, 'lint', '--check') _run_flask_command(ctx, 'test') diff --git a/{{cookiecutter.app_name}}/.isort.cfg b/{{cookiecutter.app_name}}/.isort.cfg deleted file mode 100644 index 8913d96..0000000 --- a/{{cookiecutter.app_name}}/.isort.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[settings] -line_length=120 diff --git a/{{cookiecutter.app_name}}/.travis.yml b/{{cookiecutter.app_name}}/.travis.yml index 6e25e70..b345de7 100644 --- a/{{cookiecutter.app_name}}/.travis.yml +++ b/{{cookiecutter.app_name}}/.travis.yml @@ -1,11 +1,9 @@ # Config file for automatic testing at travis-ci.org +dist: xenial language: python env: - FLASK_APP=autoapp.py FLASK_DEBUG=1 python: - - 2.7 - - 3.4 - - 3.5 - 3.6 - 3.7 install: @@ -16,5 +14,5 @@ install: before_script: - npm run lint - npm run build - - flask lint + - flask lint --check script: flask test diff --git a/{{cookiecutter.app_name}}/Pipfile b/{{cookiecutter.app_name}}/Pipfile index 24e1b57..6946eca 100644 --- a/{{cookiecutter.app_name}}/Pipfile +++ b/{{cookiecutter.app_name}}/Pipfile @@ -53,12 +53,11 @@ factory-boy = "==2.12.*" pdbpp = "==0.10.0" # Lint and code style +black = "==19.3b0" flake8 = "==3.7.7" flake8-blind-except = "==0.1.1" flake8-debugger = "==3.1.0" flake8-docstrings = "==1.3.0" flake8-isort = "==2.7.0" -flake8-quotes = "==2.0.1" isort = "==4.3.20" pep8-naming = "==0.8.2" - diff --git a/{{cookiecutter.app_name}}/README.rst b/{{cookiecutter.app_name}}/README.rst index ce9b0e4..dc15fac 100644 --- a/{{cookiecutter.app_name}}/README.rst +++ b/{{cookiecutter.app_name}}/README.rst @@ -57,13 +57,18 @@ To open the interactive shell, run :: By default, you will have access to the flask ``app``. -Running Tests -------------- +Running Tests/Linter +-------------------- To run all tests, run :: flask test +To run the linter, run :: + + flask lint + +The ``lint`` command will attempt to fix any linting/style errors in the code. If you only want to know if the code will pass CI and do not wish for the linter to make changes, add the ``--check`` argument. Migrations ---------- diff --git a/{{cookiecutter.app_name}}/requirements/dev.txt b/{{cookiecutter.app_name}}/requirements/dev.txt index 3417eb9..5054d96 100644 --- a/{{cookiecutter.app_name}}/requirements/dev.txt +++ b/{{cookiecutter.app_name}}/requirements/dev.txt @@ -8,11 +8,11 @@ factory-boy==2.11.1 pdbpp==0.10.0 # Lint and code style -flake8==3.5.0 +black==19.3b0 +flake8==3.7.7 flake8-blind-except==0.1.1 flake8-debugger==3.1.0 flake8-docstrings==1.3.0 flake8-isort==2.5 -flake8-quotes==1.0.0 isort==4.3.4 pep8-naming==0.7.0 diff --git a/{{cookiecutter.app_name}}/setup.cfg b/{{cookiecutter.app_name}}/setup.cfg index f5261ad..97ac5d6 100644 --- a/{{cookiecutter.app_name}}/setup.cfg +++ b/{{cookiecutter.app_name}}/setup.cfg @@ -1,3 +1,11 @@ [flake8] -ignore = D401 +ignore = D401,D202,E226,E302,E41 max-line-length=120 +exclude = migrations/* +max-complexity = 10 + +[isort] +line_length=88 +multi_line_output=3 +skip=migrations/* +include_trailing_comma=true diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/commands.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/commands.py index a6679d6..eac7ccd 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/commands.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/commands.py @@ -23,11 +23,12 @@ def test(): @click.command() -@click.option('-f', '--fix-imports', default=False, is_flag=True, +@click.option('-f', '--fix-imports', default=True, is_flag=True, help='Fix imports using isort, before linting') -def lint(fix_imports): - """Lint and check code style with flake8 and isort.""" - skip = ['node_modules', 'requirements'] +@click.option('-c', '--check', default=False, is_flag=True, help="Don't make any changes to files, just confirm they are formatted correctly") +def lint(fix_imports, check): + """Lint and check code style with black, flake8 and isort.""" + skip = ['node_modules', 'requirements', 'migrations'] root_files = glob('*.py') root_directories = [ name for name in next(os.walk('.'))[1] if not name.startswith('.')] @@ -42,8 +43,14 @@ def lint(fix_imports): if rv != 0: exit(rv) + isort_args = ["-rc"] + black_args = [] + if check: + isort_args.append('-c') + black_args.append('--check') if fix_imports: - execute_tool('Fixing import order', 'isort', '-rc') + execute_tool('Fixing import order', 'isort', *isort_args) + execute_tool('Formatting style', 'black', *black_args) execute_tool('Checking code style', 'flake8')