Skip to content

Commit

Permalink
chore: compatibility with python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
sylwit committed May 14, 2024
1 parent cc86745 commit a84f6d3
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,30 @@ def _replace_escaping(s: str) -> str:
return ESCAPING_RE.sub(replace_escape_sequence, s)


# for python < 3.9 compatibility
def _removeprefix(data: str, prefix: str) -> str:
if data.startswith(prefix):
return data[len(prefix):]
return data


# for python < 3.9 compatibility
def _removesuffix(data: str, suffix: str) -> str:
if data.endswith(suffix):
return data[:-len(suffix)]
return data


def _parse_labels(labels_string: str) -> Dict[str, str]:
labels: Dict[str, str] = {}
# Return if we don't have valid labels
if "=" not in labels_string:
return labels

# remove SINGLE leading and trailing commas
labels_string = labels_string.strip().removeprefix(',').removesuffix(',')
labels_string = labels_string.strip()
labels_string = _removeprefix(labels_string, ',')
labels_string = _removesuffix(labels_string, ',')

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

normalized_value = label_value.strip()
# remove SINGLE leading and trailing double quotes
normalized_value = normalized_value.removeprefix('"').removesuffix('"')
normalized_value = _removeprefix(normalized_value, '"')
normalized_value = _removesuffix(normalized_value, '"')

if "\\" in normalized_value:
normalized_value = _replace_escaping(normalized_value)
Expand Down

0 comments on commit a84f6d3

Please sign in to comment.