Skip to content

Commit

Permalink
feat: expose renderer as a standalone plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Feb 18, 2021
1 parent 9e506b2 commit e935383
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 14 deletions.
45 changes: 45 additions & 0 deletions beet/contrib/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Plugin that invokes the built-in template renderer."""


__all__ = [
"render",
]


from typing import Dict, List, Optional

from beet import Context, Plugin
from beet.core.utils import JsonDict


def beet_default(ctx: Context):
config = ctx.meta.get("render", {})

resource_pack = config.get("resource_pack")
data_pack = config.get("data_pack")

ctx.require(render(resource_pack, data_pack))


def render(
resource_pack: Optional[Dict[str, List[str]]] = None,
data_pack: Optional[Dict[str, List[str]]] = None,
) -> Plugin:
"""Return a plugin that processes the data pack and the resource pack with Jinja."""

def plugin(ctx: Context):
for groups, pack in zip([resource_pack or {}, data_pack or {}], ctx.packs):
for group, patterns in groups.items():
try:
proxy = getattr(pack, group)
file_paths = proxy.match(*patterns)
except:
raise ValueError(f"Invalid pattern group {group!r}.") from None
else:
for path in file_paths:
ctx.template.render_file(
proxy[path],
__render__={"path": path, "group": group},
)

return plugin
21 changes: 7 additions & 14 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
from typing import Iterable, Iterator, List, Optional, Sequence

from beet.contrib.render import render
from beet.core.cache import MultiCache
from beet.core.utils import FileSystemPath, intersperse
from beet.core.watch import DirectoryWatcher, FileChanges
Expand Down Expand Up @@ -237,20 +238,12 @@ def bootstrap(self, ctx: Context):
for path in config.load:
pack.load(path)

for config, pack in zip(pack_configs, ctx.packs):
for group, patterns in config.render.items():
try:
proxy = getattr(pack, group)
file_paths = proxy.match(*patterns)
except:
message = f"Invalid pattern group {group!r} in configuration."
raise ErrorMessage(message) from None
else:
for path in file_paths:
ctx.template.render_file(
proxy[path],
__render__={"path": path, "group": group},
)
ctx.require(
render(
resource_pack=self.config.resource_pack.render,
data_pack=self.config.data_pack.render,
)
)

yield

Expand Down
6 changes: 6 additions & 0 deletions tests/examples/code_render_plugin/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pipeline": ["demo"],
"meta": {
"message": "foo"
}
}
8 changes: 8 additions & 0 deletions tests/examples/code_render_plugin/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from beet import Context, Function
from beet.contrib.render import render


def beet_default(ctx: Context):
ctx.data["demo:foo"] = Function(["say {{ ctx.meta.message }}"])

ctx.require(render(data_pack={"functions": ["*"]}))
6 changes: 6 additions & 0 deletions tests/examples/code_sandbox_render/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pipeline": ["demo"],
"meta": {
"message": "foo"
}
}
18 changes: 18 additions & 0 deletions tests/examples/code_sandbox_render/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from beet import Context, Function, sandbox
from beet.contrib.render import render


def beet_default(ctx: Context):
ctx.data["demo:foo"] = Function(["say {{ ctx.meta.message }}"])
ctx.require(render_functions)

ctx.data["demo:bar"] = Function(["say {{ ctx.meta.message }}"])
ctx.require(sandbox(add_function, render_functions))


def add_function(ctx: Context):
ctx.data["demo:isolated"] = Function(["say isolated {{ ctx.meta.message }}"])


def render_functions(ctx: Context):
ctx.require(render(data_pack={"functions": ["*"]}))
24 changes: 24 additions & 0 deletions tests/examples/load_subpipelines_render/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"pipeline": [
{
"data_pack": {
"load": ["pack1"]
}
},
{
"data_pack": {
"load": ["pack2"]
}
},
"beet.contrib.render"
],
"meta": {
"pack1_message": "foo",
"pack2_message": "bar",
"render": {
"data_pack": {
"functions": ["demo:*"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say {{ ctx.meta.pack1_message }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say {{ ctx.meta.pack2_message }}
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": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say {{ ctx.meta.message }}
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 isolated
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"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": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}

0 comments on commit e935383

Please sign in to comment.