Skip to content

Commit 4d49c70

Browse files
committed
feat: Add useful info when using on terminal
Instead of getting info about memory position, now we receive info that helps us navigate the nginx conf structure on some cases. For example: Before: <nginx.Key object at 0x7fe420e7c2b0> After: <nginx.Key object (log_format)>
1 parent c284cdb commit 4d49c70

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Diff for: .github/workflows/ci.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: Python package
22

3-
on: [push]
3+
on:
4+
push:
5+
pull_request:
6+
types: [opened, synchronize]
47

58
jobs:
69
build:
@@ -29,4 +32,4 @@ jobs:
2932
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3033
- name: Test with pytest
3134
run: |
32-
python tests.py
35+
python tests.py

Diff for: nginx.py

+15
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ def __init__(self, comment, inline=False):
252252
self.comment = comment
253253
self.inline = inline
254254

255+
def __repr__(self):
256+
return "<nginx.Comment object ({0})>".format(self.comment)
257+
255258
@property
256259
def as_list(self):
257260
"""Return comment as nested list of strings."""
@@ -299,6 +302,9 @@ def __init__(self, value, *args):
299302
super(Location, self).__init__(value, *args)
300303
self.name = 'location'
301304

305+
def __repr__(self):
306+
return "<nginx.Location object ({0})>".format(self.value)
307+
302308

303309
class Events(Container):
304310
"""Container for Event-based options."""
@@ -344,6 +350,9 @@ def __init__(self, value, *args):
344350
super(Upstream, self).__init__(value, *args)
345351
self.name = 'upstream'
346352

353+
def __repr__(self):
354+
return "<nginx.Upstream object ({0})>".format(self.value)
355+
347356

348357
class Geo(Container):
349358
"""
@@ -366,6 +375,9 @@ def __init__(self, value, *args):
366375
super(Map, self).__init__(value, *args)
367376
self.name = 'map'
368377

378+
def __repr__(self):
379+
return "<nginx.Map object ({0})>".format(self.value)
380+
369381

370382
class Stream(Container):
371383
"""Container for stream sections in the main NGINX conf file."""
@@ -388,6 +400,9 @@ def __init__(self, name, value):
388400
self.name = name
389401
self.value = value
390402

403+
def __repr__(self):
404+
return "<nginx.Key object ({0})>".format(self.name)
405+
391406
@property
392407
def as_list(self):
393408
"""Return key as nested list of strings."""

Diff for: tests.py

+24
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
{
4040
server unix:/tmp/php-fcgi.socket;
4141
}
42+
map $request_body $tmp_body
43+
{
44+
"" "-";
45+
default $request_body;
46+
}
4247
server
4348
{
4449
listen 80; # This comment should be present;
@@ -354,6 +359,25 @@ def test_server_without_last_linebreak(self):
354359
self.assertTrue(nginx.loads(TESTBLOCK_CASE_13) is not None)
355360
self.assertTrue(nginx.loads(TESTBLOCK_CASE_14) is not None)
356361

362+
def test_useful_info_on_terminal(self):
363+
data = nginx.loads(TESTBLOCK_CASE_2)
364+
365+
upstream = data.children[0]
366+
self.assertEqual(str(upstream), '<nginx.Upstream object (php)>')
367+
368+
key = upstream.children[0]
369+
self.assertEqual(str(key), '<nginx.Key object (server)>')
370+
371+
nginx_map = data.children[1]
372+
self.assertEqual(str(nginx_map), '<nginx.Map object ($request_body $tmp_body)>')
373+
374+
server = data.children[2]
375+
comment = server.children[1]
376+
self.assertEqual(str(comment), '<nginx.Comment object (This comment should be present;)>')
377+
378+
location = server.children[-1]
379+
self.assertEqual(str(location), '<nginx.Location object (/)>')
380+
357381

358382
if __name__ == '__main__':
359383
unittest.main()

0 commit comments

Comments
 (0)