Skip to content

Commit

Permalink
fix: load files by trying all the possible extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Nov 26, 2021
1 parent 3171a94 commit c437220
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 11 deletions.
22 changes: 13 additions & 9 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from beet.core.file import File, FileOrigin, JsonFile, PngFile
from beet.core.utils import FileSystemPath, JsonDict, TextComponent

from .utils import list_files
from .utils import list_extensions, list_files

T = TypeVar("T")
PackFileType = TypeVar("PackFileType", bound="PackFile")
Expand Down Expand Up @@ -495,21 +495,25 @@ def scan(
name, namespace = namespace_dir, cls()

assert name and namespace is not None
extension = "".join(filename.suffixes)
extensions = list_extensions(filename)

if file_type := extra_info.get(path := "/".join(scope + [extra_name])):
namespace.extra[path] = file_type.load(pack, filename)
continue

while path := tuple(scope):
if file_type := scope_map.get((path, extension)):
key = "/".join(
filename.relative_to(Path(directory, name, *path)).parts
)[: -len(extension)]
for extension in extensions:
if file_type := scope_map.get((path, extension)):
key = "/".join(
filename.relative_to(Path(directory, name, *path)).parts
)[: -len(extension)]

namespace[file_type][key] = file_type.load(pack, filename)
break
scope.pop()
namespace[file_type][key] = file_type.load(pack, filename)
break
else:
scope.pop()
continue
break

if name and namespace:
yield name, namespace
Expand Down
14 changes: 12 additions & 2 deletions beet/library/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
__all__ = [
"list_files",
"list_extensions",
]


import os
from pathlib import Path
from typing import Iterator
from itertools import accumulate
from pathlib import Path, PurePath
from typing import Iterator, List

from beet.core.utils import FileSystemPath

Expand All @@ -14,3 +16,11 @@ def list_files(directory: FileSystemPath) -> Iterator[Path]:
for root, _, files in os.walk(directory):
for filename in files:
yield Path(root, filename).relative_to(directory)


def list_extensions(path: PurePath) -> List[str]:
extensions: List[str] = list(
accumulate(reversed(path.suffixes), lambda a, b: b + a) # type: ignore
)
extensions.reverse()
return extensions
2 changes: 2 additions & 0 deletions examples/load_predicate/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data_pack:
load: [src]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.05
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.25
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.05
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.25
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"condition": "minecraft:random_chance",
"chance": 0.5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 7,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 7,
"description": ""
}
}

0 comments on commit c437220

Please sign in to comment.