Remove spurious __pycache__ dir. Format code.

master
Michał Słomkowski 5 years ago
parent 47e7e317b2
commit c7726b7072

1
.gitignore vendored

@ -2,4 +2,5 @@
.idea
*.iml
__pycache__

@ -23,6 +23,7 @@ TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
TEMPLATE_BRACKET_OPENING_TAG = '___TEMPLATE_BRACKET_OPENING_TAG___'
TEMPLATE_BRACKET_CLOSING_TAG = '___TEMPLATE_BRACKET_CLOSING_TAG___'
def strip_line(single_line):
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
single_line = single_line.strip()
@ -39,6 +40,7 @@ def strip_line(single_line):
within_quotes = not within_quotes
return '"'.join(parts)
def count_multi_semicolon(single_line):
"""count multi_semicolon (except when within quotation marks)."""
single_line = single_line.strip()
@ -56,6 +58,7 @@ def count_multi_semicolon(single_line):
within_quotes = not within_quotes
return q, c
def multi_semicolon(single_line):
"""break multi_semicolon into multiline (except when within quotation marks)."""
single_line = single_line.strip()
@ -72,6 +75,7 @@ def multi_semicolon(single_line):
within_quotes = not within_quotes
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*}',
@ -87,6 +91,7 @@ def strip_variable_template_tags(line: str) -> str:
line,
flags=re.UNICODE)
def apply_bracket_template_tags(content: str) -> str:
""" Replaces bracket { and } with tags, so subsequent formatting is easier."""
result = ""
@ -108,17 +113,20 @@ def apply_bracket_template_tags(content: str) -> str:
last_c = c
return result
def reverse_in_quotes_status(status: bool) -> bool:
if status:
return False
return True
def strip_bracket_template_tags(content: str) -> str:
""" Replaces tags back with { and } respectively."""
content = content.replace(TEMPLATE_BRACKET_OPENING_TAG, "{", -1)
content = content.replace(TEMPLATE_BRACKET_CLOSING_TAG, "}", -1)
return content
def clean_lines(orig_lines) -> list:
"""Strips the lines and splits them if they contain curly brackets."""
cleaned_lines = []
@ -132,7 +140,7 @@ def clean_lines(orig_lines) -> list:
if line.startswith("#"):
cleaned_lines.append(strip_variable_template_tags(line))
else:
q , c = count_multi_semicolon(line)
q, c = count_multi_semicolon(line)
if q == 1 and c > 1:
ml = multi_semicolon(line)
cleaned_lines.extend(clean_lines(ml.splitlines()))

@ -38,13 +38,14 @@ class TestFormatter(unittest.TestCase):
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
self.assertEqual(["{", "ala", "# ma {{", "rewrite /([\d]{2}) /up/$1.html last;", "}", "to", "}"],
clean_lines(("{", "ala ", "# ma {{", " rewrite /([\d]{2}) /up/$1.html last; ", "}", " to", "}")))
clean_lines(
("{", "ala ", "# ma {{", " rewrite /([\d]{2}) /up/$1.html last; ", "}", " to", "}")))
self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"],
clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
self.assertEqual(["{", "aa;", "b b \"cc; dd; ee \";", "ssss;", "}"],
clean_lines(("{", "aa; b b \"cc; dd; ee \"; ssss;", "}")))
clean_lines(("{", "aa; b b \"cc; dd; ee \"; ssss;", "}")))
self.assertEqual(["location ~ /\.ht", "{"], clean_lines(["location ~ /\.ht {", ]))
@ -90,12 +91,17 @@ class TestFormatter(unittest.TestCase):
strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi'))
def test_apply_bracket_template_tags(self):
self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"", apply_bracket_template_tags("\"aaa{dd}bbb\""))
self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"", apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\""))
self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"",
apply_bracket_template_tags("\"aaa{dd}bbb\""))
self.assertEqual(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"",
apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\""))
def test_strip_bracket_template_tags(self):
self.assertEqual("\"aaa{dd}bbb\"", strip_bracket_template_tags("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\""))
self.assertEqual("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"", apply_bracket_template_tags("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\""))
self.assertEqual("\"aaa{dd}bbb\"", strip_bracket_template_tags(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\""))
self.assertEqual("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"", apply_bracket_template_tags(
"\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\""))
def test_variable_template_tags(self):
self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",

Loading…
Cancel
Save