Skip to content

Commit

Permalink
feat: handle load mount
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 1, 2022
1 parent 48958f7 commit 3d17aa2
Show file tree
Hide file tree
Showing 43 changed files with 181 additions and 12 deletions.
33 changes: 25 additions & 8 deletions beet/contrib/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@


from glob import glob
from pathlib import Path
from zipfile import ZipFile

from pydantic import BaseModel

from beet import Context, ErrorMessage, ListOption, PackageablePath, configurable
from beet import Context, ErrorMessage, PackLoadOptions, configurable


class LoadOptions(BaseModel):
resource_pack: ListOption[PackageablePath] = ListOption()
data_pack: ListOption[PackageablePath] = ListOption()
resource_pack: PackLoadOptions = PackLoadOptions()
data_pack: PackLoadOptions = PackLoadOptions()


def beet_default(ctx: Context):
Expand All @@ -27,9 +29,24 @@ def beet_default(ctx: Context):
def load(ctx: Context, opts: LoadOptions):
"""Plugin that loads data packs and resource packs."""
for load_options, pack in zip([opts.resource_pack, opts.data_pack], ctx.packs):
for pattern in load_options.entries():
if paths := glob(str(ctx.directory / pattern)):
for path in paths:
pack.load(path)
for load_entry in load_options.entries():
if isinstance(load_entry, dict):
for prefix, mount_options in load_entry.items():
entries = [
Path(entry)
for pattern in mount_options.entries()
for entry in glob(str(ctx.directory / pattern))
]
if not entries:
raise ErrorMessage(f'Couldn\'t mount "{prefix}".')
for entry in entries:
dst = f"{prefix}/{entry.name}" if len(entries) > 1 else prefix
if entry.is_file() and entry.suffix == ".zip":
entry = ZipFile(entry)
pack.mount(dst, entry)
else:
raise ErrorMessage(f'Couldn\'t load "{pattern}".')
if paths := glob(str(ctx.directory / load_entry)):
for path in paths:
pack.load(path)
else:
raise ErrorMessage(f'Couldn\'t load "{load_entry}".')
34 changes: 30 additions & 4 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__all__ = [
"ProjectConfig",
"PackConfig",
"PackLoadOptions",
"ListOption",
"PackageablePath",
"InvalidProjectConfig",
Expand Down Expand Up @@ -111,6 +112,33 @@ def __str__(self) -> str:
return str(self.__root__)


class PackLoadOptions(
ListOption[Union[PackageablePath, Dict[str, ListOption[PackageablePath]]]]
):
"""Options for loading data packs and resource packs."""

def resolve(self, directory: FileSystemPath) -> "PackLoadOptions":
"""Resolve load options relative to the given directory."""
return PackLoadOptions.parse_obj(
[
(
{
prefix: ListOption.parse_obj(
[
pattern.resolve(directory)
for pattern in mount_options.entries()
]
)
for prefix, mount_options in load_entry.items()
}
if isinstance(load_entry, dict)
else load_entry.resolve(directory)
)
for load_entry in self.entries()
]
)


class PackConfig(BaseModel):
"""Data pack and resource pack configuration."""

Expand All @@ -121,7 +149,7 @@ class PackConfig(BaseModel):
compression: Optional[Literal["none", "deflate", "bzip2", "lzma"]] = None
compression_level: Optional[int] = None

load: ListOption[PackageablePath] = ListOption()
load: PackLoadOptions = PackLoadOptions()
render: Dict[str, ListOption[str]] = {}

class Config:
Expand Down Expand Up @@ -195,9 +223,7 @@ def resolve(self, directory: FileSystemPath) -> "ProjectConfig":
)

for pack_config in [self.data_pack, self.resource_pack]:
pack_config.load = ListOption.parse_obj(
[load_path.resolve(path) for load_path in pack_config.load.entries()]
)
pack_config.load = pack_config.load.resolve(path)

self.pipeline = [
item.resolve(path) if isinstance(item, ProjectConfig) else item
Expand Down
2 changes: 2 additions & 0 deletions examples/code_mount_load/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pipeline:
- demo
19 changes: 19 additions & 0 deletions examples/code_mount_load/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from beet import Context
from beet.contrib.load import load


def beet_default(ctx: Context):
ctx.require(
load(
data_pack={
"data/demo/functions": ctx.directory / "src",
"data/owo/functions/foo.mcfunction": ctx.directory / "src/thing.txt",
"pack.mcmeta": ctx.directory / "pack.mcmeta",
},
resource_pack={
"assets/minecraft": ctx.directory / "src",
"assets/other/sounds.json": ctx.directory / "src/sounds.json",
"not_in_schema.txt": ctx.directory / "pack.mcmeta",
},
)
)
6 changes: 6 additions & 0 deletions examples/code_mount_load/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": "aaaaaa"
}
}
1 change: 1 addition & 0 deletions examples/code_mount_load/src/bar.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
1 change: 1 addition & 0 deletions examples/code_mount_load/src/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
1 change: 1 addition & 0 deletions examples/code_mount_load/src/sounds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions examples/code_mount_load/src/thing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say thing
10 changes: 10 additions & 0 deletions examples/load_mount/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data_pack:
load:
data/demo/functions: "src"
data/owo/functions/foo.mcfunction: "src/thing.txt"
pack.mcmeta: "pack.mcmeta"
resource_pack:
load:
assets/minecraft: "src"
assets/other/sounds.json: "src/sounds.json"
not_in_schema.txt: "pack.mcmeta"
6 changes: 6 additions & 0 deletions examples/load_mount/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": "aaaaaa"
}
}
1 change: 1 addition & 0 deletions examples/load_mount/src/bar.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
1 change: 1 addition & 0 deletions examples/load_mount/src/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
1 change: 1 addition & 0 deletions examples/load_mount/src/sounds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions examples/load_mount/src/thing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say thing
4 changes: 4 additions & 0 deletions examples/load_mount_glob/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data_pack:
load:
data/demo/functions:
- "src/*.mcfunction"
1 change: 1 addition & 0 deletions examples/load_mount_glob/src/bar.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
1 change: 1 addition & 0 deletions examples/load_mount_glob/src/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
6 changes: 6 additions & 0 deletions examples/load_mount_zip/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data_pack:
load:
data/demo/functions: "src.zip"
resource_pack:
load:
assets/minecraft: "src.zip"
Binary file added examples/load_mount_zip/src.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say thing
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": "aaaaaa"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 8,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say thing
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": "aaaaaa"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 8,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 8,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 9,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 8,
"description": ""
}
}

0 comments on commit 3d17aa2

Please sign in to comment.