forked from agavrel/42_CheatSheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seweolo140416.py
54 lines (43 loc) · 1.22 KB
/
seweolo140416.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
import sys
import os
import re
if len(sys.argv) < 3:
print("python 'Network' '[How to insert into markdown](www.github.com/agavrel)' '{optional}Author'")
exit()
topic = sys.argv[1]
pattern = "### 0x[0-9a-f][0-9af] " + topic
filename="README.md"
s = r"cat "+ filename +" | grep -n '" + pattern + "' " + filename + " | awk -F ':' '/0/ {print$1}'"
print(s)
pattern = re.compile("###")
stream = os.popen(s)
lineNb = int(stream.read().rstrip())
with open(filename, "r") as f:
for _ in range(lineNb):
next(f)
for line_i, line in enumerate(f, 1):
if re.search(pattern, line):
index = line_i + lineNb - 1
print( "%d\n" % index )
break
with open(filename, "r") as f:
contents = f.readlines()
s = "**" + sys.argv[2] + "** | :star::star::star: |"
if len(sys.argv) > 3:
s += " *by " + sys.argv[3] + "*"
contents.insert(index - 2, s + "\n")
with open(filename, "w") as f:
contents = "".join(contents)
f.write(contents)
s = "grep -C 4 -F '" + sys.argv[2] + "' README.md"
print(s)
os.system(s)
# if writing to a different file
#outputFileName="b.md"
#f2 = open(outputFileName, "r")
#contents = f2.readlines()
#f2.close()
#f2 = open(outputFileName, "w")
#contents = "".join(contents)
#f2.write(contents)
#f2.close()