Skip to content

Commit

Permalink
feat: add beet.contrib.unknown_files
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Apr 24, 2023
1 parent 8c17b31 commit e4c67f5
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 4 deletions.
37 changes: 37 additions & 0 deletions beet/contrib/unknown_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Plugin to load unknown files."""


__all__ = [
"UnknownAsset",
"UnknownData",
"unknown_files",
]


from typing import ClassVar, Tuple

from beet import BinaryFile, Context


class UnknownAsset(BinaryFile):
"""Class representing an unknown file in resource packs."""

scope: ClassVar[Tuple[str, ...]] = ()
extension: ClassVar[str] = ""


class UnknownData(BinaryFile):
"""Class representing an unknown file in data packs."""

scope: ClassVar[Tuple[str, ...]] = ()
extension: ClassVar[str] = ""


def beet_default(ctx: Context):
ctx.require(unknown_files)


def unknown_files(ctx: Context):
"""Plugin to load unknown files."""
ctx.assets.extend_namespace.append(UnknownAsset)
ctx.data.extend_namespace.append(UnknownData)
12 changes: 8 additions & 4 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,19 @@ def scan(

file_dir: List[str] = []

while path := tuple(scope):
while True:
path = tuple(scope)
for extension in extensions:
if file_type := scope_map.get((path, extension)):
key = "/".join(file_dir + [basename[: -len(extension)]])
key = "/".join(
file_dir + [basename[: len(basename) - len(extension)]]
)
namespace[file_type][key] = file_type.load(origin, filename)
break
else:
file_dir.insert(0, scope.pop())
continue
if scope:
file_dir.insert(0, scope.pop())
continue
break

if name and namespace:
Expand Down
1 change: 1 addition & 0 deletions beet/library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ def list_extensions(path: PurePath) -> List[str]:
accumulate(reversed(path.suffixes), lambda a, b: b + a) # type: ignore
)
extensions.reverse()
extensions.append("")
return extensions
1 change: 1 addition & 0 deletions examples/nosnap_load_unknown/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
5 changes: 5 additions & 0 deletions examples/nosnap_load_unknown/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require:
- beet.contrib.unknown_files
data_pack:
load: "src"
output: "out"
3 changes: 3 additions & 0 deletions examples/nosnap_load_unknown/src/data/demo/bar/thing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
1 change: 1 addition & 0 deletions examples/nosnap_load_unknown/src/data/demo/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bop

0 comments on commit e4c67f5

Please sign in to comment.