Cookiecutter - Python Glasgow Feb 2016

Cookiecutter - Python Glasgow Feb 2016

Cookiecutter is a command-line utility that helps you to start new projects following best practices of the community or simply based on your very own experience.

Authoring templates couldn't be any easier and you absolutely don't need to be an expert in Python. This talk briefly showcases how to do it and use Cookiecutter to greatly simplify the process of starting new projects.

F6e87b8993f94c06de18c85d1b3b7fb2?s=128

Raphael Pierzina

February 09, 2016
Tweet

Transcript

  1. Cookiecutter or what we can learn from templates

  2. Raphael Pierzina Python Glasgow - February, 2016

  3. Who am I? • Night owl, comic lover, overall geek

    • Software Engineer at FanDuel • Stuttgart ✈ Edinburgh
  4. / hackebrot @hackebrot

  5. Open Source Projects • Cookiecutter • Pytest and plugins •

    Kivy Blueprints • … a couple of smaller projects
  6. None
  7. What is Cookiecutter? • Command-line utility that helps you to

    start new projects following best practices of the community or simply based on your very own experience. • Renders templates based on user-input on the CLI • Project templates can be in any programming language or markup format
  8. What is Cookiecutter? • 100% Python - using click and

    jinja2 • Compatible with CPython 2.7, 3.3, 3.4, 3.5 and also PyPy • Available on OS X, Linux and Windows • Free software, permissive BSD license
  9. None
  10. Community • The core committer team is @audreyr, @pydanny, @michaeljoseph,

    @pfmoore, and @hackebrot • Close to 3k stars on GitHub, around 12k downloads per month on PyPI with 600 templates on GitHub • Talks at multiple conferences (PyCon ZA, EuroPython, …) • Open Source sponsor for PyCon US 2016
  11. Why Templates? • A great addition to user documentation that

    you can explore • Even in Python we sometimes do the same stuff over and over again • Writing scripts is really easy, polishing a package to upload it to PyPI can be tricky • Some packages require (or recommend) a certain project structure and naming convention
  12. Templates • Local templates (that you’ve downloaded or created) •

    Mercurial/Git repositories on BitBucket and GitHub
  13. Templates 54 “official” Templates Python 36 Python C C++ C#

    Common Lisp JS LaTeX/XeTeX Berkshelf-Vagrant HTML Scala 6502 Assembly Kotlin
  14. None
  15. Installation $ pip install cookiecutter # Recommended! $ brew install

    cookiecutter # OS X $ apt-get install cookiecutter # Ubuntu ...
  16. CLI or API • Python API to run cookiecutter from

    your own tools • Command-line interface using click # http://click.pocoo.org/5/python3/ export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8
  17. CLI Options Usage: cookiecutter [OPTIONS] TEMPLATE Create a project from

    a Cookiecutter project template (TEMPLATE). Options: -V, --version Show the version and exit. --no-input Do not prompt for parameters and only use cookiecutter.json file content -c, --checkout TEXT branch, tag or commit to checkout after git clone -v, --verbose Print debug information --replay Do not prompt for parameters and only use information entered previously -f, --overwrite-if-exists Overwrite the contents of the output directory if it already exists -o, --output-dir PATH Where to output the generated project dir into --config-file PATH User configuration file --default-config Do not load a config file. Use the defaults instead -h, --help Show this message and exit.
  18. Usage $ cookiecutter gh:pytest-dev/cookiecutter-pytest-plugin

  19. None
  20. Demo Using Cookiecutter

  21. cookiecutter.json { "full_name": "Raphael Pierzina", "email": "raphael@hackebrot.de", "github_username": "hackebrot", "plugin_name":

    "foobar", "module_name": "{{ cookiecutter.plugin_name|lower|replace('-', '_') }}", "short_description": "A simple plugin to use with Pytest", "version": "0.1.0", "pytest_version": "2.8.1", "year": "2015", "docs_tool": ["mkdocs", "sphinx", "none"], "license": ["MIT", "BSD-3", "GNU GPL v3.0", "Apache Software License 2.0"] }
  22. Template Structure cookiecutter-pytest-plugin/ !"" LICENSE !"" Makefile !"" README.md !""

    cookiecutter.json !"" docs # !"" contributor-guide # # !"" documentation.md # # $"" quickstart.md # !"" index.md # $"" user-guide # !"" publish-plugin.md # $"" quickstart.md !"" hooks # !"" post_gen_project.py # $"" pre_gen_project.py !"" mkdocs.yml
  23. Template Structure !"" pytest-{{cookiecutter.plugin_name}} # !"" LICENSE # !"" README.rst

    # !"" appveyor.yml # !"" mkdocs # # !"" index.md # # $"" mkdocs.yml # !"" pytest_{{cookiecutter.module_name}}.py # !"" setup.py
  24. Template Structure # !"" sphinxdocs # # !"" Makefile #

    # !"" _build # # !"" _static # # !"" _templates # # !"" conf.py # # !"" index.rst # # $"" make.bat # !"" tests # # !"" conftest.py # # $"" test_{{cookiecutter.module_name}}.py # $"" tox.ini
  25. Template Structure !"" tests # !"" conftest.py # $"" test_create_template.py

    $"" tox.ini 12 directories, 29 files
  26. Templated File name='pytest-{{cookiecutter.plugin_name}}', version='{{cookiecutter.version}}', author='{{cookiecutter.full_name}}', author_email='{{cookiecutter.email}}', maintainer='{{cookiecutter.full_name}}', maintainer_email='{{cookiecutter.email}}', license='{{cookiecutter.license}}',

  27. Templated File {% if cookiecutter.license == "MIT" -%} 'License ::

    OSI Approved :: MIT License', {%- elif cookiecutter.license == "BSD-3" -%} 'License :: OSI Approved :: BSD License', {%- elif cookiecutter.license == "GNU GPL v3.0" -%} 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', {%- elif cookiecutter.license == "Apache Software License 2.0" -%} 'License :: OSI Approved :: Apache Software License', {%- endif %}
  28. Sphinx Quickstart > Root path for the documentation [.]: <ENTER>

    > Separate source and build directories (y/n) [n]: y > Name prefix for templates and static dir [_]: <ENTER> > Project name: pytest-{{cookiecutter.plugin_name}} > Author name(s): {{cookiecutter.full_name}} > Project version: {{cookiecutter.version}} > Project release [{{cookiecutter.version}}]: <ENTER> > Source file suffix [.rst]: <ENTER>
  29. Jinja2 Template {% for item in commands.split(',') -%} {% set

    command = item.strip().lower() %} @cli.command('{{command}}', help="Meaningful message here") def {{command}}_cmd(*args, **kwargs): pass {% endfor %}
  30. Jinja2 Output @cli.command('foo', help="Meaningful message here") def foo_cmd(*args, **kwargs): pass

    @cli.command('bar', help="Meaningful message here") def bar_cmd(*args, **kwargs): pass @cli.command('helloworld', help="Meaningful message here") def helloworld_cmd(*args, **kwargs): pass
  31. Demo Creating a “HelloWorld” Template

  32. ~/.cookiecutterrc default_context: full_name: "Raphael Pierzina" email: "raphael@hackebrot.de" github_username: "hackebrot" abbreviations:

    cookiedozer: https://github.com/hackebrot/cookiedozer.git gh: https://github.com/{0}.git
  33. Showcase User Config $ cookiecutter gh:audreyr/cookiecutter-pypackage Cloning into 'cookiecutter-pypackage'... remote:

    Counting objects: 505, done. remote: Total 505 (delta 0), reused 0 (delta 0), pack-reused 505 Receiving objects: 100% (505/505), 77.43 KiB | 0 bytes/s, done. Resolving deltas: 100% (265/265), done. Checking connectivity... done. full_name [Raphael Pierzina]: email [raphael@hackebrot.de]: github_username [hackebrot]: project_name [Python Boilerplate]: Test User Config repo_name [boilerplate]: project_short_description [Python Boilerplate contains all the boilerplate you need to create a Python package.]: release_date [2015-01-11]: year [2015]: version [0.1.0]:
  34. None
  35. psst…cibopath

  36. cibopath $ cibopath search django cookiecutter-django................. https://github.com/pydanny/cookiecutter-django cookiecutter-django-cms............. https://github.com/palazzem/cookiecutter-django-cms cookiecutter-django-crud............

    https://github.com/wildfish/cookiecutter-django-crud cookiecutter-django-lborgav......... https://github.com/lborgav/cookiecutter-django cookiecutter-django-paas............ https://github.com/pbacterio/cookiecutter-django-paas cookiecutter-django-rest............ https://github.com/agconti/cookiecutter-django-rest cookiecutter-django-rest-framework.. https://github.com/jpadilla/cookiecutter-django-rest-framework cookiecutter-djangopackage.......... https://github.com/pydanny/cookiecutter-djangopackage cookiecutter-simple-django.......... https://github.com/marcofucci/cookiecutter-simple-django cookiecutter-wagtail................ https://github.com/torchbox/cookiecutter-wagtail wagtail-cookiecutter-foundation..... https://github.com/chrisdev/wagtail-cookiecutter-foundation
  37. Questions?

  38. pytest sprint 2016 https://www.indiegogo.com/projects/ python-testing-sprint-mid-2016#/

  39. https://speakerdeck.com/hackebrot

  40. / hackebrot @hackebrot