summaryrefslogtreecommitdiff
path: root/pint/definitions.py
blob: ce89e94d412c97499464e070c68c410c4273cc7d (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
"""
    pint.definitions
    ~~~~~~~~~~~~~~~~

    Kept for backwards compatibility

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

from __future__ import annotations

from . import errors
from ._vendor import flexparser as fp
from .delegates import ParserConfig, txt_defparser


class Definition:
    """This is kept for backwards compatibility"""

    @classmethod
    def from_string(cls, input_string: str, non_int_type: type = float) -> Definition:
        """Parse a string into a definition object.

        Parameters
        ----------
        input_string
            Single line string.
        non_int_type
            Numerical type used for non integer values.

        Raises
        ------
        DefinitionSyntaxError
            If a syntax error was found.
        """
        cfg = ParserConfig(non_int_type)
        parser = txt_defparser.DefParser(cfg, None)
        pp = parser.parse_string(input_string)
        for definition in parser.iter_parsed_project(pp):
            if isinstance(definition, Exception):
                raise errors.DefinitionSyntaxError(str(definition))
            if not isinstance(definition, (fp.BOS, fp.BOF, fp.BOS)):
                return definition

        # TODO: What shall we do in this return path.