Skip to content

Commit

Permalink
Added uuid extension to be able to generate uuids (cookiecutter#1493)
Browse files Browse the repository at this point in the history
* Update extensions.py

* Added tests and full function

* fixed bug

* fixed call

* Fixed doc string

* linting
  • Loading branch information
jonaswre authored and cagonza6 committed Jun 16, 2021
1 parent f58e784 commit 71ebf03
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions cookiecutter/environment.py
Expand Up @@ -26,6 +26,7 @@ def __init__(self, **kwargs):
'cookiecutter.extensions.JsonifyExtension',
'cookiecutter.extensions.RandomStringExtension',
'cookiecutter.extensions.SlugifyExtension',
'cookiecutter.extensions.UUIDExtension',
'jinja2_time.TimeExtension',
]
extensions = default_extensions + self._read_extensions(context)
Expand Down
15 changes: 15 additions & 0 deletions cookiecutter/extensions.py
@@ -1,6 +1,7 @@
"""Jinja2 extensions."""
import json
import string
import uuid
from secrets import choice

from jinja2.ext import Extension
Expand Down Expand Up @@ -49,3 +50,17 @@ def slugify(value, **kwargs):
return pyslugify(value, **kwargs)

environment.filters['slugify'] = slugify


class UUIDExtension(Extension):
"""Jinja2 Extension to generate uuid4 string."""

def __init__(self, environment):
"""Jinja2 Extension constructor."""
super(UUIDExtension, self).__init__(environment)

def uuid4():
"""Generate UUID4."""
return str(uuid.uuid4())

environment.globals.update(uuid4=uuid4)
21 changes: 21 additions & 0 deletions docs/advanced/template_extensions.rst
Expand Up @@ -32,6 +32,7 @@ By default Cookiecutter includes the following extensions:
- ``cookiecutter.extensions.JsonifyExtension``
- ``cookiecutter.extensions.RandomStringExtension``
- ``cookiecutter.extensions.SlugifyExtension``
- ``cookiecutter.extensions.UUIDExtension``
- ``jinja2_time.TimeExtension``

Jsonify extension
Expand Down Expand Up @@ -109,3 +110,23 @@ be passed to `slugify()`.
.. _`now`: https://github.com/hackebrot/jinja2-time#now-tag
.. _`jinja2_time.TimeExtension`: https://github.com/hackebrot/jinja2-time
.. _`python-slugify`: https://github.com/un33k/python-slugify

UUID4 extension
~~~~~~~~~~~~~~~~~~~~~~~

*New in Cookiecutter 1.x*

The ``cookiecutter.extensions.UUIDExtension`` extension provides a ``uuid4()``
method in templates that generates a uuid4.

Generate a uuid4 string:

.. code-block:: jinja
{{ uuid4() }}
Outputs:

.. code-block:: text
83b5de62-31b4-4a1e-83fa-8c548de65a11
@@ -0,0 +1 @@
{{ uuid4() }}
16 changes: 16 additions & 0 deletions tests/test_default_extensions.py
Expand Up @@ -3,6 +3,7 @@

import freezegun
import pytest
import uuid

from cookiecutter.main import cookiecutter

Expand Down Expand Up @@ -46,3 +47,18 @@ def test_jinja2_slugify_extension(tmpdir):
)

assert os.path.basename(project_dir) == "it-s-slugified-foobar"


def test_jinja2_uuid_extension(tmpdir):
"""Verify Jinja2 uuid extension work correctly."""
project_dir = cookiecutter(
'tests/test-extensions/default/', no_input=True, output_dir=str(tmpdir)
)
changelog_file = os.path.join(project_dir, 'id')
assert os.path.isfile(changelog_file)

with open(changelog_file, 'r', encoding='utf-8') as f:
changelog_lines = f.readlines()

uuid.UUID(changelog_lines[0], version=4)
assert True
1 change: 1 addition & 0 deletions tests/test_environment.py
Expand Up @@ -22,3 +22,4 @@ def test_env_should_come_with_default_extensions():
assert 'cookiecutter.extensions.JsonifyExtension' in env.extensions
assert 'cookiecutter.extensions.RandomStringExtension' in env.extensions
assert 'cookiecutter.extensions.SlugifyExtension' in env.extensions
assert 'cookiecutter.extensions.UUIDExtension' in env.extensions

0 comments on commit 71ebf03

Please sign in to comment.