-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcap_params.py
More file actions
executable file
·32 lines (28 loc) · 955 Bytes
/
cap_params.py
File metadata and controls
executable file
·32 lines (28 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
'''
script to find and optionally replace param refs in rst files
'''
import re
from argparse import ArgumentParser
parser = ArgumentParser('find and optionally replace parameters')
parser.add_argument("--change", action='store_true', help="change matches to use param markup")
parser.add_argument("files", nargs='+')
args = parser.parse_args()
for f in args.files:
print(f"Processing {f}")
txt = open(f, 'r').read()
matches = re.findall(r'[,.\s][A-Z][A-Z0-9]+_[A-Z_]+[,.\s]', txt)
matches = re.findall(r'[,.\s][A-Z]+_[A-Z_]+[,.\s]', txt)
changed = False
for m in matches:
s = str(m)
param = s.strip()
s2 = f"{s[0]}:ref:`{param}<{param}>`{s[-1]}"
if args.change:
txt = txt.replace(s, s2)
changed = True
print(f"Replaced [{s}] with [{s2}]")
else:
print(f"Found [{s}]")
if changed:
open(f, 'w').write(txt)