Skip to content

Commit

Permalink
feat: add generator draft
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 22, 2021
1 parent b419495 commit 1e341c5
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions beet/toolchain/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from beet.core.file import TextFileBase
from beet.core.utils import TextComponent
from beet.library.base import NamespaceFile
from beet.library.data_pack import Function
from beet.library.data_pack import DataPack, Function
from beet.library.resource_pack import ResourcePack

from .tree import TreeNode, generate_tree
from .utils import LazyFormat, stable_hash
Expand All @@ -46,6 +47,13 @@ class Generator:
default_factory=lambda: defaultdict(int) # type: ignore
)

assets: ResourcePack = field(init=False)
data: DataPack = field(init=False)

def __post_init__(self):
self.assets = self.ctx.assets
self.data = self.ctx.data

def __getitem__(self: GeneratorType, key: Any) -> GeneratorType:
return self.__class__(
ctx=self.ctx,
Expand Down Expand Up @@ -166,9 +174,9 @@ def __call__(
)

pack = (
self.ctx.data
if file_type in self.ctx.data.namespace_type.field_map
else self.ctx.assets
self.data
if file_type in self.data.namespace_type.field_map
else self.assets
)

pack[key] = file_instance
Expand Down Expand Up @@ -260,4 +268,22 @@ def function_tree(
root = self[Function].path(fmt, hash) if fmt else self[Function].path(hash=hash)

for node in generate_tree(root, items, key):
yield node, self.ctx.data.functions.setdefault(node.parent, Function())
yield node, self.data.functions.setdefault(node.parent, Function())

def draft(self: GeneratorType) -> GeneratorType:
"""Return a generator that works on an intermediate resource pack and data pack."""
draft = self.__class__(
ctx=self.ctx,
scope=self.scope,
registry=self.registry,
)
draft.assets = ResourcePack()
draft.data = DataPack()
return draft

def apply(self):
"""Merge the working resource pack and data pack into the context."""
if self.assets is not self.ctx.assets:
self.ctx.assets.merge(self.assets)
if self.data is not self.ctx.data:
self.ctx.data.merge(self.data)

0 comments on commit 1e341c5

Please sign in to comment.