summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/telegraf/utils.py
blob: 783e9edc7b0072e4fa29da86621cee3de9d20ef3 (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
from typing import Union

ValueType = Union[str, bool, int, float]


def format_string(key: ValueType) -> str:
    if isinstance(key, str):
        return key.replace(',', r'\,') \
                  .replace(' ', r'\ ') \
                  .replace('=', r'\=')
    else:
        return str(key)


def format_value(value: ValueType) -> str:
    if isinstance(value, str):
        value = value.replace('"', '\"')
        return f'"{value}"'
    elif isinstance(value, bool):
        return str(value)
    elif isinstance(value, int):
        return f"{value}i"
    elif isinstance(value, float):
        return str(value)
    else:
        raise ValueError()