Skip to content

Commit

Permalink
Add a UUID cookiecutter extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed May 30, 2024
1 parent 25070c1 commit c2b7290
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/briefcase/integrations/cookiecutter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Jinja2 extensions."""

import uuid

from jinja2.ext import Extension


Expand Down Expand Up @@ -116,3 +118,17 @@ def bool_attr(obj):
return "true" if obj else "false"

environment.filters["bool_attr"] = bool_attr


class UUIDExtension(Extension):
"""Extensions for generating UUIDs."""

def __init__(self, environment):
"""Initialize the extension with the given environment."""
super().__init__(environment)

def dns_uuid5(obj):
"""A DNS-based UUID5 object generated from the provided content."""
return str(uuid.uuid5(uuid.NAMESPACE_DNS, obj))

environment.filters["dns_uuid5"] = dns_uuid5
19 changes: 19 additions & 0 deletions tests/integrations/cookiecutter/test_UUIDExtension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from unittest.mock import MagicMock

import pytest

from briefcase.integrations.cookiecutter import UUIDExtension


@pytest.mark.parametrize(
"value, expected",
[
("example.com", "cfbff0d1-9375-5685-968c-48ce8b15ae17"),
("foobar.example.com", "941bbcd9-03e1-568a-a728-8434055bc338"),
],
)
def test_dns_uuid5_value(value, expected):
env = MagicMock()
env.filters = {}
UUIDExtension(env)
assert env.filters["dns_uuid5"](value) == expected

0 comments on commit c2b7290

Please sign in to comment.