| #!/usr/bin/env python3 |
| |
| import hashlib |
| import os |
| import pprint |
| import re |
| import sys |
| import textwrap |
| |
| from pathlib import Path |
| |
| |
| NON_ALPHA = re.compile('[^a-z._]') |
| |
| |
| def filename(s): |
| s = s.lower() |
| return NON_ALPHA.sub('_', s) |
| |
| |
| def get_line(lines): |
| l = lines.pop(0) |
| if l.startswith('* '): |
| l = l[2:] |
| elif l.startswith('*'): |
| l = l[1:] |
| return l |
| |
| def main(argv): |
| |
| for fname in Path(argv[1]).rglob("*.comments"): |
| basename = str(fname)[:-len('.comments')] |
| odir = os.path.dirname(fname) |
| |
| tables = [[]] |
| |
| with open(fname) as f: |
| lines = f.read().splitlines() |
| |
| while lines: |
| l = get_line(lines) |
| if l.lower().endswith('table'): |
| tables[-1].append(l) |
| |
| while lines: |
| l = get_line(lines) |
| if not l: |
| break |
| tables[-1].append(l) |
| tables.append([]) |
| continue |
| |
| if not tables[-1]: |
| tables.pop(-1) |
| |
| tables_out = {} |
| for i in range(0, len(tables)): |
| table_name = tables[i].pop(0) |
| table_data = textwrap.dedent('\n'.join(tables[i])) |
| |
| ofile = "{}.{}.table".format(basename, filename(table_name)) |
| |
| data = '\n'.join([table_name, '', '', table_data]) |
| md5sum = hashlib.md5(data.encode('utf-8')).hexdigest() |
| ofile_sum = os.path.join(odir, "{}.{}.table".format(filename(table_name), md5sum)) |
| |
| if not os.path.exists(ofile_sum): |
| with open(ofile_sum, 'w') as f: |
| f.write(data) |
| print("Wrote", ofile_sum) |
| print('Linked', ofile_sum, "to", ofile) |
| |
| return 0 |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main(sys.argv)) |