Parse a config.h-style file. A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary.
(fp, vars=None)
| 431 | |
| 432 | |
| 433 | def parse_config_h(fp, vars=None): |
| 434 | """Parse a config.h-style file. |
| 435 | |
| 436 | A dictionary containing name/value pairs is returned. If an |
| 437 | optional dictionary is passed in as the second argument, it is |
| 438 | used instead of a new dictionary. |
| 439 | """ |
| 440 | if vars is None: |
| 441 | vars = {} |
| 442 | import re |
| 443 | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
| 444 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
| 445 | |
| 446 | while True: |
| 447 | line = fp.readline() |
| 448 | if not line: |
| 449 | break |
| 450 | m = define_rx.match(line) |
| 451 | if m: |
| 452 | n, v = m.group(1, 2) |
| 453 | try: |
| 454 | if n in _ALWAYS_STR: |
| 455 | raise ValueError |
| 456 | v = int(v) |
| 457 | except ValueError: |
| 458 | pass |
| 459 | vars[n] = v |
| 460 | else: |
| 461 | m = undef_rx.match(line) |
| 462 | if m: |
| 463 | vars[m.group(1)] = 0 |
| 464 | return vars |
| 465 | |
| 466 | |
| 467 | def get_config_h_filename(): |
no test coverage detected