(key, value, ignored_keys, indent, in_list=False)
| 469 | valid_keys = re.compile("^[A-Za-z][0-9A-Za-z_]*$") |
| 470 | |
| 471 | def stringize(key, value, ignored_keys, indent, in_list=False): |
| 472 | if not isinstance(key, str): |
| 473 | raise EasyGraphError(f"{key!r} is not a string") |
| 474 | if not valid_keys.match(key): |
| 475 | raise EasyGraphError(f"{key!r} is not a valid key") |
| 476 | if not isinstance(key, str): |
| 477 | key = str(key) |
| 478 | if key not in ignored_keys: |
| 479 | if isinstance(value, (int, bool)): |
| 480 | if key == "label": |
| 481 | yield indent + key + ' "' + str(value) + '"' |
| 482 | elif value is True: |
| 483 | # python bool is an instance of int |
| 484 | yield indent + key + " 1" |
| 485 | elif value is False: |
| 486 | yield indent + key + " 0" |
| 487 | # GML only supports signed 32-bit integers |
| 488 | elif value < -(2**31) or value >= 2**31: |
| 489 | yield indent + key + ' "' + str(value) + '"' |
| 490 | else: |
| 491 | yield indent + key + " " + str(value) |
| 492 | elif isinstance(value, float): |
| 493 | text = repr(value).upper() |
| 494 | # GML matches INF to keys, so prepend + to INF. Use repr(float(*)) |
| 495 | # instead of string literal to future proof against changes to repr. |
| 496 | if text == repr(float("inf")).upper(): |
| 497 | text = "+" + text |
| 498 | else: |
| 499 | # GML requires that a real literal contain a decimal point, but |
| 500 | # repr may not output a decimal point when the mantissa is |
| 501 | # integral and hence needs fixing. |
| 502 | epos = text.rfind("E") |
| 503 | if epos != -1 and text.find(".", 0, epos) == -1: |
| 504 | text = text[:epos] + "." + text[epos:] |
| 505 | if key == "label": |
| 506 | yield indent + key + ' "' + text + '"' |
| 507 | else: |
| 508 | yield indent + key + " " + text |
| 509 | elif isinstance(value, dict): |
| 510 | yield indent + key + " [" |
| 511 | next_indent = indent + " " |
| 512 | for key, value in value.items(): |
| 513 | yield from stringize(key, value, (), next_indent) |
| 514 | yield indent + "]" |
| 515 | elif ( |
| 516 | isinstance(value, (list, tuple)) |
| 517 | and key != "label" |
| 518 | and value |
| 519 | and not in_list |
| 520 | ): |
| 521 | if len(value) == 1: |
| 522 | yield indent + key + " " + f'"{LIST_START_VALUE}"' |
| 523 | for val in value: |
| 524 | yield from stringize(key, val, (), indent, True) |
| 525 | else: |
| 526 | if stringizer: |
| 527 | try: |
| 528 | value = stringizer(value) |
no test coverage detected