Skip to content

Added support for comments between arguments #68

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

Merged
merged 1 commit into from
Apr 8, 2019
Merged
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
18 changes: 17 additions & 1 deletion crossplane/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def _parse(parsing, tokens, ctx=(), consume=False):

# parse recursively by pulling from a flat stream of tokens
for token, lineno, quoted in tokens:
comments_in_args = []

# we are parsing a block, so break if it's closing
if token == '}' and not quoted:
break
Expand Down Expand Up @@ -118,7 +120,11 @@ def _parse(parsing, tokens, ctx=(), consume=False):
args = stmt['args']
token, __, quoted = next(tokens) # disregard line numbers of args
while token not in ('{', ';', '}') or quoted:
stmt['args'].append(token)
if token.startswith('#') and not quoted:
comments_in_args.append(token[1:])
else:
stmt['args'].append(token)

token, __, quoted = next(tokens)

# consume the directive if it is ignored and move on
Expand Down Expand Up @@ -196,6 +202,16 @@ def _parse(parsing, tokens, ctx=(), consume=False):

parsed.append(stmt)

# add all comments found inside args after stmt is added
for comment in comments_in_args:
comment_stmt = {
'directive': '#',
'line': stmt['line'],
'args': [],
'comment': comment
}
parsed.append(comment_stmt)

return parsed

# the includes list grows as "include" directives are found in _parse
Expand Down
7 changes: 7 additions & 0 deletions tests/configs/comments-between-args/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http { #comment 1
log_format #comment 2
\#arg\ 1 #comment 3
'#arg 2' #comment 4
#comment 5
;
}
31 changes: 31 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ def test_build_with_quoted_unicode():
assert built == u"env 'русский текст';"


def test_build_multiple_comments_on_one_line():
payload = [
{
"directive": "#",
"line": 1,
"args": [],
"comment": "comment1"
},
{
"directive": "user",
"line": 2,
"args": ["root"]
},
{
"directive": "#",
"line": 2,
"args": [],
"comment": "comment2"
},
{
"directive": "#",
"line": 2,
"args": [],
"comment": "comment3"
}
]
built = crossplane.build(payload, indent=4, tabs=False)
assert built == '#comment1\nuser root; #comment2 #comment3'



def test_build_files_with_missing_status_and_errors(tmpdir):
assert len(tmpdir.listdir()) == 0
payload = {
Expand Down
63 changes: 62 additions & 1 deletion tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def test_config_with_comments():
}
],
"status" : "ok",
"file" : os.path.join(dirname, 'nginx.conf')
"file" : config
}
]
}
Expand Down Expand Up @@ -914,3 +914,64 @@ def test_combine_parsed_missing_values():
}
]
}


def test_comments_between_args():
dirname = os.path.join(here, 'configs', 'comments-between-args')
config = os.path.join(dirname, 'nginx.conf')
payload = crossplane.parse(config, comments=True)
assert payload == {
'status': 'ok',
'errors': [],
'config': [
{
'file': config,
'status': 'ok',
'errors': [],
'parsed': [
{
'directive': 'http',
'line': 1,
'args': [],
'block': [
{
'directive': '#',
'line': 1,
'args': [],
'comment': 'comment 1'
},
{
'directive': 'log_format',
'line': 2,
'args': ['\\#arg\\ 1', '#arg 2']
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 2'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 3'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 4'
},
{
'directive': '#',
'line': 2,
'args': [],
'comment': 'comment 5'
}
]
}
]
}
]
}