-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from googlefonts/workflow-action
Add fontra-workflow action wrapper, and configure it
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |