Skip to content

Commit 56b1b63

Browse files
committed
Rewritten GenFile as HDLWriter
1 parent 164f196 commit 56b1b63

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

hdltools/gen_file.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

hdltools/hdl_writer.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# Copyright (C) 2025 HDLtools Project
3+
#
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
#
6+
7+
"""
8+
Generates and writes the output HDL code based on templates.
9+
"""
10+
11+
from pathlib import Path
12+
from jinja2 import Environment, FileSystemLoader
13+
14+
15+
class HDLWriter:
16+
"""Generates and writes the output HDL code."""
17+
18+
def __init__(self):
19+
tdir = Path(__file__).parent.joinpath('templates')
20+
self.env = Environment(loader=FileSystemLoader(str(tdir)))
21+
self.code = ''
22+
23+
def render(self, tempname, context):
24+
"""Render the specified template."""
25+
template = self.env.get_template(f'{tempname}.jinja')
26+
self.code = template.render(context)
27+
28+
def write_file(self, path):
29+
"""Writes the generated HDL code to the specified file."""
30+
with open(path, 'w', encoding='utf-8') as fobj:
31+
fobj.write(self.code)
32+
33+
def get_code(self):
34+
"""Get the generated code."""
35+
return self.code

0 commit comments

Comments
 (0)