summaryrefslogtreecommitdiff
path: root/pint/facets/group/definitions.py
blob: 5bb14f80f851ddc346369bf406fca6cd3326befe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
    pint.facets.group.defintions
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2022 by Pint Authors, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Tuple

from ...definitions import Definition
from ...errors import DefinitionSyntaxError
from ...util import SourceIterator
from ..plain.definitions import UnitDefinition


@dataclass(frozen=True)
class GroupDefinition:
    """Definition of a group.

        @group <name> [using <group 1>, ..., <group N>]
            <definition 1>
            ...
            <definition N>
        @end

    Example::

        @group AvoirdupoisUS using Avoirdupois
            US_hundredweight = hundredweight = US_cwt
            US_ton = ton
            US_force_ton = force_ton = _ = US_ton_force
        @end

    """

    #: Regex to match the header parts of a definition.
    _header_re = re.compile(r"@group\s+(?P<name>\w+)\s*(using\s(?P<used_groups>.*))*")

    name: str
    units: Tuple[Tuple[int, UnitDefinition], ...]
    using_group_names: Tuple[str, ...]

    @property
    def unit_names(self) -> Tuple[str, ...]:
        return tuple(u.name for lineno, u in self.units)

    @classmethod
    def from_lines(cls, lines, non_int_type=float):
        """Return a Group object parsing an iterable of lines.

        Parameters
        ----------
        lines : list[str]
            iterable
        define_func : callable
            Function to define a unit in the registry; it must accept a single string as
            a parameter.

        Returns
        -------

        """

        lines = SourceIterator(lines)
        lineno, header = next(lines)

        r = cls._header_re.search(header)

        if r is None:
            raise ValueError("Invalid Group header syntax: '%s'" % header)

        name = r.groupdict()["name"].strip()
        groups = r.groupdict()["used_groups"]
        if groups:
            parent_group_names = tuple(a.strip() for a in groups.split(","))
        else:
            parent_group_names = ()

        units = []
        for lineno, line in lines:
            definition = Definition.from_string(line, non_int_type=non_int_type)
            if not isinstance(definition, UnitDefinition):
                raise DefinitionSyntaxError(
                    "Only UnitDefinition are valid inside _used_groups, not "
                    + str(definition),
                    lineno=lineno,
                )
            units.append((lineno, definition))

        return cls(name, tuple(units), parent_group_names)