Skip to content

Commit

Permalink
Adjust filters to support packages with no namespace or multiple name…
Browse files Browse the repository at this point in the history
…spaces
  • Loading branch information
davisagli committed Oct 27, 2024
1 parent 5d538f1 commit 79a284a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cookieplone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Plone Foundation <[email protected]>
#
# SPDX-License-Identifier: MIT
__version__ = "0.7.2.dev0"
__version__ = "0.8.0.dev0"
24 changes: 22 additions & 2 deletions cookieplone/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@
@simple_filter
def package_name(v) -> str:
"""Return the Python package name (without namespace)."""
return v.split(".")[1]
return v.split(".")[-1]


@simple_filter
def package_namespace(v) -> str:
"""Return the Python package namespace."""
return v.split(".")[0]
parts = v.rsplit(".", 1)
if len(parts) > 1:
return parts[0]
return ""


@simple_filter
def package_namespaces(v) -> str:
"""Return Python package namespaces formatted for setup.py."""
result = []
nsparts = []
for ns in v.split(".")[:-1]:
nsparts.append(ns)
result.append(".".join(nsparts))
return ", ".join(f'"{item}"' for item in result)


@simple_filter
def package_path(v) -> str:
"""Return path to the package code within the src directory."""
return "/".join(v.split("."))


@simple_filter
Expand Down
12 changes: 12 additions & 0 deletions cookieplone/utils/plone.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ def format_python_codebase(path: Path):
for cmd, func in PY_FORMATTERS:
logger.debug(f"Format codebase: Running {cmd}")
func(path)


def create_namespace_packages(path: Path, package_name: str):
"""Create namespace packages to hold an existing package."""
current = path.parent
for namespace in package_name.split(".")[:-1]:
current = current / namespace
current.mkdir()
(current / "__init__.py").write_text(
'__import__("pkg_resources").declare_namespace(__name__)\n'
)
path.rename(current / package_name.split(".")[-1])
5 changes: 0 additions & 5 deletions cookieplone/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ def validate_python_package_name(value: str) -> str:
parts = value.split(".")
if any(not part.isidentifier() for part in parts):
return f"'{value}' is not a valid Python identifier."
if len(parts) != 2:
return (
"The Python package name must contain a single namespace "
"(e.g. collective.something)"
)
return ""


Expand Down
1 change: 1 addition & 0 deletions news/43.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support Python package names with no namespace or multiple namespaces. @davisagli
12 changes: 12 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ def func(filter_: str) -> Path:
@pytest.mark.parametrize(
"filter_,raw,expected",
[
["package_name", "{{'foo' | package_name}}", "foo"],
["package_name", "{{'foo.bar' | package_name}}", "bar"],
["package_namespace", "{{'foo' | package_namespace}}", ""],
["package_namespace", "{{'foo.bar' | package_namespace}}", "foo"],
["package_namespaces", "{{'foo' | package_namespaces}}", ""],
["package_namespaces", "{{'foo.bar' | package_namespaces}}", '"foo"'],
[
"package_namespaces",
"{{'foo.bar.baz' | package_namespaces}}",
'"foo", "foo.bar"',
],
["package_path", "{{'foo' | package_path}}", "foo"],
["package_path", "{{'foo.bar' | package_path}}", "foo/bar"],
["package_path", "{{'foo.bar.baz' | package_path}}", "foo/bar/baz"],
["pascal_case", "{{'foo_bar' | pascal_case}}", "FooBar"],
["use_prerelease_versions", "{{ '' | use_prerelease_versions }}", "No"],
["node_version_for_volto", "{{'17' | node_version_for_volto}}", "18"],
Expand Down
17 changes: 2 additions & 15 deletions tests/utils/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,9 @@ def test_validate_language_code(value: str, expected: str):
(" ", "' ' is not a valid Python identifier."),
("", "'' is not a valid Python identifier."),
("project title", "'project title' is not a valid Python identifier."),
(
"project_title",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
("project_title", ""),
("project-title", "'project-title' is not a valid Python identifier."),
(
"projecttitle",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
(
"projectTitle",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
("projecttitle", ""),
("project.title", ""),
),
)
Expand Down

0 comments on commit 79a284a

Please sign in to comment.