cookiecutter-flit

A Cookiecutter template for a Flit package
git clone git://git.danielmoch.com/cookiecutter-flit.git
Log | Files | Refs | LICENSE

commit 168faf22f5eddb7ee08e61387d55c9f70cad1ee2
parent 84bdb57f9e87a826284586b5d859d6bad70e29bd
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Mon, 20 Aug 2018 20:15:36 -0400

Improvements

1. Always use pytest
2. Flit initialization improvements
3. Immediately initialize pipenv

Diffstat:
Mcookiecutter.json | 2+-
Mhooks/post_gen_project.py | 11+++++------
A{{cookiecutter.project_slug}}/.gitignore | 4++++
M{{cookiecutter.project_slug}}/Makefile | 9+++++++--
M{{cookiecutter.project_slug}}/Pipfile | 5++---
M{{cookiecutter.project_slug}}/pyproject.toml | 5+++++
D{{cookiecutter.project_slug}}/tests/__init__.py | 3---
M{{cookiecutter.project_slug}}/tests/test_{{cookiecutter.project_slug}}.py | 26++------------------------
M{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/__init__.py | 16++++++++++++++--
9 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/cookiecutter.json b/cookiecutter.json @@ -8,7 +8,7 @@ "project_short_description": "Flit Boilerplate contains all the boilerplate you need to create a Python Flit package.", "pypi_username": "{{ cookiecutter.github_username }}", "version": "0.1.0", - "use_pytest": "y", "use_pipenv": "y", + "script_entrypoint": "y", "open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"] } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py @@ -1,5 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os +import subprocess PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -10,13 +11,11 @@ def remove_file(filepath): if __name__ == '__main__': - if '{{ cookiecutter.use_pytest }}' == 'y': - remove_file('tests/__init__.py') - else: - remove_file('pytest.ini') - if 'Not open source' == '{{ cookiecutter.open_source_license }}': remove_file('LICENSE') if '{{ cookiecutter.use_pipenv }}' == 'n': remove_file('Pipfile') + else: + subprocess.run(['pipenv', 'lock', '--dev']) + subprocess.run(['pipenv', 'sync', '--dev']) diff --git a/{{cookiecutter.project_slug}}/.gitignore b/{{cookiecutter.project_slug}}/.gitignore @@ -0,0 +1,4 @@ +dist/ +.coverage +.pytest_cache/ +htmlcov/ diff --git a/{{cookiecutter.project_slug}}/Makefile b/{{cookiecutter.project_slug}}/Makefile @@ -25,10 +25,12 @@ endef export PRINT_HELP_PYSCRIPT BROWSER := python -c "$$BROWSER_PYSCRIPT" + +# Comment out the following line to run outside of Pipenv {%- if cookiecutter.use_pipenv == 'y' %} PIPENV := pipenv run {%- else %} -PIPENV := +# PIPENV := pipenv run {%- endif %} help: @@ -61,7 +63,7 @@ test: ## run tests quickly with the default Python $(PIPENV) python -m pytest coverage: ## check code coverage quickly with the default Python - $(PIPENV) coverage run --source {{ cookiecutter.project_slug }} -m pytest + $(PIPENV) python -m pytest $(PIPENV) coverage report -m $(PIPENV) coverage html $(BROWSER) htmlcov/index.html @@ -75,3 +77,6 @@ dist: clean ## builds source and wheel package install: clean ## install the package to the active Python's site-packages $(PIPENV) flit install + +debug: install ## debug the package from site packages + $(PIPENV) pudb3 `$(PIPENV) which {{ cookiecutter.project_slug }}` install diff --git a/{{cookiecutter.project_slug}}/Pipfile b/{{cookiecutter.project_slug}}/Pipfile @@ -6,11 +6,10 @@ name = "pypi" [packages] [dev-packages] -{%- if cookiecutter.use_pytest == 'y' %} -pytest = "*" +pytest = "<3.7.0" pytest-cov = "*" pytest-pylint = "*" -{%- endif %} +pudb = "*" [requires] python_version = "3.7" diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml @@ -19,3 +19,8 @@ classifiers = ["License :: OSI Approved :: Apache Software License"] {% elif cookiecutter.open_source_license == 'GNU General Public License v3' -%} classifiers = ["License :: OSI Approved :: GNU General Public License v3 (GPLv3)"] {% endif %} +{% if cookiecutter.script_entrypoint == 'y' -%} + +[tool.flit.scripts] +{{ cookiecutter.project_slug }} = "{{cookiecutter.project_slug}}:main" +{% endif %} diff --git a/{{cookiecutter.project_slug}}/tests/__init__.py b/{{cookiecutter.project_slug}}/tests/__init__.py @@ -1,3 +0,0 @@ -# -*- coding: utf-8 -*- - -"""Unit test package for {{ cookiecutter.project_slug }}.""" diff --git a/{{cookiecutter.project_slug}}/tests/test_{{cookiecutter.project_slug}}.py b/{{cookiecutter.project_slug}}/tests/test_{{cookiecutter.project_slug}}.py @@ -1,17 +1,10 @@ # -*- coding: utf-8 -*- -"""Tests for `{{ cookiecutter.project_slug }}` package.""" +"""Tests for {{ cookiecutter.project_slug }} module""" -{% if cookiecutter.use_pytest == 'y' -%} import pytest -{% else %} -import unittest -{%- endif %} - -from {{ cookiecutter.project_slug }} import {{ cookiecutter.project_slug }} - -{%- if cookiecutter.use_pytest == 'y' %} +import {{ cookiecutter.project_slug }} @pytest.fixture def response(): @@ -27,18 +20,3 @@ def test_content(response): """Sample pytest test function with the pytest fixture as an argument.""" # from bs4 import BeautifulSoup # assert 'GitHub' in BeautifulSoup(response.content).title.string -{%- else %} - - -class Test{{ cookiecutter.project_slug|title }}(unittest.TestCase): - """Tests for `{{ cookiecutter.project_slug }}` package.""" - - def setUp(self): - """Set up test fixtures, if any.""" - - def tearDown(self): - """Tear down test fixtures, if any.""" - - def test_000_something(self): - """Test something.""" -{%- endif %} diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/__init__.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/__init__.py @@ -1,8 +1,20 @@ # -*- coding: utf-8 -*- """{{ cookiecutter.project_short_description }}""" +{% if cookiecutter.script_entrypoint == 'y' -%} +import argparse + +from . import {{ cookiecutter.project_slug }} +{% endif %} __author__ = '{{ cookiecutter.full_name }}' -__email__ = '{{ cookiecutter.email }}' __version__ = '{{ cookiecutter.version }}' -__copyright__ = 'Copyright (c) {% now 'local', '%Y' %}, {{ cookiecutter.full_name }}' +{% if cookiecutter.script_entrypoint == 'y' -%} + +def main(): + """Main {{ cookiecutter.project_slug }} entrypoint""" + parser = argparse.ArgumentParser() + parser.add_argument( + '-v', action='version', + version='%(prog)s {}'.format(__version__)) +{% endif %}