Coverage for hdl_registers/generator/html/constant_table.py: 92%
38 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 20:51 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 20:51 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the hdl-registers project, an HDL register generator fast enough to run
5# in real time.
6# https://hdl-registers.com
7# https://github.com/hdl-registers/hdl-registers
8# --------------------------------------------------------------------------------------------------
10# Standard libraries
11from pathlib import Path
12from typing import TYPE_CHECKING, Any
14# First party libraries
15from hdl_registers.constant.bit_vector_constant import UnsignedVectorConstant
16from hdl_registers.constant.boolean_constant import BooleanConstant
17from hdl_registers.constant.float_constant import FloatConstant
18from hdl_registers.constant.integer_constant import IntegerConstant
19from hdl_registers.constant.string_constant import StringConstant
21# Local folder libraries
22from .html_generator_common import HtmlGeneratorCommon
23from .html_translator import HtmlTranslator
25if TYPE_CHECKING:
26 # First party libraries
27 from hdl_registers.constant.constant import Constant
28 from hdl_registers.register_list import RegisterList
31class HtmlConstantTableGenerator(HtmlGeneratorCommon):
32 """
33 Generate HTML code with register constant information in a table.
34 See the :ref:`generator_html` article for usage details.
35 """
37 __version__ = "1.0.0"
39 SHORT_DESCRIPTION = "HTML constant table"
41 @property
42 def output_file(self) -> Path:
43 """
44 Result will be placed in this file.
45 """
46 return self.output_folder / f"{self.name}_constant_table.html"
48 def __init__(self, register_list: "RegisterList", output_folder: Path):
49 super().__init__(register_list=register_list, output_folder=output_folder)
51 self._html_translator = HtmlTranslator()
53 def get_code(self, **kwargs: Any) -> str:
54 """
55 Get a HTML table with information about register constants.
56 """
57 if not self.register_list.constants:
58 return ""
60 html = """\
61<table>
62<thead>
63 <tr>
64 <th>Name</th>
65 <th>Value</th>
66 <th>Description</th>
67 </tr>
68</thead>
69<tbody>"""
71 for constant in self.iterate_constants():
72 description = self._html_translator.translate(constant.description)
73 html += f"""
74 <tr>
75 <td><strong>{constant.name}</strong></td>
76 <td>{self._format_constant_value(constant=constant)}</td>
77 <td>{description}</td>
78 </tr>"""
80 html += """
81</tbody>
82</table>"""
83 return html
85 def _format_constant_value(self, constant: "Constant") -> str:
86 if isinstance(constant, UnsignedVectorConstant):
87 return f"{constant.prefix}{constant.value}"
89 if isinstance(constant, StringConstant):
90 return f'"{constant.value}"'
92 # For others, just cast to string.
93 if isinstance(constant, (BooleanConstant, IntegerConstant, FloatConstant)):
94 return str(constant.value)
96 raise ValueError(f'Got unexpected constant type. "{constant}".')