From 24697feebc10f20381ec1aa8fc28ec6aaae345a4 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Thu, 14 Apr 2022 15:53:15 +0200 Subject: [PATCH] Swift: integrated template name in dataclass --- swift/codegen/dbschemegen.py | 2 +- swift/codegen/lib/dbscheme.py | 2 ++ swift/codegen/lib/render.py | 11 +++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/swift/codegen/dbschemegen.py b/swift/codegen/dbschemegen.py index 3976d1b39e1..eb3a9028858 100755 --- a/swift/codegen/dbschemegen.py +++ b/swift/codegen/dbschemegen.py @@ -83,7 +83,7 @@ def generate(opts, renderer): includes=get_includes(data, include_dir=input.parent), declarations=get_declarations(data)) - renderer.render("dbscheme", out, dbscheme) + renderer.render(dbscheme, out) if __name__ == "__main__": diff --git a/swift/codegen/lib/dbscheme.py b/swift/codegen/lib/dbscheme.py index 2eb2d7f5bb1..8fe18c0169b 100644 --- a/swift/codegen/lib/dbscheme.py +++ b/swift/codegen/lib/dbscheme.py @@ -97,6 +97,8 @@ class DbSchemeInclude: @dataclass class DbScheme: + template: ClassVar = 'dbscheme' + src: str includes: List[DbSchemeInclude] declarations: List[DbDecl] diff --git a/swift/codegen/lib/render.py b/swift/codegen/lib/render.py index 035dc99352d..be47d1297cb 100644 --- a/swift/codegen/lib/render.py +++ b/swift/codegen/lib/render.py @@ -7,6 +7,7 @@ https://mustache.github.io/ import hashlib import logging +import pathlib import pystache @@ -39,14 +40,16 @@ class Renderer: def rendered(self): return self.written | self.skipped - def render(self, name, output, data): - """ Render the template called `name` in the template directory, writing to `output` using `data` as context + def render(self, data, output: pathlib.Path): + """ Render `data` to `output`. + + `data` must have a `template` attribute denoting which template to use from the template directory. If the file is unchanged, then no write is performed (and `done_something` remains unchanged) """ - mnemonic, _, _ = name.lower().partition(".") + mnemonic = type(data).__name__ output.parent.mkdir(parents=True, exist_ok=True) - data = self.r.render_name(name, data, generator=self.generator) + data = self.r.render_name(data.template, data, generator=self.generator) if output.is_file(): with open(output, "rb") as file: if _md5(data.encode()) == _md5(file.read()):