From 73f01a74ec370ba78db50c065938f1ced9dcbd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20S=C5=82omkowski?= Date: Thu, 16 Jun 2016 01:53:26 +0200 Subject: [PATCH] Add function description and some readme. --- README.md | 15 ++++++++++++++- nginxfmt.py | 9 +++++++-- test_nginxfmt.py | 7 ++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bbeebcb..6ef6f4c 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,17 @@ # nginx config file formatter -Formats *nginx* configuration files. \ No newline at end of file +Formats *nginx* configuration files. + +## Installation + +Requirements: Python 3+. + +## Usage + +* todo command line args +* you can pass several files in command line + +## Credits + +Copyright 2016 Michał Słomkowski @ 1CONNECT. License: Apache 2.0. diff --git a/nginxfmt.py b/nginxfmt.py index 71721f7..66ebf69 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -14,6 +14,7 @@ INDENTATION = ' ' * 4 def strip_line(single_line): + """Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks).""" within_quotes = False parts = [] for part in re.split('"', single_line.strip()): @@ -26,6 +27,7 @@ def strip_line(single_line): def clean_lines(orig_lines): + """Strips the lines and splits them if they contain curly brackets.""" cleaned_lines = [] for line in orig_lines: line = strip_line(line) @@ -38,7 +40,8 @@ def clean_lines(orig_lines): return cleaned_lines -def join_opening_parenthesis(lines): +def join_opening_bracket(lines): + """When opening curly bracket is in it's own line (K&R convention), it's joined with precluding line (Java).""" modified_lines = [] for i in range(len(lines)): if i > 0 and lines[i] == "{": @@ -49,6 +52,7 @@ def join_opening_parenthesis(lines): def perform_indentation(lines): + """Indents the lines according to their nesting level determined by curly brackets.""" indented_lines = [] current_indent = 0 for line in lines: @@ -64,8 +68,9 @@ def perform_indentation(lines): def format_config_file(contents): + """Accepts the string containing nginx configuration and returns formatted one. Adds newline at the end.""" lines = clean_lines(contents.splitlines()) - lines = join_opening_parenthesis(lines) + lines = join_opening_bracket(lines) lines = perform_indentation(lines) return '\n'.join(lines) + '\n' diff --git a/test_nginxfmt.py b/test_nginxfmt.py index 8e5771c..b7f3e1a 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -1,7 +1,12 @@ +"""Unit tests for nginxfmt module.""" + import unittest from nginxfmt import * +__author__ = "Michał Słomkowski" +__license__ = "Apache 2.0" + class TestFormatter(unittest.TestCase): def _check_formatting(self, original_text, formatted_text): @@ -9,7 +14,7 @@ class TestFormatter(unittest.TestCase): def test_join_opening_parenthesis(self): self.assertEqual(["foo", "bar {", "johan {", "tee", "ka", "}"], - join_opening_parenthesis(("foo", "bar {", "johan", "{", "tee", "ka", "}"))) + join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}"))) def test_clear_lines(self): self.assertEqual(["ala", "ma", "{", "kota", "}", "to;", "", "ook"],