forked from peakwinter/python-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx.py
More file actions
executable file
·311 lines (255 loc) · 8.22 KB
/
nginx.py
File metadata and controls
executable file
·311 lines (255 loc) · 8.22 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import re
INDENT = ' '
class Conf(object):
def __init__(self, *args):
self.blocks = list(args)
self.servers = []
self.upd()
def add(self, *args):
self.blocks.extend(args)
self.upd()
return self.blocks
def remove(self, *args):
for x in args:
self.blocks.remove(x)
self.upd()
return self.blocks
def filter(self, btype='', name=''):
flist = []
for x in self.blocks:
if name and isinstance(x, Key) and x.name == name:
flist.append(x)
elif isinstance(x, Container) and x.__class__.__name__ == btype and x.value == name:
flist.append(x)
elif not name and btype and x.__class__.__name__ == btype:
flist.append(x)
return flist
def upd(self):
svr = []
for x in self.blocks:
if isinstance(x, Server):
svr.append(x)
self.servers = svr
def all(self):
return self.blocks
def as_list(self):
ret = []
for x in self.blocks:
ret.append(x.as_list())
return ret
def as_dict(self):
return {'conf': [x.as_dict() for x in self.blocks]}
def as_block(self):
ret = []
for x in self.blocks:
if isinstance(x, (Key, Comment)):
ret.append(x.as_block())
else:
for y in x.as_block():
ret.append(y)
return ret
class Server(object):
def __init__(self, *args):
self.blocks = list(args)
self.locations = []
self.comments = []
self.keys = []
self.upd()
def add(self, *args):
self.blocks.extend(args)
self.upd()
return self.blocks
def remove(self, *args):
for x in args:
self.blocks.remove(x)
self.upd()
return self.blocks
def filter(self, btype='', name=''):
flist = []
for x in self.blocks:
if name and isinstance(x, Key) and x.name == name:
flist.append(x)
elif isinstance(x, Container) and x.__class__.__name__ == btype and x.value == name:
flist.append(x)
elif not name and btype and x.__class__.__name__ == btype:
flist.append(x)
return flist
def upd(self):
l, c, k = [], [], []
for x in self.blocks:
if isinstance(x, Location):
l.append(x)
elif isinstance(x, Comment):
c.append(x)
elif isinstance(x, Key):
k.append(x)
self.locations, self.comments, self.keys = l, c, k
def all(self):
return self.blocks
def as_list(self):
ret = []
for x in self.blocks:
ret.append(x.as_list())
return ['server', '', ret]
def as_dict(self):
return {'server': [x.as_dict() for x in self.blocks]}
def as_block(self):
ret = []
ret.append('server {\n')
for x in self.blocks:
if isinstance(x, (Key, Comment)):
ret.append(INDENT + x.as_block())
elif isinstance(x, Container):
y = x.as_block()
ret.append('\n'+INDENT+y[0])
for z in y[1:]:
ret.append(INDENT+z)
ret.append('}\n')
return ret
class Container(object):
def __init__(self, value, *args):
self.name = ''
self.value = value
self.comments = []
self.keys = []
self.blocks = list(args)
self.upd()
def add(self, *args):
self.blocks.extend(args)
self.upd()
return self.blocks
def remove(self, *args):
for x in args:
self.blocks.remove(x)
self.upd()
return self.blocks
def upd(self):
c, k = [], []
for x in self.blocks:
if isinstance(x, Comment):
c.append(x)
elif isinstance(x, Key):
k.append(x)
self.comments, self.keys = c, k
def all(self):
return self.blocks
def as_list(self):
ret = []
for x in self.blocks:
ret.append(x.as_list())
return [self.name, self.value, ret]
def as_dict(self):
return {'{} {}'.format(self.name, self.value): [x.as_dict() for x in self.blocks]}
def as_block(self):
ret = []
ret.append('{} {} {{\n'.format(self.name, self.value))
for x in self.blocks:
if isinstance(x, (Key, Comment)):
ret.append(INDENT + x.as_block())
elif isinstance(x, Container):
y = x.as_block()
ret.append('\n'+INDENT+INDENT+y[0])
for z in y[1:]:
ret.append(INDENT+z)
else:
y = x.as_block()
ret.append(INDENT+y)
ret.append('}\n')
return ret
class Comment(object):
def __init__(self, comment):
self.comment = comment
def as_list(self):
return [self.comment]
def as_dict(self):
return {'#': self.comment}
def as_block(self):
return '# {}\n'.format(self.comment)
class Location(Container):
def __init__(self, value, *args):
super(Location, self).__init__(value, *args)
self.name = 'location'
class LimitExcept(Container):
def __init__(self, value, *args):
super(LimitExcept, self).__init__(value, *args)
self.name = 'limit_except'
class Types(Container):
def __init__(self, value, *args):
super(Types, self).__init__(value, *args)
self.name = 'types'
class If(Container):
def __init__(self, value, *args):
super(If, self).__init__(value, *args)
self.name = 'if'
class Upstream(Container):
def __init__(self, value, *args):
super(Upstream, self).__init__(value, *args)
self.name = 'upstream'
class Key(object):
def __init__(self, name, value):
self.name = name
self.value = value
def as_list(self):
return [self.name, self.value]
def as_dict(self):
return {self.name: self.value}
def as_block(self):
return '{} {};\n'.format(self.name, self.value)
def loads(data, conf=True):
f = Conf() if conf else []
lopen = []
for line in data.split('\n'):
if re.match('\s*server\s*{', line):
s = Server()
lopen.insert(0, s)
elif re.match('\s*location.*{', line):
lpath = re.match('\s*location\s*(.*\S+)\s*{', line).group(1)
l = Location(lpath)
lopen.insert(0, l)
elif re.match('\s*if.*{', line):
ifs = re.match('\s*if\s*(.*\S+)\s*{', line).group(1)
ifs = If(ifs)
lopen.insert(0, ifs)
elif re.match('\s*upstream.*{', line):
ups = re.match('\s*upstream\s*(.*\S+)\s*{', line).group(1)
u = Upstream(ups)
lopen.insert(0, u)
elif re.match('\s*#\s*', line):
c = Comment(re.match('\s*#\s*(.*)$', line).group(1))
if len(lopen):
lopen[0].add(c)
else:
f.add(c) if conf else f.append(c)
elif re.match('.*;', line):
kname, kval = re.match('.*(?:^|^\s*|{\s*)(\S+)\s(.+);', line).group(1, 2)
k = Key(kname, kval)
lopen[0].add(k)
elif re.match('.*}', line):
closenum = len(re.findall('}', line))
while closenum > 0:
if isinstance(lopen[0], Server):
f.add(lopen[0]) if conf else f.append(lopen[0])
lopen.pop(0)
elif isinstance(lopen[0], Container):
c = lopen[0]
lopen.pop(0)
if lopen:
lopen[0].add(c)
else:
f.add(c) if conf else f.append(c)
closenum = closenum - 1
return f
def load(fobj):
return loads(fobj.read())
def loadf(path):
with open(path, 'r') as f:
return load(f)
def dumps(obj):
return ''.join(obj.as_block())
def dump(obj, fobj):
fobj.write(dumps(obj))
return fobj
def dumpf(obj, path):
with open(path, 'w') as f:
dump(obj, f)
return path