| 1 |
""" |
| 2 |
pint.facets.systems.definitions |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: 2022 by Pint Authors, see AUTHORS for more details. |
| 6 |
:license: BSD, see LICENSE for more details. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
from collections.abc import Iterable |
| 12 |
from dataclasses import dataclass |
| 13 |
|
| 14 |
from ... import errors |
| 15 |
from ...compat import Self |
| 16 |
|
| 17 |
|
| 18 |
@dataclass(frozen=True) |
| 19 |
class BaseUnitRule: |
| 20 |
"""A rule to define a base unit within a system.""" |
| 21 |
|
| 22 |
#: name of the unit to become base unit |
| 23 |
#: (must exist in the registry) |
| 24 |
new_unit_name: str |
| 25 |
#: name of the unit to be kicked out to make room for the new base uni |
| 26 |
#: If None, the current base unit with the same dimensionality will be used |
| 27 |
old_unit_name: str | None = None |
| 28 |
|
| 29 |
# Instead of defining __post_init__ here, |
| 30 |
# it will be added to the container class |
| 31 |
# so that the name and a meaningfull class |
| 32 |
# could be used. |
| 33 |
|
| 34 |
|
| 35 |
@dataclass(frozen=True) |
| 36 |
class SystemDefinition(errors.WithDefErr): |
| 37 |
"""Definition of a System.""" |
| 38 |
|
| 39 |
#: name of the system |
| 40 |
name: str |
| 41 |
#: unit groups that will be included within the system |
| 42 |
using_group_names: tuple[str, ...] |
| 43 |
#: rules to define new base unit within the system. |
| 44 |
rules: tuple[BaseUnitRule, ...] |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def from_lines( |
| 48 |
cls: type[Self], lines: Iterable[str], non_int_type: type |
| 49 |
) -> Self | None: |
| 50 |
# TODO: this is to keep it backwards compatible |
| 51 |
# TODO: check when is None returned. |
| 52 |
from ...delegates import ParserConfig, txt_defparser |
| 53 |
|
| 54 |
cfg = ParserConfig(non_int_type) |
| 55 |
parser = txt_defparser.DefParser(cfg, None) |
| 56 |
pp = parser.parse_string("\n".join(lines) + "\n@end") |
| 57 |
for definition in parser.iter_parsed_project(pp): |
| 58 |
if isinstance(definition, cls): |
| 59 |
return definition |
| 60 |
|
| 61 |
@property |
| 62 |
def unit_replacements(self) -> tuple[tuple[str, str | None], ...]: |
| 63 |
# TODO: check if None can be dropped. |
| 64 |
return tuple((el.new_unit_name, el.old_unit_name) for el in self.rules) |
| 65 |
|
| 66 |
def __post_init__(self): |
| 67 |
if not errors.is_valid_system_name(self.name): |
| 68 |
raise self.def_err(errors.MSG_INVALID_SYSTEM_NAME) |
| 69 |
|
| 70 |
for k in self.using_group_names: |
| 71 |
if not errors.is_valid_group_name(k): |
| 72 |
raise self.def_err( |
| 73 |
f"refers to '{k}' that " + errors.MSG_INVALID_GROUP_NAME |
| 74 |
) |
| 75 |
|
| 76 |
for ndx, rule in enumerate(self.rules, 1): |
| 77 |
if not errors.is_valid_unit_name(rule.new_unit_name): |
| 78 |
raise self.def_err( |
| 79 |
f"rule #{ndx} refers to '{rule.new_unit_name}' that " |
| 80 |
+ errors.MSG_INVALID_UNIT_NAME |
| 81 |
) |
| 82 |
if rule.old_unit_name and not errors.is_valid_unit_name(rule.old_unit_name): |
| 83 |
raise self.def_err( |
| 84 |
f"rule #{ndx} refers to '{rule.old_unit_name}' that " |
| 85 |
+ errors.MSG_INVALID_UNIT_NAME |
| 86 |
) |