Add function description and some readme.

pull/12/head
Michał Słomkowski 9 years ago
parent d2b62206fd
commit 73f01a74ec

@ -3,3 +3,16 @@
# nginx config file formatter # nginx config file formatter
Formats *nginx* configuration files. 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.

@ -14,6 +14,7 @@ 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)."""
within_quotes = False within_quotes = False
parts = [] parts = []
for part in re.split('"', single_line.strip()): for part in re.split('"', single_line.strip()):
@ -26,6 +27,7 @@ def strip_line(single_line):
def clean_lines(orig_lines): def clean_lines(orig_lines):
"""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)
@ -38,7 +40,8 @@ def clean_lines(orig_lines):
return cleaned_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 = [] modified_lines = []
for i in range(len(lines)): for i in range(len(lines)):
if i > 0 and lines[i] == "{": if i > 0 and lines[i] == "{":
@ -49,6 +52,7 @@ def join_opening_parenthesis(lines):
def perform_indentation(lines): def perform_indentation(lines):
"""Indents the lines according to their nesting level determined by curly brackets."""
indented_lines = [] indented_lines = []
current_indent = 0 current_indent = 0
for line in lines: for line in lines:
@ -64,8 +68,9 @@ def perform_indentation(lines):
def format_config_file(contents): 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 = clean_lines(contents.splitlines())
lines = join_opening_parenthesis(lines) lines = join_opening_bracket(lines)
lines = perform_indentation(lines) lines = perform_indentation(lines)
return '\n'.join(lines) + '\n' return '\n'.join(lines) + '\n'

@ -1,7 +1,12 @@
"""Unit tests for nginxfmt module."""
import unittest import unittest
from nginxfmt import * from nginxfmt import *
__author__ = "Michał Słomkowski"
__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, formatted_text):
@ -9,7 +14,7 @@ class TestFormatter(unittest.TestCase):
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_parenthesis(("foo", "bar {", "johan", "{", "tee", "ka", "}"))) join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}")))
def test_clear_lines(self): def test_clear_lines(self):
self.assertEqual(["ala", "ma", "{", "kota", "}", "to;", "", "ook"], self.assertEqual(["ala", "ma", "{", "kota", "}", "to;", "", "ook"],

Loading…
Cancel
Save