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