Skip to content

Commit

Permalink
Merge pull request #7 from googlefonts/workflow-action
Browse files Browse the repository at this point in the history
Add fontra-workflow action wrapper, and configure it
  • Loading branch information
justvanrossum authored Mar 4, 2024
2 parents 25ebd55 + 7129ea1 commit a5e7c03
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ classifiers = [
fontra-compile = "fontra_compile.__main__:main"


[project.entry-points."fontra.workflow.actions"]
fontra_compile = "fontra_compile.workflow_action"


[tool.hatch.build.targets.wheel]
packages = ["src/fontra_compile"]

Expand Down
34 changes: 34 additions & 0 deletions src/fontra_compile/workflow_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import pathlib
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from typing import AsyncGenerator

from fontra.core.protocols import ReadableFontBackend
from fontra.workflow.actions import OutputActionProtocol, registerActionClass

from .builder import Builder


@registerActionClass("fontra-compile")
@dataclass(kw_only=True)
class FontraCompileAction:
destination: str
input: ReadableFontBackend | None = field(init=False, default=None)

@asynccontextmanager
async def connect(
self, input: ReadableFontBackend
) -> AsyncGenerator[ReadableFontBackend | OutputActionProtocol, None]:
self.input = input
try:
yield self
finally:
self.input = None

async def process(self, outputDir: os.PathLike = pathlib.Path()) -> None:
outputFontPath = outputDir / self.destination
builder = Builder(self.input)
await builder.setup()
ttFont = await builder.build()
ttFont.save(outputFontPath)
39 changes: 39 additions & 0 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pathlib
import subprocess

import yaml
from fontra.workflow.workflow import Workflow
from test_compile import cleanupTTX

testDir = pathlib.Path(__file__).resolve().parent
dataDir = testDir / "data"


testWorkFlow = """
steps:
- action: input
source: "tests/data/MutatorSans.fontra"
- action: fontra-compile
destination: "output1.ttf"
"""


async def test_workflow(tmpdir):
tmpdir = pathlib.Path(tmpdir)
config = yaml.safe_load(testWorkFlow)

workflow = Workflow(config=config)

async with workflow.endPoints() as endPoints:
assert endPoints.endPoint is not None

for output in endPoints.outputs:
await output.process(tmpdir)
ttxPath = dataDir / "MutatorSans.ttx"
outPath = tmpdir / output.destination
outTTXPath = tmpdir / (outPath.stem + ".ttx")
subprocess.run(["ttx", outPath], check=True)

ttxLines = cleanupTTX(outTTXPath.read_text())
expectedLines = cleanupTTX(ttxPath.read_text())
assert expectedLines == ttxLines

0 comments on commit a5e7c03

Please sign in to comment.