Change strip_line to prevent collapsing spaces in comments.

pull/12/head
Michał Słomkowski 8 years ago
parent 82be152c34
commit ec596dd7e8

@ -15,9 +15,13 @@ INDENTATION = ' ' * 4
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()
if single_line.startswith('#'):
return single_line
within_quotes = False within_quotes = False
parts = [] parts = []
for part in re.split('"', single_line.strip()): for part in re.split('"', single_line):
if within_quotes: if within_quotes:
parts.append(part) parts.append(part)
else: else:
@ -88,13 +92,13 @@ def format_config_file(contents):
if __name__ == "__main__": if __name__ == "__main__":
argparser = argparse.ArgumentParser(description=__doc__) arg_parser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument("-v", "--verbose", action="store_true", help="show formatted file names") arg_parser.add_argument("-v", "--verbose", action="store_true", help="show formatted file names")
argparser.add_argument("-b", "--backup-original", action="store_true", help="backup original config file") arg_parser.add_argument("-b", "--backup-original", action="store_true", help="backup original config file")
argparser.add_argument("config_file", type=argparse.FileType('r'), nargs='+') arg_parser.add_argument("config_file", type=argparse.FileType('r'), nargs='+')
args = argparser.parse_args() args = arg_parser.parse_args()
for config_file in args.config_file: for config_file in args.config_file:
original_file_content = config_file.read() original_file_content = config_file.read()

@ -26,7 +26,7 @@ class TestFormatter(unittest.TestCase):
self.assertEqual(["{", "ala", "ma", "{", "{", "kota", "}", "to", "}"], self.assertEqual(["{", "ala", "ma", "{", "{", "kota", "}", "to", "}"],
clean_lines(("{", "ala ", "ma {{", " kota ", "}", " to} "))) clean_lines(("{", "ala ", "ma {{", " kota ", "}", " to} ")))
self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"], self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"],
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }"))) clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
def test_perform_indentation(self): def test_perform_indentation(self):

Loading…
Cancel
Save