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 .idea
*.iml *.iml
__pycache__

@ -23,6 +23,7 @@ TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
TEMPLATE_BRACKET_OPENING_TAG = '___TEMPLATE_BRACKET_OPENING_TAG___' TEMPLATE_BRACKET_OPENING_TAG = '___TEMPLATE_BRACKET_OPENING_TAG___'
TEMPLATE_BRACKET_CLOSING_TAG = '___TEMPLATE_BRACKET_CLOSING_TAG___' TEMPLATE_BRACKET_CLOSING_TAG = '___TEMPLATE_BRACKET_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)."""
single_line = single_line.strip() single_line = single_line.strip()
@ -39,6 +40,7 @@ def strip_line(single_line):
within_quotes = not within_quotes within_quotes = not within_quotes
return '"'.join(parts) return '"'.join(parts)
def count_multi_semicolon(single_line): def count_multi_semicolon(single_line):
"""count multi_semicolon (except when within quotation marks).""" """count multi_semicolon (except when within quotation marks)."""
single_line = single_line.strip() single_line = single_line.strip()
@ -56,6 +58,7 @@ def count_multi_semicolon(single_line):
within_quotes = not within_quotes within_quotes = not within_quotes
return q, c return q, c
def multi_semicolon(single_line): def multi_semicolon(single_line):
"""break multi_semicolon into multiline (except when within quotation marks).""" """break multi_semicolon into multiline (except when within quotation marks)."""
single_line = single_line.strip() single_line = single_line.strip()
@ -72,6 +75,7 @@ def multi_semicolon(single_line):
within_quotes = not within_quotes within_quotes = not within_quotes
return '"'.join(parts) return '"'.join(parts)
def apply_variable_template_tags(line: str) -> str: def apply_variable_template_tags(line: str) -> str:
"""Replaces variable indicators ${ and } with tags, so subsequent formatting is easier.""" """Replaces variable indicators ${ and } with tags, so subsequent formatting is easier."""
return re.sub(r'\${\s*(\w+)\s*}', return re.sub(r'\${\s*(\w+)\s*}',
@ -87,6 +91,7 @@ def strip_variable_template_tags(line: str) -> str:
line, line,
flags=re.UNICODE) flags=re.UNICODE)
def apply_bracket_template_tags(content: str) -> str: def apply_bracket_template_tags(content: str) -> str:
""" Replaces bracket { and } with tags, so subsequent formatting is easier.""" """ Replaces bracket { and } with tags, so subsequent formatting is easier."""
result = "" result = ""
@ -108,17 +113,20 @@ def apply_bracket_template_tags(content: str) -> str:
last_c = c last_c = c
return result return result
def reverse_in_quotes_status(status: bool) -> bool: def reverse_in_quotes_status(status: bool) -> bool:
if status: if status:
return False return False
return True return True
def strip_bracket_template_tags(content: str) -> str: def strip_bracket_template_tags(content: str) -> str:
""" Replaces tags back with { and } respectively.""" """ Replaces tags back with { and } respectively."""
content = content.replace(TEMPLATE_BRACKET_OPENING_TAG, "{", -1) content = content.replace(TEMPLATE_BRACKET_OPENING_TAG, "{", -1)
content = content.replace(TEMPLATE_BRACKET_CLOSING_TAG, "}", -1) content = content.replace(TEMPLATE_BRACKET_CLOSING_TAG, "}", -1)
return content return content
def clean_lines(orig_lines) -> list: def clean_lines(orig_lines) -> list:
"""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 = []

@ -38,7 +38,8 @@ class TestFormatter(unittest.TestCase):
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }"))) clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
self.assertEqual(["{", "ala", "# ma {{", "rewrite /([\d]{2}) /up/$1.html last;", "}", "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;", "}"], self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"],
clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}"))) clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
@ -90,12 +91,17 @@ class TestFormatter(unittest.TestCase):
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_apply_bracket_template_tags(self): 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\"",
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\"")) 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): 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\"", strip_bracket_template_tags(
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\"")) "\"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): def test_variable_template_tags(self):
self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___", self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",

Loading…
Cancel
Save