Skip to content

Commit 13d8e5a

Browse files
author
jacook
committed
upd readme and add basic filtering
1 parent 48b98e0 commit 13d8e5a

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,71 @@
22

33
A module for easily creating and modifying nginx serverblock configurations in Python (including comments!).
44

5-
Work in progress.
5+
### Examples
6+
7+
Create an nginx serverblock and save it to file:
8+
9+
>>> import nginx
10+
>>> c = nginx.Conf()
11+
>>> u = nginx.Upstream('php',
12+
... nginx.Key('server', 'unix:/tmp/php-fcgi.socket')
13+
... )
14+
>>> c.add(u)
15+
>>> s = nginx.Server()
16+
>>> s.add(
17+
... nginx.Key('listen', '80'),
18+
... nginx.Comment('Yes, python-nginx can read/write comments!'),
19+
... nginx.Key('server_name', 'localhost 127.0.0.1'),
20+
... nginx.Key('root', '/srv/http'),
21+
... nginx.Key('index', 'index.php'),
22+
... nginx.Location('= /robots.txt',
23+
... nginx.Key('allow', 'all'),
24+
... nginx.Key('log_not_found', 'off'),
25+
... nginx.Key('access_log', 'off')
26+
... )
27+
... nginx.Location('~ \.php$',
28+
... nginx.Key('include', 'fastcgi.conf'),
29+
... nginx.Key('fastcgi_intercept_errors', 'on'),
30+
... nginx.Key('fastcgi_pass', 'php')
31+
... )
32+
... )
33+
>>> c.add(s)
34+
>>> nginx.dumpf(c, '/etc/nginx/sites-available/mysite')
35+
36+
Load an nginx serverblock from a file:
37+
38+
>>> import nginx
39+
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
40+
>>> c.all()
41+
[<main.Server object at 0x7f1ed4573890>]
42+
>>> c.servers[0].all()
43+
[<main.Comment object at 0x7f1ed45736d0>, <main.Key object at 0x7f1ed4573750>, <main.Key object at 0x7f1ed4573790>, <main.Location object at 0x7f1ed4573850>]
44+
>>> c.as_dict()
45+
{'conf': [{'server': [{'#': 'This is a test comment'}, {'server_name': 'localhost'}, {'root': '/srv/http'}, {'location /': [{'allow': 'all'}]}]}]}
46+
47+
Format an nginx serverblock into a string (change the amount of spaces (or tabs) for each indentation level by modifying `nginx.INDENT` first):
48+
49+
>>> c.all()
50+
[<main.Server object at 0x7f1ed4573890>]
51+
>>> c.as_block()
52+
['server {\n', ' # This is a test comment\n', ' server_name localhost;\n', ' root /srv/http;\n', '\n location / {\n', ' allow all;\n', ' }\n\n', '}\n']
53+
54+
Find where you put your keys:
55+
56+
>>> import nginx
57+
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
58+
>>> c.filter('Server')
59+
[<main.Server object at 0x7f1ed4573890>]
60+
>>> c.filter('Server')[0].filter('Key', 'root')
61+
[<main.Key object at 0x7f1ed4573790>]
62+
>>> c.filter('Server')[0].filter('Location')
63+
[<main.Location object at 0x7f1ed4573850>]
64+
65+
Or just get everything by its type:
66+
67+
>>> import nginx
68+
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
69+
>>> c.servers
70+
[<main.Server object at 0x7f1ed4573890>]
71+
>>> c.servers[0].keys
72+
[<main.Key object at 0x7f1ed4573750>, <main.Key object at 0x7f1ed4573790>]

main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ def remove(self, *args):
1818
self.upd()
1919
return self.blocks
2020

21+
def filter(self, btype='', name=''):
22+
flist = []
23+
for x in self.blocks:
24+
if name and isinstance(x, Key) and x.name == name:
25+
flist.append(x)
26+
elif not name and btype and x.__class__.__name__ == btype:
27+
flist.append(x)
28+
return flist
29+
2130
def upd(self):
2231
svr = []
2332
for x in self.blocks:
@@ -34,6 +43,9 @@ def as_list(self):
3443
ret.append(x.as_list())
3544
return ret
3645

46+
def as_dict(self):
47+
return {'conf': [x.as_dict() for x in self.blocks]}
48+
3749
def as_block(self):
3850
ret = []
3951
for x in self.blocks:
@@ -63,6 +75,15 @@ def remove(self, *args):
6375
self.upd()
6476
return self.blocks
6577

78+
def filter(self, btype='', name=''):
79+
flist = []
80+
for x in self.blocks:
81+
if name and isinstance(x, Key) and x.name == name:
82+
flist.append(x)
83+
elif not name and btype and x.__class__.__name__ == btype:
84+
flist.append(x)
85+
return flist
86+
6687
def upd(self):
6788
l, c, k = [], [], []
6889
for x in self.blocks:
@@ -83,6 +104,9 @@ def as_list(self):
83104
ret.append(x.as_list())
84105
return ['server', '', ret]
85106

107+
def as_dict(self):
108+
return {'server': [x.as_dict() for x in self.blocks]}
109+
86110
def as_block(self):
87111
ret = []
88112
ret.append('server {\n')
@@ -135,6 +159,9 @@ def as_list(self):
135159
ret.append(x.as_list())
136160
return [self.name, self.value, ret]
137161

162+
def as_dict(self):
163+
return {self.name+' '+self.value: [x.as_dict() for x in self.blocks]}
164+
138165
def as_block(self):
139166
ret = []
140167
ret.append(self.name + ' ' + self.value + ' {\n')
@@ -155,6 +182,9 @@ def __init__(self, comment):
155182
def as_list(self):
156183
return [self.comment]
157184

185+
def as_dict(self):
186+
return {'#': self.comment}
187+
158188
def as_block(self):
159189
return '# ' + self.comment + '\n'
160190

@@ -197,6 +227,9 @@ def __init__(self, name, value):
197227
def as_list(self):
198228
return [self.name, self.value]
199229

230+
def as_dict(self):
231+
return {self.name: self.value}
232+
200233
def as_block(self):
201234
return self.name + ' ' + self.value + ';\n'
202235

0 commit comments

Comments
 (0)