Skip to content

Commit f529cfb

Browse files
committed
Make sure name is converted to an identifier for files
If we still use `-` in files, things break, so we should always convert the `name` to a valid identifier when using it in files. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 4ecd910 commit f529cfb

File tree

8 files changed

+29
-14
lines changed

8 files changed

+29
-14
lines changed

cookiecutter/cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"default_codeowners": "(like @some-org/some-team; defaults to a team based on the repo type)",
2525
"_extensions": [
2626
"jinja2_time.TimeExtension",
27+
"local_extensions.as_identifier",
2728
"local_extensions.default_codeowners",
2829
"local_extensions.github_repo_name",
2930
"local_extensions.introduction",

cookiecutter/hooks/post_gen_project.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,10 @@ def finish_lib_setup() -> None:
451451
- `lib`: `src/frequenz/{name}`
452452
- `rest`: `src/frequenz/{type}/{name}`
453453
"""
454+
name = cookiecutter.name.lower().replace("-", "_")
454455
recursive_overwrite_move(
455-
_pathlib.Path(f"src/frequenz/{cookiecutter.type}/{cookiecutter.name}"),
456-
_pathlib.Path(f"src/frequenz/{cookiecutter.name}"),
456+
_pathlib.Path(f"src/frequenz/{cookiecutter.type}/{name}"),
457+
_pathlib.Path(f"src/frequenz/{name}"),
457458
)
458459
_pathlib.Path(f"src/frequenz/{cookiecutter.type}").rmdir()
459460

cookiecutter/local_extensions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ def _get_from_json(key: str) -> str:
4747

4848

4949
# Ignoring because cookiecutter simple_filter decorator is not typed.
50+
@_simple_filter # type: ignore[misc]
51+
def as_identifier(name: str) -> str:
52+
"""Convert a name to a valid identifier.
53+
54+
Args:
55+
name: The name to convert.
56+
57+
Returns:
58+
The converted identifier.
59+
"""
60+
return name.lower().replace("-", "_")
61+
62+
5063
@_simple_filter # type: ignore[misc]
5164
def python_package(cookiecutter: dict[str, str]) -> str:
5265
"""Generate the Python package (import) depending on the repository type.

cookiecutter/{{cookiecutter.github_repo_name}}/.github/labeler.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
# For example:
1111
#
1212
# "part:module":
13-
# - "src/frequenz/{{cookiecutter.type}}/{{cookiecutter.name}}/module/**"
13+
# - "src/frequenz/{{cookiecutter.type}}/{{cookiecutter.name | as_identifier}}/module/**"
1414
#
1515
# "part:other":
16-
# - "src/frequenz/{{cookiecutter.type}}/{{cookiecutter.name}}/other/**"
16+
# - "src/frequenz/{{cookiecutter.type}}/{{cookiecutter.name | as_identifier}}/other/**"
1717
#
1818
# # For excluding some files (in this example, label "part:complicated"
1919
# # everything inside src/ with a .py suffix, except for src/__init__.py)

cookiecutter/{{cookiecutter.github_repo_name}}/proto/frequenz/api/{{cookiecutter.name}}/{{cookiecutter.name}}.proto renamed to cookiecutter/{{cookiecutter.github_repo_name}}/proto/frequenz/api/{{cookiecutter.name | as_identifier}}/{{cookiecutter.name | as_identifier}}.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
syntax = "proto3";
1414

15-
package frequenz.api.{{cookiecutter.name}}.{{cookiecutter.name}};
15+
package frequenz.api.{{cookiecutter.name | as_identifier}}.{{cookiecutter.name | as_identifier}};
1616

1717
// An example message.
1818
//

cookiecutter/{{cookiecutter.github_repo_name}}/tests/test_{{cookiecutter.name}}.py renamed to cookiecutter/{{cookiecutter.github_repo_name}}/tests/test_{{cookiecutter.name | as_identifier}}.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
# License: {{cookiecutter.license}}
22
# Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_name}}
33

4-
"""Tests for the {{cookiecutter.name}} package."""
4+
"""Tests for the {{cookiecutter.python_package}} package."""
55

66
{%- if cookiecutter.type == "api" %}
77

88

99
def test_package_import() -> None:
1010
"""Test that the package can be imported."""
1111
# pylint: disable=import-outside-toplevel
12-
from frequenz.api import {{cookiecutter.name}}
12+
from frequenz.api import {{cookiecutter.name | as_identifier}}
1313

14-
assert {{cookiecutter.name}} is not None
14+
assert {{cookiecutter.name | as_identifier}} is not None
1515

1616

1717
def test_module_import_components() -> None:
1818
"""Test that the modules can be imported."""
1919
# pylint: disable=import-outside-toplevel
20-
from frequenz.api.{{cookiecutter.name}} import {{cookiecutter.name}}_pb2
20+
from frequenz.api.{{cookiecutter.name | as_identifier}} import {{cookiecutter.name | as_identifier}}_pb2
2121

22-
assert {{cookiecutter.name}}_pb2 is not None
22+
assert {{cookiecutter.name | as_identifier}}_pb2 is not None
2323

2424
# pylint: disable=import-outside-toplevel
25-
from frequenz.api.{{cookiecutter.name}} import {{cookiecutter.name}}_pb2_grpc
25+
from frequenz.api.{{cookiecutter.name | as_identifier}} import {{cookiecutter.name | as_identifier}}_pb2_grpc
2626

27-
assert {{cookiecutter.name}}_pb2_grpc is not None
27+
assert {{cookiecutter.name | as_identifier}}_pb2_grpc is not None
2828
{%- else %}
2929
import pytest
3030

3131
from {{cookiecutter.python_package}} import delete_me
3232

3333

34-
def test_{{cookiecutter.name}}_succeeds() -> None: # TODO(cookiecutter): Remove
34+
def test_{{cookiecutter.name | as_identifier}}_succeeds() -> None: # TODO(cookiecutter): Remove
3535
"""Test that the delete_me function succeeds."""
3636
assert delete_me() is True
3737

3838

39-
def test_{{cookiecutter.name}}_fails() -> None: # TODO(cookiecutter): Remove
39+
def test_{{cookiecutter.name | as_identifier}}_fails() -> None: # TODO(cookiecutter): Remove
4040
"""Test that the delete_me function fails."""
4141
with pytest.raises(RuntimeError, match="This function should be removed!"):
4242
delete_me(blow_up=True)

0 commit comments

Comments
 (0)