Extract the next comma-separated label term from the text. The results are stripped terms for the label name, label value, and then the remainder of the string including the final , or }. Raises ValueError if the term is empty and we're in openmetrics mode.
(text: str, openmetrics: bool)
| 104 | |
| 105 | |
| 106 | def _next_term(text: str, openmetrics: bool) -> Tuple[str, str, str]: |
| 107 | """Extract the next comma-separated label term from the text. The results |
| 108 | are stripped terms for the label name, label value, and then the remainder |
| 109 | of the string including the final , or }. |
| 110 | |
| 111 | Raises ValueError if the term is empty and we're in openmetrics mode. |
| 112 | """ |
| 113 | |
| 114 | # There may be a leading comma, which is fine here. |
| 115 | if text[0] == ',': |
| 116 | text = text[1:] |
| 117 | if not text: |
| 118 | return "", "", "" |
| 119 | if text[0] == ',': |
| 120 | raise ValueError("multiple commas") |
| 121 | |
| 122 | splitpos = _next_unquoted_char(text, '=,}') |
| 123 | if splitpos >= 0 and text[splitpos] == "=": |
| 124 | labelname = text[:splitpos] |
| 125 | text = text[splitpos + 1:] |
| 126 | splitpos = _next_unquoted_char(text, ',}') |
| 127 | else: |
| 128 | labelname = "__name__" |
| 129 | |
| 130 | if splitpos == -1: |
| 131 | splitpos = len(text) |
| 132 | term = text[:splitpos] |
| 133 | if not term and openmetrics: |
| 134 | raise ValueError("empty term:", term) |
| 135 | |
| 136 | rest = text[splitpos:] |
| 137 | return labelname, term.strip(), rest.strip() |
| 138 | |
| 139 | |
| 140 | def _next_unquoted_char(text: str, chs: Optional[str], startidx: int = 0) -> int: |
no test coverage detected