Skip to content

Commit

Permalink
feat: freeze context
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 21, 2022
1 parent f9f3e81 commit 1bebe8d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
33 changes: 17 additions & 16 deletions beet/toolchain/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ def __call__(self, **kwds: Any) -> "ConfigurablePlugin":
class ContextContainer(Container[Callable[["Context"], Any], Any]):
"""Dict-like container that instantiates and holds objects injected into the context."""

def __init__(self, ctx: "Context"):
super().__init__()
self.ctx = ctx
ctx: "Context"

def missing(self, key: Callable[["Context"], Any]) -> Any:
return key(self.ctx)
Expand Down Expand Up @@ -158,7 +156,7 @@ def flush(self):
self.generated.flush()


@dataclass
@dataclass(eq=False, frozen=True)
class Context:
"""The build context."""

Expand All @@ -170,25 +168,24 @@ class Context:

directory: Path
output_directory: Optional[Path]
meta: JsonDict
cache: ProjectCache
worker: WorkerPoolHandle
template: TemplateManager
generate: Generator = field(init=False)
meta: JsonDict = extra_field()
cache: ProjectCache = extra_field()
worker: WorkerPoolHandle = extra_field()
template: TemplateManager = extra_field()

assets: ResourcePack = field(default_factory=ResourcePack)
data: DataPack = field(default_factory=DataPack)

whitelist: InitVar[Optional[List[str]]] = None
whitelist: InitVar[Optional[List[str]]] = extra_field(default=None)

_container: ContextContainer = extra_field(init=False)
_path_entry: str = extra_field(init=False)
_container: ContextContainer = extra_field(
init=False,
default_factory=ContextContainer,
)

def __post_init__(self, whitelist: Optional[List[str]]):
self._container = ContextContainer(self)
self._path_entry = str(self.directory.resolve())
self._container.ctx = self

self.generate = self.inject(Generator)
self.generate.assets = self.assets
self.generate.data = self.data

Expand Down Expand Up @@ -234,7 +231,7 @@ def inject(self, cls: Any) -> Any:
@contextmanager
def activate(self):
"""Push the context directory to sys.path and handle cleanup to allow module reloading."""
with local_import_path(self._path_entry), self.cache:
with local_import_path(str(self.directory.resolve())), self.cache:
yield self.inject(Pipeline)

@contextmanager
Expand Down Expand Up @@ -280,6 +277,10 @@ def validate(
def packs(self) -> Tuple[ResourcePack, DataPack]:
return self.assets, self.data

@property
def generate(self) -> Generator:
return self.inject(Generator)

def require(self, *args: PluginSpec):
"""Execute the specified plugin."""
self.inject(Pipeline).require(*args)
Expand Down
4 changes: 4 additions & 0 deletions examples/code_description/beet.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"name": "Something else",
"description": { "text": "bold description", "bold": true },
"author": "Fizzy",
"version": "1.2.3",
"pipeline": ["demo"]
}
6 changes: 0 additions & 6 deletions examples/code_description/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@


def beet_default(ctx: Context):
ctx.project_id = "something_else"
ctx.project_name = "Something else"
ctx.project_description = {"text": "bold description", "bold": True}
ctx.project_author = "Fizzy"
ctx.project_version = "1.2.3"

ctx.data.description = ["override for ", {"text": "data pack", "color": "red"}]
4 changes: 4 additions & 0 deletions examples/code_metadata/beet.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"name": "Demo",
"description": "The description of my project",
"author": "Example",
"version": "1.7.4",
"pipeline": ["demo"]
}
17 changes: 10 additions & 7 deletions examples/code_metadata/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@


def beet_default(ctx: Context):
ctx.project_id = "demo"
ctx.project_name = "Demo"
ctx.project_description = "The description of my project"
ctx.project_author = "Example"
ctx.project_version = "1.7.4"

ctx.data["demo:foo"] = Function(ctx.template.render("foo.mcfunction"))
ctx.data["demo:foo"] = Function(
ctx.template.render(
"foo.mcfunction",
id=ctx.project_id,
name=ctx.project_name,
description=ctx.project_description,
author=ctx.project_author,
version=ctx.project_version,
)
)
10 changes: 5 additions & 5 deletions examples/code_metadata/templates/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
say {{ project_id }}
say {{ project_name }}
say {{ project_description }}
say {{ project_author }}
say {{ project_version }}
say {{ id }}
say {{ name }}
say {{ description }}
say {{ author }}
say {{ version }}

0 comments on commit 1bebe8d

Please sign in to comment.