"""doc_add.py AFTER_SUFFIX NEW_SUFFIX TEXT: after every docs/fields line whose key ends with AFTER_SUFFIX, a line for the sibling key ending NEW_SUFFIX (run from the repository root).""" import glob, json, sys after, new, text = sys.argv[1:4] for p in glob.glob('docs/fields/*.json'): lines = open(p).read().split('\n'); out = []; n = 0 for l in lines: out.append(l) k = l.strip().split('"')[1] if l.strip().startswith('"') else '' if k.endswith(after): key = k[:-len(after)] + new if f'"{key}"' not in ''.join(lines): out.append(l[:len(l) - len(l.lstrip())] + json.dumps(key) + ': ' + json.dumps(text) + ',') n += 1 if n: s = '\n'.join(out); json.loads(s); open(p, 'w').write(s); print(p, n)