aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/parsers/parse_value.py
blob: 58d8864973c0478df95d63d49f27285ffc4945d6 (plain) (blame)
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
"""This module allows you to parse a string into its native type."""

import ast
import re
from typing import Any


def parse_value(value: Any) -> Any:
    """Parse a value into native type.

    parse_value('5') -> 5   (int)
    parse_value('-5') -> -5 (int)

    parse_value('5.52') -> 5.52   (float)
    parse_value('-53.2') -> -53.2 (float)

    parse_value('blahfafda') -> 'blahfafda' (string)

    parse_value('true') -> True (bool)
    parse_value('FALSE') -> False (bool)

    parse_value("  [ [ [ [] ]] ]  ") -> [[[[]]]] (list)

    Args:
        value: The value to parse.

    Returns:
        The parsed value, if it could be parsed
        as a dictionary, list, int, float, or bool.
        Otherwise, it simply returns the original
        passed in value.
    """
    if isinstance(value, dict):
        parsed_map = {}
        for item in value:
            parsed_map[item] = parse_value(value[item])
        return parsed_map

    if isinstance(value, list):
        parsed = []
        for item in value:
            parsed.append(parse_value(item))
        return parsed

    if can_be_parsed_as_int(value):
        return int(value.strip()) if isinstance(value, str) else int(value)

    if can_be_parsed_as_float(value):
        return float(value.strip()) if isinstance(value, str) else float(value)

    if can_be_parsed_as_bool(value):
        return bool(value) if isinstance(value, bool) else bool(strtobool(value))

    if can_be_parsed_as_dict(value):
        return parse_value(ast.literal_eval(value.strip()))

    if can_be_parsed_as_list(value):
        return parse_value(ast.literal_eval(value.strip()))

    return value


def can_be_parsed_as_int(value: Any) -> bool:
    """Check whether a value can be parsed as a integer.

    can_be_parsed_as_int(5) -> True
    can_be_parsed_as_int("5") -> True
    can_be_parsed_as_int("-5") -> True
    can_be_parsed_as_int(True) -> False

    Args:
        value: The value to check if it can be parsed as an integer.

    Returns:
        True if we can parse the value as a integer. False otherwise.
    """
    if isinstance(value, int) and not isinstance(value, bool):
        return True

    if not isinstance(value, str):
        return False

    value = value.strip()

    if value.startswith("-"):
        return value[1:].isdigit()

    return value.isdigit()


def can_be_parsed_as_float(value: Any) -> bool:
    """Checks whether a value can be parsed as a float.

    can_be_parsed_as_float(45) -> False
    can_be_parsed_as_float(1.1) -> True
    can_be_parsed_as_float("1.1") -> True
    can_be_parsed_as_float("-1.1") -> True
    can_be_parsed_as_float(".1") -> True

    Args:
        value: The value to be parsed.

    Returns:
        True if the value can be parsed as a float. False otherwise.
    """
    FLOAT_REGEX = r"^\d*\.\d+$"

    if isinstance(value, float):
        return True

    if not isinstance(value, str):
        return False

    value = value.strip()

    if value.startswith("-"):
        value = value[1:]

    return bool(re.match(FLOAT_REGEX, value))


def can_be_parsed_as_bool(value: Any) -> bool:
    """Checks whether a value can be parsed as a boolean.

    can_be_parsed_as_bool(True) -> True
    can_be_parsed_as_bool("true") -> True
    can_be_parsed_as_bool("false") -> True
    can_be_parsed_as_bool("TRUE") -> True
    can_be_parsed_as_bool(0) -> False

    Args:
        value: The value to be parsed.

    Returns:
        True if the value can be parsed as a boolean. False otherwise.
    """
    if isinstance(value, bool):
        return True

    if not isinstance(value, str):
        return False

    value = value.lower().strip()

    return value == "true" or value == "false"


# distutils strtobool backport: https://github.com/yukinarit/strtobool
def strtobool(val):
    """Convert a string representation of truth to true (1) or false (0).
    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'val' is anything else.
    """
    val = val.lower()
    if val in ("y", "yes", "t", "true", "on", "1"):
        return 1
    elif val in ("n", "no", "f", "false", "off", "0"):
        return 0
    else:
        raise ValueError("invalid truth value %r" % (val,))


def can_be_parsed_as_dict(value: Any) -> bool:
    """Checks whether a value can be parsed as a dictionary.

    can_be_parsed_as_dict({}) -> True
    can_be_parsed_as_dict("{}") -> True
    can_be_parsed_as_dict("  {  }  ") -> True
    can_be_parsed_as_dict("{ 'hello': 5 }") -> True
    can_be_parsed_as_dict([]]) -> False
    can_be_parsed_as_dict(" { invalid } ") -> False

    Args:
        value: The value to be parsed.

    Returns:
        True if the value can be parsed as a dictionary. False otherwise.
    """
    if isinstance(value, dict):
        return True

    if not isinstance(value, str):
        return False

    value = value.strip()

    if value[0] == "{" and value[-1] == "}":
        try:
            ast.literal_eval(value)
            return True
        except Exception:
            return False

    return False


def can_be_parsed_as_list(value: Any) -> bool:
    """Checks whether a value can be parsed as a list.

    can_be_parsed_as_list([]) -> True
    can_be_parsed_as_list([1, 2]) -> True
    can_be_parsed_as_list("[]") -> True
    can_be_parsed_as_list("  []  ") -> True
    can_be_parsed_as_list("[1, 2]") -> True
    can_be_parsed_as_list({}) -> False
    can_be_parsed_as_list(" [ invalid ] ") -> False

    Args:
        value: The value to be parsed.

    Returns:
        True if the value can be parsed as a list. False otherwise.
    """
    if isinstance(value, list):
        return True

    if not isinstance(value, str):
        return False

    value = value.strip()

    if value[0] == "[" and value[-1] == "]":
        try:
            ast.literal_eval(value)
            return True
        except Exception:
            return False

    return False