これは何? †
- プロジェクトテンプレートのフレームワーク
- 似たものとして、Github Template がある
- Cookiecutter を使うと、テンプレートに対して簡単なキーワード置換を行うことができる
インストール †
$ sudo apt install python3-pip
$ sudo pip3 install cookiecutter
既存の有名なテンプレート †
- https://github.com/audreyfeldroy/cookiecutter-pypackage
- https://github.com/pydanny/cookiecutter-django
- https://github.com/pytest-dev/cookiecutter-pytest-plugin
$ cookiecutter https://github.com/audreyfeldroy/cookiecutter-pypackage
full_name [Audrey Roy Greenfeld]: Atsushi Hondoh
email [audreyr@example.com]: atsushi@example.com
github_username [audreyr]: atsushi
project_name [Python Boilerplate]: MyPyPkg
project_slug [mypypkg]:
project_short_description [Python Boilerplate contains all the boilerplate you need to create a Python package.]:
pypi_username [atsushi]:
version [0.1.0]:
use_pytest [n]:
use_black [n]:
use_pypi_deployment_with_travis [y]:
add_pyup_badge [n]:
Select command_line_interface:
1 - Click
2 - Argparse
3 - No command-line interface
Choose from 1, 2, 3 [1]: 2
create_author_file [y]:
Select open_source_license:
1 - MIT license
2 - BSD license
3 - ISC license
4 - Apache Software License 2.0
5 - GNU General Public License v3
6 - Not open source
Choose from 1, 2, 3, 4, 5, 6 [1]:
$ tree mypypkg/
mypypkg/
├── AUTHORS.rst
├── CONTRIBUTING.rst
├── HISTORY.rst
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.rst
├── docs
│ ├── Makefile
│ ├── authors.rst
│ ├── conf.py
│ ├── contributing.rst
│ ├── history.rst
│ ├── index.rst
│ ├── installation.rst
│ ├── make.bat
│ ├── readme.rst
│ └── usage.rst
├── mypypkg
│ ├── __init__.py
│ ├── cli.py
│ └── mypypkg.py
├── requirements_dev.txt
├── setup.cfg
├── setup.py
├── tests
│ ├── __init__.py
│ └── test_mypypkg.py
└── tox.ini
ほーほー、うまくプロジェクトができてる。
自分でテンプレートを作る †
- テンプレートのテンプレートがある
$ cookiecutter https://github.com/eviweb/cookiecutter-template
full_name [Your name]:
email [Your address email (eg. you@example.com)]:
github_username [Your github username]:
project_name [Name of the project]: cookeicutter-at-pypackage
project_slug [cookiecutter-cookeicutter-at-pypackage]:
project_short_description [A short description of the project]:
release_date [2021-11-01]:
version [0.1.0]:
copy_hooks [no]:
$ tree cookiecutter-cookeicutter-at-pypackage/
cookiecutter-cookeicutter-at-pypackage/
├── CHANGELOG.md
├── LICENSE
├── README.md
├── VERSION
├── cookiecutter.json
├── pytest.ini
├── requirements-dev.txt
├── tests
│ ├── __init__.py
│ └── test_bake_project.py
└── {{cookiecutter.project_slug}}
2 directories, 9 files
- {{cookiecutter.project_slug}} 以下が展開されるプロジェクトになる
- cookiecutter {template} の質問の内容は cookiecutter.json
{
"full_name": "Your name",
"email": "Your address email (eq. you@example.com)",
"github_username": "Your github username",
"project_name": "Name of the project",
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
"project_short_description": "A short description of the project",
"release_date": "{% now 'local' %}",
"version": "0.1.0",
"_extensions": ["jinja2_time.TimeExtension"]
}
- 設定内容は、{{cookiecutter.xxx}} で参照できる
- テンプレートは、ディレクトリの他 zip や Git レポジトリを使える
Python