Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cookiecutter: Come to the Dark Side! - EuroPyth...

Cookiecutter: Come to the Dark Side! - EuroPython 2015

It requires great diligence and occasionally gets pretty cumbersome if you start new tools on a regular basis.

Why not just use a template for it? Cookiecutter is a CLI tool written in pure Python that enables you to do so. Not only is it working for Python code, but also markdown formats and even other programming languages. We will talk about the ideas behind Cookiecutter and go over how you can create your very own template, so you and others can benefit from your experience.

You can find a recording of my talk on the EuroPython Youtube Channel at https://youtu.be/yxXio-jzAMQ

For more information on this talk please see the official outline at https://ep2015.europython.eu/conference/talks/come-to-the-dark-side-we-have-a-whole-bunch-of-cookiecutters

Raphael Pierzina

July 23, 2015
Tweet

More Decks by Raphael Pierzina

Other Decks in Programming

Transcript

  1. Raphael Pierzina • Background in 3D Animation • Core Developer

    of Cookiecutter • Volunteer for Adopt Pytest Month • Technical Reviewer on a Kivy Book
  2. A command-line utility that creates projects from cookiecutters (project templates).

    E.g. Python package projects, jQuery plugin projects. Artwork by Audrey Roy Greenfeld
  3. Features • Cross-platform: Windows, Mac, and Linux are officially supported.

    • Free software distributed under the terms of the BSD license • Works with Python 2.7, 3.3, 3.4, and PyPy. • Unicode support for named platforms and Python versions • Project templates can be in any programming language or markup format
  4. Technologies • 100% of templating is done with Jinja2 •

    Support for Git and Mercurial • Sphinx Docs hosted on Read the Docs • Running Tox and Pytest (at 96% test coverage) • CLI and user prompting via Click
  5. Community The core committer team is @audreyr, @pydanny, @michaeljoseph, @pfmoore,

    and @hackebrot. We welcome you and invite you to participate. Up to date 61 contributors and template authors in total.
  6. –Audrey Roy Greenfeld “After EuroPython 2013, Danny and I both

    came away from the conference with a renewed spirit and enthusiasm to sprint on open source.”
  7. –Audrey Roy Greenfeld “I had a lot of ideas for

    packaging various snippets of my existing code as new Python libraries. But I found that I was doing a lot of repetitive work every time I created a new Python package. ”
  8. Create Template Root $ git clone https://github.com/hackebrot/cookiedozer.git $ cd cookiedozer

    $ tree -a -L 1 . ├── .git ├── .gitignore ├── LICENSE └── README.md
  9. Python Package $ mkdir -p {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}} $ cd $_ $

    touch __init__.py $ touch {{cookiecutter.repo_name}}.py $ touch {{cookiecutter.app_class_name}}.kv $ touch main.py
  10. __init__.py # -*- coding: utf-8 -*- __author__ = '{{cookiecutter.full_name}}' __email__

    = '{{cookiecutter.email}}' __version__ = '{{cookiecutter.version}}'
  11. {{cookiecutter.repo_name}}.py # -*- coding: utf-8 -*- """ {{cookiecutter.repo_name}} ============================ The

    root of :class:`{{cookiecutter.app_class_name}}` is created from the kv file. """ import kivy kivy.require({{cookiecutter.kivy_version}}) from kivy.app import App class {{cookiecutter.app_class_name}}(App): """Basic Kivy App with a user defined title.""" title = '{{cookiecutter.app_title}}' def build(self): return self.root
  12. main.py #!/usr/bin/env python # -*- coding: utf-8 -*- from {{cookiecutter.repo_name}}

    import {{cookiecutter.app_class_name}} def main(): {{cookiecutter.app_class_name}}().run() if __name__ == '__main__': main()
  13. cookiecutter.json { "full_name": "Raphael Pierzina", "email": "[email protected]", "app_title": "Hello World",

    "app_class_name": "HelloWorldApp", "repo_name": "helloworld", "version": "0.1.0", "kivy_version": "1.8.0" }
  14. The ability to destroy a planet is insignificant next to

    the power of Cookiecutter. Advanced Usage
  15. Templates in Variables { "full_name": "Raphael Pierzina", "email": "[email protected]", "app_title":

    "Hello World", "app_class_name": "{{cookiecutter.app_title|replace(' ', '')}}App", "repo_name": "{{cookiecutter.app_title|replace(' ', '')|lower}}", "version": "0.1.0", "kivy_version": "1.8.0" }
  16. post_gen_project.py #!/usr/bin/env python # -*- coding: utf-8 -*- """Rename the

    generated kv file to be compatible with the original kivy kv file detection of `App.load_kv()`. """ import os package_dir = '{{cookiecutter.repo_name}}' old_kv_file = os.path.join(package_dir, '{{cookiecutter.app_class_name}}.kv') lower_app_class_name = '{{cookiecutter.app_class_name}}'.lower() if (lower_app_class_name.endswith('app')): lower_app_class_name = lower_app_class_name[:-3] new_kv_file = os.path.join(package_dir, '{}.kv'.format(lower_app_class_name)) os.rename(old_kv_file, new_kv_file)
  17. Current Layout $ tree . ├── cookiecutter.json ├── {{cookiecutter.repo_name}} │

    ├── {{cookiecutter.repo_name}} │ │ ├── {{cookiecutter.app_class_name}}.kv │ │ ├── {{cookiecutter.repo_name}}.py │ │ ├── __init__.py │ │ └── main.py │ ├── LICENSE │ └── README.rst ├── hooks │ └── post_gen_project.py ├── LICENSE └── README.rst
  18. Test Suite $ mkdir \{\{cookiecutter.repo_name\}\}/tests $ cd $_ $ touch

    conftest.py $ touch test_{{cookiecutter.repo_name}}.py
  19. conftest.py # -*- coding: utf-8 -*- import pytest @pytest.fixture(scope="session") def

    app(request): """Uses the InteractiveLauncher to provide access to an app instance. The finalizer stops the launcher once the tests are finished. Returns: :class:`{{cookiecutter.app_class_name}}`: App instance """ from kivy.interactive import InteractiveLauncher from {{cookiecutter.repo_name}}.{{cookiecutter.repo_name}} import {{cookiecutter.app_class_name}}
  20. test_{{cookiecutter.repo_name}}.py # -*- coding: utf-8 -*- def test_app_title(app): """Simply tests

    if the default app title meets the expectations. Args: app (:class:`{{cookiecutter.app_class_name}}`): Default app instance Raises: AssertionError: If the title does not match """ assert app.title == '{{cookiecutter.app_title}}'
  21. 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: {{cookiecutter.app_title}} > Author name(s): {{cookiecutter.full_name}} > Project version: {{cookiecutter.version}} > Project release [{{cookiecutter.version}}]: <ENTER> > Source file suffix [.rst]: <ENTER>
  22. ~/.cookiecutterrc default_context: full_name: "Raphael Pierzina" email: "[email protected]" github_username: "hackebrot" abbreviations:

    cookiedozer: https://github.com/hackebrot/cookiedozer.git gh: https://github.com/{0}.git
  23. 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 [[email protected]]: 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]:
  24. Choices in Context { "full_name": "Raphael Pierzina", "email": "[email protected]", "github_username":

    "hackebrot", "project_name": "Kivy Project", "minimum_kivy_version": "1.8.0", "orientation": ["all", "landscape", "portrait"] }
  25. Choice Prompt Select orientation: 1 - all 2 - landscape

    3 - portrait Choose from 1, 2, 3 [1]: 2
  26. Click Prompts def read_user_choice(var_name, options): """Prompt the user to choose

    from several options for the given variable. The first item will be returned if no input happens. :param str var_name: Variable as specified in the context :param list options: Sequence of options that are available to select from :return: Exactly one item of ``options`` that has been chosen by the user """ # Please see http://click.pocoo.org/4/api/#click.prompt if not isinstance(options, list): raise TypeError if not options: raise ValueError
  27. Click Prompts choice_map = OrderedDict( ('{}'.format(i), value) for i, value

    in enumerate(options, 1) ) choices = choice_map.keys() default = '1' choice_lines = ['{} - {}'.format(*c) for c in choice_map.items()] prompt = '\n'.join(( 'Select {}:'.format(var_name), '\n'.join(choice_lines), 'Choose from {}'.format(', '.join(choices)) )) user_choice = click.prompt( prompt, type=click.Choice(choices), default=default ) return choice_map[user_choice]
  28. Join us, and together we can rule the galaxy as

    Cookiecutterers. Template Contributions
  29. Templates 41 Templates Python 26 Python C C++ C# Common

    Lisp JS LaTeX/XeTeX Berkshelf-Vagrant HTML Scala 6502 Assembly