Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for empty file names #436

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,6 @@ class NamespaceProxy(

def split_key(self, key: str) -> Tuple[str, str]:
namespace, _, file_path = key.partition(":")
if not file_path:
raise KeyError(key)
return namespace, file_path

def join_key(self, key1: str, key2: str) -> str:
Expand Down
15 changes: 14 additions & 1 deletion beet/library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,22 @@ def list_origin_folders(prefix: str, origin: FileOrigin) -> Dict[str, List[PureP
return folders


def modified_suffixes(path: PurePath) -> List[str]:
"""
Equivalent to path.suffixes but support file with empty name
"""
name = path.name
if name.endswith("."):
return []
if name.startswith("."):
name = name[1:]
return ["." + suffix for suffix in name.split(".")]
return path.suffixes


def list_extensions(path: PurePath) -> List[str]:
extensions: List[str] = list(
accumulate(reversed(path.suffixes), lambda a, b: b + a) # type: ignore
accumulate(reversed(modified_suffixes(path)), lambda a, b: b + a) # type: ignore
)
extensions.reverse()
extensions.append("")
Expand Down
9 changes: 9 additions & 0 deletions examples/code_void/add_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from beet import Context, Function


def beet_default(ctx: Context):
for f in ctx.data.functions.keys():
ctx.data.functions[f] = Function(f"say {f}")

ctx.data.functions["namespace:"] = Function("give me sugar")
ctx.data.functions["namespace:a/"] = Function("give me apple")
10 changes: 10 additions & 0 deletions examples/code_void/beet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data_pack:
load: [.]
resource_pack:
load: [.]

output: build


pipeline:
- add_header
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
give me sugar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
give me apple
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:..
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:.mcfunction.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a.py.git
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/..
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say test:a/a.py.git
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 61,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 46,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"path": "aaa...mcfunction",
"suffixes": [
".",
".",
".mcfunction"
],
"modified_suffixes": [
".",
".",
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"path": "load.mcfunction",
"suffixes": [
".mcfunction"
],
"modified_suffixes": [
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"path": "load.py.mcfunction",
"suffixes": [
".py",
".mcfunction"
],
"modified_suffixes": [
".py",
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"path": "...mcfunction",
"suffixes": [],
"modified_suffixes": [
".",
".",
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"path": "..mcfunction",
"suffixes": [],
"modified_suffixes": [
".",
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"path": ".mcfunction",
"suffixes": [],
"modified_suffixes": [
".mcfunction"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"path": ".py.mcfunction",
"suffixes": [
".mcfunction"
],
"modified_suffixes": [
".py",
".mcfunction"
]
}
25 changes: 25 additions & 0 deletions tests/test_modified_suffixes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from pytest_insta import SnapshotFixture
from beet.library.utils import modified_suffixes
from pathlib import Path


PATHS = [
"load.mcfunction",
".mcfunction",
"..mcfunction",
"load.py.mcfunction",
".py.mcfunction",
"aaa...mcfunction",
"...mcfunction",
]


@pytest.mark.parametrize("path", PATHS)
def test_paths_suffixes(snapshot: SnapshotFixture, path: str):
real_path = Path(path)
assert snapshot(f"{path}.json") == {
"path": path,
"suffixes": list(real_path.suffixes),
"modified_suffixes": list(modified_suffixes(real_path)),
}
Loading