Coverage for hdl_registers/generator/html/constant_table.py: 92%
38 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +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 = f"""\
61{self.header}
62<table>
63<thead>
64 <tr>
65 <th>Name</th>
66 <th>Value</th>
67 <th>Description</th>
68 </tr>
69</thead>
70<tbody>"""
72 for constant in self.iterate_constants():
73 description = self._html_translator.translate(constant.description)
74 html += f"""
75 <tr>
76 <td><strong>{constant.name}</strong></td>
77 <td>{self._format_constant_value(constant=constant)}</td>
78 <td>{description}</td>
79 </tr>"""
81 html += """
82</tbody>
83</table>"""
84 return html
86 def _format_constant_value(self, constant: "Constant") -> str:
87 if isinstance(constant, UnsignedVectorConstant):
88 return f"{constant.prefix}{constant.value}"
90 if isinstance(constant, StringConstant):
91 return f'"{constant.value}"'
93 # For others, just cast to string.
94 if isinstance(constant, (BooleanConstant, IntegerConstant, FloatConstant)):
95 return str(constant.value)
97 raise ValueError(f'Got unexpected constant type. "{constant}".')