|
5 | 5 | import os |
6 | 6 | import re |
7 | 7 | import shlex |
| 8 | +from collections import namedtuple |
8 | 9 |
|
9 | 10 | from ..rgb import to_color as as_color |
10 | 11 | from ..utils import log_error |
11 | 12 |
|
12 | 13 | key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$') |
| 14 | +BadLine = namedtuple('BadLine', 'number line exception') |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def to_color(x): |
@@ -90,22 +92,28 @@ def parse_line(line, type_map, special_handling, ans, all_keys, base_path_for_in |
90 | 92 | ans[key] = val |
91 | 93 |
|
92 | 94 |
|
93 | | -def _parse(lines, type_map, special_handling, ans, all_keys): |
| 95 | +def _parse(lines, type_map, special_handling, ans, all_keys, accumulate_bad_lines=None): |
94 | 96 | name = getattr(lines, 'name', None) |
95 | 97 | if name: |
96 | 98 | base_path_for_includes = os.path.dirname(os.path.abspath(name)) |
97 | 99 | else: |
98 | 100 | from ..constants import config_dir |
99 | 101 | base_path_for_includes = config_dir |
100 | | - for line in lines: |
101 | | - parse_line(line, type_map, special_handling, ans, all_keys, base_path_for_includes) |
| 102 | + for i, line in enumerate(lines): |
| 103 | + try: |
| 104 | + parse_line(line, type_map, special_handling, ans, all_keys, base_path_for_includes) |
| 105 | + except Exception as e: |
| 106 | + if accumulate_bad_lines is None: |
| 107 | + raise |
| 108 | + accumulate_bad_lines.append(BadLine(i + 1, line.rstrip(), e)) |
102 | 109 |
|
103 | 110 |
|
104 | 111 | def parse_config_base( |
105 | | - lines, defaults, type_map, special_handling, ans, check_keys=True |
| 112 | + lines, defaults, type_map, special_handling, ans, check_keys=True, |
| 113 | + accumulate_bad_lines=None |
106 | 114 | ): |
107 | 115 | all_keys = defaults._asdict() if check_keys else None |
108 | | - _parse(lines, type_map, special_handling, ans, all_keys) |
| 116 | + _parse(lines, type_map, special_handling, ans, all_keys, accumulate_bad_lines) |
109 | 117 |
|
110 | 118 |
|
111 | 119 | def create_options_class(keys): |
|
0 commit comments