Load rules metadata and text from file at ``location``. Return a list of RuleData.
(location="00-new-licenses.txt")
| 82 | |
| 83 | |
| 84 | def load_data(location="00-new-licenses.txt"): |
| 85 | """ |
| 86 | Load rules metadata and text from file at ``location``. Return a list of |
| 87 | RuleData. |
| 88 | """ |
| 89 | with open(location) as o: |
| 90 | lines = o.read().splitlines(False) |
| 91 | |
| 92 | rules = [] |
| 93 | |
| 94 | data_lines = [] |
| 95 | text_lines = [] |
| 96 | in_data = False |
| 97 | in_text = False |
| 98 | last_lines = [] |
| 99 | for ln, line in enumerate(lines, 1): |
| 100 | last_lines.append(": ".join([str(ln), line])) |
| 101 | if line == "----------------------------------------": |
| 102 | if not (ln == 1 or in_text): |
| 103 | raise Exception( |
| 104 | "Invalid structure: #{ln}: {line}\n".format(**locals()) |
| 105 | +"\n".join(last_lines[-10:]) |
| 106 | ) |
| 107 | |
| 108 | in_data = True |
| 109 | in_text = True |
| 110 | if data_lines and "".join(text_lines).strip(): |
| 111 | rules.append(RuleData(data_lines=data_lines, text_lines=text_lines)) |
| 112 | |
| 113 | data_lines = [] |
| 114 | text_lines = [] |
| 115 | continue |
| 116 | |
| 117 | if line == "---": |
| 118 | if not in_data: |
| 119 | raise Exception( |
| 120 | "Invalid structure: #{ln}: {line}\n".format(**locals()) |
| 121 | +"\n".join(last_lines[-10:]) |
| 122 | ) |
| 123 | |
| 124 | in_data = False |
| 125 | in_text = True |
| 126 | continue |
| 127 | |
| 128 | if in_data: |
| 129 | data_lines.append(line) |
| 130 | continue |
| 131 | |
| 132 | if in_text: |
| 133 | text_lines.append(line) |
| 134 | continue |
| 135 | |
| 136 | return rules |
| 137 | |
| 138 | |
| 139 | def all_rule_by_tokens(): |