Skip to content

feat: Add useful info when using on terminal #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Python package

on: [push]
on:
push:
pull_request:
types: [opened, synchronize]

jobs:
build:
Expand Down Expand Up @@ -29,4 +32,4 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
python tests.py
python tests.py
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()