Add support for variable marks ${ and }. Fixes #4

pull/12/head
Michał Słomkowski 7 years ago
parent 09f0b8a700
commit 4488f2ee63

@ -7,6 +7,7 @@ way, described below:
* neighbouring empty lines are collapsed to at most two empty lines * neighbouring empty lines are collapsed to at most two empty lines
* curly braces placement follows Java convention * curly braces placement follows Java convention
* whitespaces are collapsed, except in comments an quotation marks * whitespaces are collapsed, except in comments an quotation marks
* whitespaces in variable designators are removed: `${ my_variable }` is collapsed to `${my_variable}`
## Installation ## Installation

@ -1,7 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Script formats nginx configuration file.""" """This Python script formats nginx configuration files in consistent way.
Originally published under https://github.com/1connect/nginx-config-formatter
"""
import argparse import argparse
import codecs import codecs
@ -10,9 +13,13 @@ import re
__author__ = "Michał Słomkowski" __author__ = "Michał Słomkowski"
__license__ = "Apache 2.0" __license__ = "Apache 2.0"
__version__ = "1.0.1"
INDENTATION = ' ' * 4 INDENTATION = ' ' * 4
TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___'
TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
def strip_line(single_line): def strip_line(single_line):
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks).""" """Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
@ -31,19 +38,37 @@ def strip_line(single_line):
return '"'.join(parts) return '"'.join(parts)
def apply_variable_template_tags(line: str) -> str:
"""Replaces variable indicators ${ and } with tags, so subsequent formatting is easier."""
return re.sub(r'\${\s*(\w+)\s*}',
TEMPLATE_VARIABLE_OPENING_TAG + r"\1" + TEMPLATE_VARIABLE_CLOSING_TAG,
line,
flags=re.UNICODE)
def strip_variable_template_tags(line: str) -> str:
"""Replaces tags back with ${ and } respectively."""
return re.sub(TEMPLATE_VARIABLE_OPENING_TAG + r'\s*(\w+)\s*' + TEMPLATE_VARIABLE_CLOSING_TAG,
r'${\1}',
line,
flags=re.UNICODE)
def clean_lines(orig_lines): def clean_lines(orig_lines):
"""Strips the lines and splits them if they contain curly brackets.""" """Strips the lines and splits them if they contain curly brackets."""
cleaned_lines = [] cleaned_lines = []
for line in orig_lines: for line in orig_lines:
line = strip_line(line) line = strip_line(line)
line = apply_variable_template_tags(line)
if line == "": if line == "":
cleaned_lines.append("") cleaned_lines.append("")
continue continue
else: else:
if line.startswith("#"): if line.startswith("#"):
cleaned_lines.append(line) cleaned_lines.append(strip_variable_template_tags(line))
else: else:
cleaned_lines.extend([l.strip() for l in re.split("([\\{\\}])", line) if l != ""]) cleaned_lines.extend(
[strip_variable_template_tags(l).strip() for l in re.split(r"([{\\}])", line) if l != ""])
return cleaned_lines return cleaned_lines

@ -14,9 +14,12 @@ __license__ = "Apache 2.0"
class TestFormatter(unittest.TestCase): class TestFormatter(unittest.TestCase):
def _check_formatting(self, original_text, formatted_text): def _check_formatting(self, original_text: str, formatted_text: str):
self.assertMultiLineEqual(formatted_text, format_config_contents(original_text)) self.assertMultiLineEqual(formatted_text, format_config_contents(original_text))
def _check_variable_tags_symmetry(self, text):
self.assertMultiLineEqual(text, strip_variable_template_tags(apply_variable_template_tags(text)))
def test_join_opening_parenthesis(self): def test_join_opening_parenthesis(self):
self.assertEqual(["foo", "bar {", "johan {", "tee", "ka", "}"], self.assertEqual(["foo", "bar {", "johan {", "tee", "ka", "}"],
join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}"))) join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}")))
@ -75,6 +78,12 @@ class TestFormatter(unittest.TestCase):
self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi', self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi',
strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi')) strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi'))
def test_variable_template_tags(self):
self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",
apply_variable_template_tags("foo bar ${myvar}"))
self._check_variable_tags_symmetry("lorem ipsum ${dolor} $amet")
self._check_variable_tags_symmetry("lorem ipsum ${dolor} $amet\nother $var and ${var_name2}")
def test_umlaut_in_string(self): def test_umlaut_in_string(self):
self._check_formatting( self._check_formatting(
"# Statusseite für Monitoring freigeben \n" + "# Statusseite für Monitoring freigeben \n" +
@ -125,6 +134,33 @@ class TestFormatter(unittest.TestCase):
" }\n" + " }\n" +
"}\n") "}\n")
def test_template_variables_with_dollars(self):
self._check_formatting('server {\n' +
' # commented ${line} should not be touched\n' +
'listen 80 default_server;\n' +
'server_name localhost;\n' +
'location / {\n' +
'proxy_set_header X-User-Auth "In ${cookie_access_token} ${ other}";\n' +
'proxy_set_header X-User-Other "foo ${bar}";\n' +
'}\n' +
'}',
'server {\n' +
' # commented ${line} should not be touched\n' +
' listen 80 default_server;\n' +
' server_name localhost;\n' +
' location / {\n' +
' proxy_set_header X-User-Auth "In ${cookie_access_token} ${other}";\n' +
' proxy_set_header X-User-Other "foo ${bar}";\n' +
' }\n' +
'}\n')
self._check_formatting(' some_tag { with_templates "my ${var} and other ${ variable_name } "; }\n' +
'# in my line\n',
'some_tag {\n' +
' with_templates "my ${var} and other ${variable_name} ";\n' +
'}\n' +
'# in my line\n')
def test_loading_utf8_file(self): def test_loading_utf8_file(self):
tmp_file = tempfile.mkstemp('utf-8')[1] tmp_file = tempfile.mkstemp('utf-8')[1]
shutil.copy('test-files/umlaut-utf8.conf', tmp_file) shutil.copy('test-files/umlaut-utf8.conf', tmp_file)

Loading…
Cancel
Save