-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexoez.py
executable file
·178 lines (156 loc) · 5.06 KB
/
hexoez.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
https://github.com/acupt/acupscript
"""
import os
import sys
import re
def rewrite(file, op, target, argv):
file_data = ""
state = 0;
values = []
old_values = []
with open(file, "r") as f:
for line in f:
if state == 0: # 属性区域之前
if line.startswith('---'):
state = 1
elif state == 1: # 属性区域之中,目标属性之前
if line.startswith(target + ':'):
if line.strip() == target + ':': # 参数在下一行,或者没有
state = 2
else: # 参数就在本行
values = get_values(line)
old_values = values[:]
op_values(op, values, argv)
line = "%s: %s\n" % (target, line_single(values))
state = 3
elif state == 2: # 属性区域之中,目标属性之中
if line.startswith(' ') or line.startswith('- '):
values.append(get_value(line))
continue
else:
old_values = values[:]
op_values(op, values, argv)
line = line_multi(values) + line
state = 3
file_data += line
with open(file, "w") as f:
f.write(file_data)
print("%s %s %s %s -> %s" % (file, op, target, old_values, values))
return 1
return 0
def get_values(line):
line = str(line)
line = line[line.index(':') + 1:]
line = line.strip()
values = []
if line.startswith('[') and line.endswith(']'):
# [1,2,"a"]
line = line[1:len(line) - 1]
for s in line.split(','):
s = s.strip()
if s.startswith('"') and s.endswith('"'):
values.append(s[1:len(s) - 1])
else:
values.append(s)
else:
if line.startswith('"') and line.endswith('"'):
values.append(line[1:len(line) - 1])
else:
values.append(line)
return values
def get_value(line):
line = str(line)
start = 0;
for i in range(len(line)):
if line[i] != ' ' and line[i] != '-':
start = i
break
if start == len(line) - 1:
return ''
if line[start] == '"':
end = line.rindex('"')
if start == end:
line = line[start + 1:]
else:
line = line[start + 1:end]
else:
line = line[start:]
return line.strip(" \n")
def op_values(op='', values=[], argv=[]):
if op == 'add':
for a in argv:
if a not in values:
values.append(a)
elif op == 'del':
for v in values:
for a in argv:
if v == a:
values.remove(v)
break
elif op == 'update':
for i in range(0, len(values)):
if values[i] == argv[0]:
values[i] = argv[1]
def line_multi(values=[]):
line = ''
for v in values:
v = safe_str(v)
line += ' - %s\n' % v
return line
def line_single(values=[]):
if len(values) == 0:
return ''
if len(values) == 1:
return safe_str(values[0])
line = '['
for v in values:
v = safe_str(v)
if len(line) > 1:
line += ','
line += v
return line + ']'
def safe_str(s):
s = str(s)
if ' ' in s and not s.startswith('"'):
s = '"%s"' % s
return s
def search(curpath, pattern, files=[]):
list = os.listdir(curpath) # 列出当前目录下所有文件
for subpath in list: # 遍历当前目录所有文件
if os.path.isdir(os.path.join(curpath, subpath)): # 若文件仍为目录,递归查找子目录
newpath = os.path.join(curpath, subpath)
search(newpath, pattern, files)
elif os.path.isfile(os.path.join(curpath, subpath)): # 若为文件,判断是否包含搜索字串
if not subpath.endswith('.md'):
continue
if re.match(pattern, subpath):
print("scan %s" % os.path.join(curpath, subpath))
files.append(os.path.join(curpath, subpath))
if __name__ == '__main__':
if len(sys.argv) <= 4:
print("argv missing:\npython hexoez.py <op> <target> [argv] [file]")
sys.exit(2)
op = sys.argv[1]
target = sys.argv[2]
file = str(sys.argv[len(sys.argv) - 1])
argv = []
for i in range(3, len(sys.argv) - 1):
argv.append(sys.argv[i])
print("op\t= %s" % op)
print("target\t= %s" % target)
print("file\t= %s" % file)
print("argv\t= %s" % str(argv))
if file.endswith('.md'):
rewrite(file, op, target, argv)
exit(0)
files = []
workingpath = os.path.abspath(file)
search(workingpath, '.', files)
change_num = 0
for f in files:
pass
change_num += rewrite(f, op, target, argv)
print("%d scanned in %s, %d modified" % (len(files), file, change_num))