Skip to content

Commit

Permalink
feat: Add useful info when using on terminal
Browse files Browse the repository at this point in the history
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)>
  • Loading branch information
vinigfer committed Feb 21, 2023
1 parent c284cdb commit 88aac34
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ def __init__(self, comment, inline=False):
self.comment = comment
self.inline = inline

def __repr__(self):
return "<nginx.Comment object ({0})>".format(self.comment)

@property
def as_list(self):
"""Return comment as nested list of strings."""
Expand Down Expand Up @@ -299,6 +302,9 @@ def __init__(self, value, *args):
super(Location, self).__init__(value, *args)
self.name = 'location'

def __repr__(self):
return "<nginx.Location object ({0})>".format(self.value)


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

def __repr__(self):
return "<nginx.Upstream object ({0})>".format(self.value)


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

def __repr__(self):
return "<nginx.Map object ({0})>".format(self.value)


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

def __repr__(self):
return "<nginx.Key object ({0})>".format(self.name)

@property
def as_list(self):
"""Return key as nested list of strings."""
Expand Down
24 changes: 24 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
{
server unix:/tmp/php-fcgi.socket;
}
map $request_body $tmp_body
{
"" "-";
default $request_body;
}
server
{
listen 80; # This comment should be present;
Expand Down Expand Up @@ -354,6 +359,25 @@ def test_server_without_last_linebreak(self):
self.assertTrue(nginx.loads(TESTBLOCK_CASE_13) is not None)
self.assertTrue(nginx.loads(TESTBLOCK_CASE_14) is not None)

def test_useful_info_on_terminal(self):
data = nginx.loads(TESTBLOCK_CASE_2)

upstream = data.children[0]
self.assertEqual(str(upstream), '<nginx.Upstream object (php)>')

key = upstream.children[0]
self.assertEqual(str(key), '<nginx.Key object (server)>')

nginx_map = data.children[1]
self.assertEqual(str(nginx_map), '<nginx.Map object ($request_body $tmp_body)>')

server = data.children[2]
comment = server.children[1]
self.assertEqual(str(comment), '<nginx.Comment object (This comment should be present;)>')

location = server.children[-1]
self.assertEqual(str(location), '<nginx.Location object (/)>')


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

0 comments on commit 88aac34

Please sign in to comment.