Skip to content

Commit

Permalink
remove useless empty lines in json and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
darthbear committed Dec 12, 2014
1 parent 40600ca commit 8fe2b09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def convert_number(tokens):
# TODO: find a way to make comma optional and yet works with multilines
list_elements = any_expr - Optional(eol_comma) - ZeroOrMore(any_expr - Optional(eol_comma))
list_expr << ListParser(
Suppress('[]') | (Suppress('[') - Optional(eol) - Optional(list_elements) - Suppress(']'))) + Optional(
(Suppress('[') - Optional(eol) - Optional(list_elements) - Suppress(']'))) + Optional(
comment | eol_comma)

# for a dictionary : or = is optional
Expand Down
46 changes: 25 additions & 21 deletions pyhocon/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ def to_json(config, level=0):
"""
lines = ""
if isinstance(config, ConfigTree):
lines += '{\n'
bet_lines = []
for key, item in config.items():
bet_lines.append('{indent}"{key}": {value}'.format(
indent=''.rjust((level + 1) * 2, ' '),
key=key,
value=HOCONConverter.to_json(item, level + 1))
)
lines += ',\n'.join(bet_lines)
lines += '\n{indent}}}'.format(indent=''.rjust(level * 2, ' '))
if len(config) == 0:
lines += '{}'
else:
lines += '{\n'
bet_lines = []
for key, item in config.items():
bet_lines.append('{indent}"{key}": {value}'.format(
indent=''.rjust((level + 1) * 2, ' '),
key=key,
value=HOCONConverter.to_json(item, level + 1))
)
lines += ',\n'.join(bet_lines)
lines += '\n{indent}}}'.format(indent=''.rjust(level * 2, ' '))
elif isinstance(config, list):
if len(config) == 0:
lines += '[]'
Expand All @@ -47,16 +50,17 @@ def to_yaml(config, level=0):
"""
lines = ""
if isinstance(config, ConfigTree):
if level > 0:
lines += '\n'
bet_lines = []
for key, item in config.items():
bet_lines.append('{indent}{key}: {value}'.format(
indent=''.rjust((level + 1) * 2, ' '),
key=key,
value=HOCONConverter.to_yaml(item, level + 1))
)
lines += '\n'.join(bet_lines)
if len(config) > 0:
if level > 0:
lines += '\n'
bet_lines = []
for key, item in config.items():
bet_lines.append('{indent}{key}: {value}'.format(
indent=''.rjust((level + 1) * 2, ' '),
key=key,
value=HOCONConverter.to_yaml(item, level + 1))
)
lines += '\n'.join(bet_lines)
elif isinstance(config, list):
if len(config) == 0:
lines += '[]'
Expand Down Expand Up @@ -97,7 +101,7 @@ def escape_value(value):
lines.append('.'.join(key_stack) + ' = ' + escape_value(config))
else:
lines.append('.'.join(key_stack) + ' = ' + str(config))
return '\n'.join(lines)
return '\n'.join([line for line in lines if len(line) > 0])

@staticmethod
def convert(format):
Expand Down
2 changes: 1 addition & 1 deletion samples/database.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
databases {
# MySQL
"mysql" {
"mysql" = {
host = "abc.com" # change it
port = 3306 # default
username: scott // can use : or =
Expand Down

0 comments on commit 8fe2b09

Please sign in to comment.