Swift: integrated template name in dataclass

This commit is contained in:
Paolo Tranquilli 2022-04-14 15:53:15 +02:00
Родитель 197ea5b8f3
Коммит 24697feebc
3 изменённых файлов: 10 добавлений и 5 удалений

Просмотреть файл

@ -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__":

Просмотреть файл

@ -97,6 +97,8 @@ class DbSchemeInclude:
@dataclass
class DbScheme:
template: ClassVar = 'dbscheme'
src: str
includes: List[DbSchemeInclude]
declarations: List[DbDecl]

Просмотреть файл

@ -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()):