File: full.py

package info (click to toggle)
python-pint 0.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,940 kB
  • sloc: python: 20,478; makefile: 148
file content (268 lines) | stat: -rw-r--r-- 8,127 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
pint.delegates.formatter.full
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Implements:
- Full: dispatch to other formats, accept defaults.

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

from __future__ import annotations

import locale
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Literal

from ..._typing import Magnitude
from ...compat import Unpack, babel_parse
from ...util import iterable
from ._compound_unit_helpers import BabelKwds, SortFunc, sort_by_unit_name
from ._to_register import REGISTERED_FORMATTERS
from .html import HTMLFormatter
from .latex import LatexFormatter, SIunitxFormatter
from .plain import (
    BaseFormatter,
    CompactFormatter,
    DefaultFormatter,
    PrettyFormatter,
    RawFormatter,
)

if TYPE_CHECKING:
    from ...compat import Locale
    from ...facets.measurement import Measurement
    from ...facets.plain import (
        MagnitudeT,
        PlainQuantity,
        PlainUnit,
    )
    from ...registry import UnitRegistry


class FullFormatter(BaseFormatter):
    """A formatter that dispatch to other formatters.

    Has a default format, locale and babel_length
    """

    _formatters: dict[str, Any] = {}

    default_format: str = ""

    # TODO: This can be over-riden by the registry definitions file
    dim_order: tuple[str, ...] = (
        "[substance]",
        "[mass]",
        "[current]",
        "[luminosity]",
        "[length]",
        "[]",
        "[time]",
        "[temperature]",
    )

    default_sort_func: SortFunc | None = staticmethod(sort_by_unit_name)

    locale: Locale | None = None

    def __init__(self, registry: UnitRegistry | None = None):
        super().__init__(registry)

        self._formatters = {}
        self._formatters["raw"] = RawFormatter(registry)
        self._formatters["D"] = DefaultFormatter(registry)
        self._formatters["H"] = HTMLFormatter(registry)
        self._formatters["P"] = PrettyFormatter(registry)
        self._formatters["Lx"] = SIunitxFormatter(registry)
        self._formatters["L"] = LatexFormatter(registry)
        self._formatters["C"] = CompactFormatter(registry)

    def set_locale(self, loc: str | None) -> None:
        """Change the locale used by default by `format_babel`.

        Parameters
        ----------
        loc : str or None
            None (do not translate), 'sys' (detect the system locale) or a locale id string.
        """
        if isinstance(loc, str):
            if loc == "sys":
                loc = locale.getdefaultlocale()[0]

            # We call babel parse to fail here and not in the formatting operation
            babel_parse(loc)

        self.locale = loc

    def get_formatter(self, spec: str):
        if spec == "":
            return self._formatters["D"]
        for k, v in self._formatters.items():
            if k in spec:
                return v

        for k, v in REGISTERED_FORMATTERS.items():
            if k in spec:
                orphan_fmt = REGISTERED_FORMATTERS[k]
                break
        else:
            return self._formatters["D"]

        try:
            fmt = orphan_fmt.__class__(self._registry)
            spec = getattr(fmt, "spec", spec)
            self._formatters[spec] = fmt
            return fmt
        except Exception:
            return orphan_fmt

    def format_magnitude(
        self, magnitude: Magnitude, mspec: str = "", **babel_kwds: Unpack[BabelKwds]
    ) -> str:
        mspec = mspec or self.default_format
        return self.get_formatter(mspec).format_magnitude(
            magnitude, mspec, **babel_kwds
        )

    def format_unit(
        self,
        unit: PlainUnit | Iterable[tuple[str, Any]],
        uspec: str = "",
        sort_func: SortFunc | None = None,
        **babel_kwds: Unpack[BabelKwds],
    ) -> str:
        uspec = uspec or self.default_format
        sort_func = sort_func or self.default_sort_func
        return self.get_formatter(uspec).format_unit(
            unit, uspec, sort_func=sort_func, **babel_kwds
        )

    def format_quantity(
        self,
        quantity: PlainQuantity[MagnitudeT],
        spec: str = "",
        **babel_kwds: Unpack[BabelKwds],
    ) -> str:
        spec = spec or self.default_format
        # If Compact is selected, do it at the beginning
        if "#" in spec:
            spec = spec.replace("#", "")
            obj = quantity.to_compact()
        else:
            obj = quantity

        del quantity

        locale = babel_kwds.get("locale", self.locale)

        if locale:
            if "use_plural" in babel_kwds:
                use_plural = babel_kwds["use_plural"]
            else:
                use_plural = obj.magnitude > 1
                if iterable(use_plural):
                    use_plural = True
        else:
            use_plural = False

        return self.get_formatter(spec).format_quantity(
            obj,
            spec,
            sort_func=self.default_sort_func,
            use_plural=use_plural,
            length=babel_kwds.get("length", None),
            locale=locale,
        )

    def format_measurement(
        self,
        measurement: Measurement,
        meas_spec: str = "",
        **babel_kwds: Unpack[BabelKwds],
    ) -> str:
        meas_spec = meas_spec or self.default_format
        # If Compact is selected, do it at the beginning
        if "#" in meas_spec:
            meas_spec = meas_spec.replace("#", "")
            obj = measurement.to_compact()
        else:
            obj = measurement

        del measurement

        use_plural = obj.magnitude.nominal_value > 1
        if iterable(use_plural):
            use_plural = True

        return self.get_formatter(meas_spec).format_measurement(
            obj,
            meas_spec,
            sort_func=self.default_sort_func,
            use_plural=babel_kwds.get("use_plural", use_plural),
            length=babel_kwds.get("length", None),
            locale=babel_kwds.get("locale", self.locale),
        )

    #######################################
    # This is for backwards compatibility
    #######################################

    def format_unit_babel(
        self,
        unit: PlainUnit | Iterable[tuple[str, Any]],
        spec: str = "",
        length: Literal["short", "long", "narrow"] | None = None,
        locale: Locale | None = None,
    ) -> str:
        if self.locale is None and locale is None:
            raise ValueError(
                "format_babel requires a locale argumente if the Formatter locale is not set."
            )

        return self.format_unit(
            unit,
            spec or self.default_format,
            sort_func=self.default_sort_func,
            use_plural=False,
            length=length,
            locale=locale or self.locale,
        )

    def format_quantity_babel(
        self,
        quantity: PlainQuantity[MagnitudeT],
        spec: str = "",
        length: Literal["short", "long", "narrow"] | None = None,
        locale: Locale | None = None,
    ) -> str:
        if self.locale is None and locale is None:
            raise ValueError(
                "format_babel requires a locale argumente if the Formatter locale is not set."
            )

        use_plural = quantity.magnitude > 1
        if iterable(use_plural):
            use_plural = True

        return self.format_quantity(
            quantity,
            spec or self.default_format,
            sort_func=self.default_sort_func,
            use_plural=use_plural,
            length=length,
            locale=locale or self.locale,
        )


################################################################
# This allows to format units independently of the registry
#
REGISTERED_FORMATTERS["raw"] = RawFormatter()
REGISTERED_FORMATTERS["D"] = DefaultFormatter()
REGISTERED_FORMATTERS["H"] = HTMLFormatter()
REGISTERED_FORMATTERS["P"] = PrettyFormatter()
REGISTERED_FORMATTERS["Lx"] = SIunitxFormatter()
REGISTERED_FORMATTERS["L"] = LatexFormatter()
REGISTERED_FORMATTERS["C"] = CompactFormatter()