Skip to content

Commit 7205ac5

Browse files
committed
chore: compatibility with python 3.8
Signed-off-by: Sylvain Witmeyer <sylvain.witmeyer@gmail.com>
1 parent cc86745 commit 7205ac5

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

prometheus_client/parser.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,30 @@ def _replace_escaping(s: str) -> str:
3737
return ESCAPING_RE.sub(replace_escape_sequence, s)
3838

3939

40+
# for python < 3.9 compatibility
41+
def _removeprefix(data: str, prefix: str) -> str:
42+
if data.startswith(prefix):
43+
return data[len(prefix):]
44+
return data
45+
46+
47+
# for python < 3.9 compatibility
48+
def _removesuffix(data: str, suffix: str) -> str:
49+
if data.endswith(suffix):
50+
return data[:-len(suffix)]
51+
return data
52+
53+
4054
def _parse_labels(labels_string: str) -> Dict[str, str]:
4155
labels: Dict[str, str] = {}
4256
# Return if we don't have valid labels
4357
if "=" not in labels_string:
4458
return labels
4559

4660
# remove SINGLE leading and trailing commas
47-
labels_string = labels_string.strip().removeprefix(',').removesuffix(',')
61+
labels_string = labels_string.strip()
62+
labels_string = _removeprefix(labels_string, ',')
63+
labels_string = _removesuffix(labels_string, ',')
4864

4965
sub_labels = labels_string.split(",")
5066
try:
@@ -54,7 +70,8 @@ def _parse_labels(labels_string: str) -> Dict[str, str]:
5470

5571
normalized_value = label_value.strip()
5672
# remove SINGLE leading and trailing double quotes
57-
normalized_value = normalized_value.removeprefix('"').removesuffix('"')
73+
normalized_value = _removeprefix(normalized_value, '"')
74+
normalized_value = _removesuffix(normalized_value, '"')
5875

5976
if "\\" in normalized_value:
6077
normalized_value = _replace_escaping(normalized_value)

0 commit comments

Comments
 (0)