| 38 | |
| 39 | |
| 40 | class HeaderGroups(dict): |
| 41 | def __init__(self, tag): |
| 42 | """ |
| 43 | A dictionary that also can read headers given a tag expression. |
| 44 | |
| 45 | TODO: might have gone overboard on this one, could maybe be two functions. |
| 46 | """ |
| 47 | self.re_matcher = re.compile( |
| 48 | tag_str.format(tag=tag), re.MULTILINE | re.DOTALL | re.VERBOSE |
| 49 | ) |
| 50 | super(HeaderGroups, self).__init__() |
| 51 | |
| 52 | def read_header(self, filename): |
| 53 | """ |
| 54 | Read a header file in and add items to the dict, based on the item's action. |
| 55 | """ |
| 56 | with open(filename) as f: |
| 57 | inner = f.read() |
| 58 | |
| 59 | matches = self.re_matcher.findall(inner) |
| 60 | |
| 61 | if not matches: |
| 62 | warnings.warn( |
| 63 | "Failed to find any matches in {filename}".format(filename=filename) |
| 64 | ) |
| 65 | |
| 66 | for name, action, content in matches: |
| 67 | if action == "verbatim": |
| 68 | assert ( |
| 69 | name not in self |
| 70 | ), "{name} read in more than once! Quitting.".format(name=name) |
| 71 | self[name] = content |
| 72 | elif action == "set": |
| 73 | self[name] = self.get(name, set()) | set(content.strip().splitlines()) |
| 74 | else: |
| 75 | raise RuntimeError("Action not understood, must be verbatim or set") |
| 76 | |
| 77 | def post_process(self): |
| 78 | """ |
| 79 | Turn sets into multiple line strings. |
| 80 | """ |
| 81 | for key in self: |
| 82 | if isinstance(self[key], set): |
| 83 | self[key] = "\n".join(sorted(self[key])) |
| 84 | |
| 85 | |
| 86 | def make_header(output, main_header, files, tag, namespace, macro=None, version=None): |