forked from tamarin-prover/manual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
executable file
·129 lines (105 loc) · 3.05 KB
/
filter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""
Drop-in preprocessor to replace pandoc's 'slice' and 'include' extensions.
E.g.:
~~~~ {.tamarin slice="code/Naxos.spthy" lower=16 upper=32}
~~~~
"""
import re
def extractexclude(l, regexp):
"""
Take a text line, and check for the regexp, which is supposed to have one defined group.
Extract the group if possible: if so, remove the entire regexp from the
line and return (newline without regexp, group(1))
If not, just return (l,None)
This is useful for extracting values from "var=value" type strings and then
removing them form the source string.
"""
match = re.search(regexp,l)
if match:
res = match.group(1)
# print l
# print match.start()
# print match.end()
# print res
newline = l[:match.start()] + l[match.end():]
return (newline,res)
else:
return (l, None)
def includefile(filename,lower=0,upper=-1):
"""
Return an array representing a slice of the lines of a file. If no lower
and upper are given, return the entire file. Indices start from zero.
"""
fp = open(filename,'r')
i = 0
res = []
for l in fp:
if i >= lower:
if (i <= upper or upper == -1):
res += [l]
i += 1
fp.close()
return res
def filterinput(l,filename):
"""
Basic include function; returns array of lines including l
"""
mid = includefile(filename)
return [l] + mid
def filterslice(l,filename):
"""
Slice function; extract lower and upper as well, return array including cleaned-up l
"""
(l,res) = extractexclude(l, r'lower\s*=\s*(\d*)')
if res != None:
lower = int(res)
(l,res) = extractexclude(l, r'upper\s*=\s*(\d*)')
if res != None:
upper = int(res)
mid = includefile(filename,lower,upper)
return [l] + mid
return None
def filtercheck(l):
"""
Check for l for any relevant filters and execute
"""
(l, res) = extractexclude(l, r'include\s*=\s*"([^"]*)"')
if res != None:
return filterinput(l,res)
(l, res) = extractexclude(l, r'slice\s*=\s*"([^"]*)"')
if res != None:
return filterslice(l,res)
return None
def filter(infn,outfn):
"""
Check for codeblock which might contain filters
"""
codeblock = "~~~~"
fpi = open(infn, 'r')
res = []
for l in fpi:
res += [l]
if l.startswith(codeblock):
if len(res) >= 2:
if res[-2].startswith(codeblock):
ins = filtercheck(res[-2])
if ins != None:
res = res[:-2] + ins + [l]
fpi.close()
fpo = open(outfn, 'w')
for l in res:
fpo.write(l)
fpo.close()
def main():
"""
./filter.py infile outfile
Read argument one, produce argument two
"""
import sys
if len(sys.argv) >= (1+2):
filter(sys.argv[1],sys.argv[2])
else:
print("Need two arguments: infile and outfile.")
if __name__ == '__main__':
main()