-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
replace.py
executable file
·54 lines (39 loc) · 1.58 KB
/
replace.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
"""Simple script to replace a list of regex patterns in the input file.
WARNING: this modifies the input file in place!!! It does leave file~ as a
backup.
Usage::
replace.py <input filename>
"""
#-----------------------------------------------------------------------------
# Configure here - define replacement list
#-----------------------------------------------------------------------------
patterns = [(r'\\documentclass{article}',
r'\\documentclass[11pt]{article}\n \\usepackage{palatino}'),
(r'^\s+IPython 2013 Progress Report - Sloan Foundation', ''),
(r'1.15 million', r'\$1.15 million'),
]
# You shouldn't need to configure any further code.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import re
import sys
#-----------------------------------------------------------------------------
# Main code
#-----------------------------------------------------------------------------
# Compile regexes first. That way, if there's an error in any of them, we fail
# early before touching any files.
replacements = [ (re.compile(pat, re.MULTILINE), rep)
for (pat, rep) in patterns ]
# Rename input file, read it in, run over the regexes and write output file
fname = sys.argv[1]
backup = fname+'~'
os.rename(fname, backup)
with open(backup) as fin:
out = fin.read()
for rx, rep in replacements:
out = rx.sub(rep, out)
with open(fname, 'w') as f:
f.write(out)