"""
Utilities
"""

"""
Takes a python dictionary and converts it to nginx compatible configuration block
"""


def to_nginx_template(config):
    template = ''
    for key, value in config.iteritems():
        if isinstance(value, dict):
            for key2, value2 in value.items():
                template += "{} {} {};\n".format(key, key2, value)
        else:
            template += "{} {};\n".format(key, value)
    return template


"""
nginx configuration indentation
"""


def make_indent(contents):
    indents = '    '
    lines = map(str.strip, contents.splitlines())
    current_indent = 0
    for index, line in enumerate(lines):
        if line.endswith('}'):
            current_indent -= 1
        lines[index] = current_indent * indents + line

        if line.endswith('{'):
            current_indent += 1

    return '\n'.join(lines)

@@ -11,16 +11,285 @@
 ies%0A%22%22%22%0A
+from string import Template%0A%0A%22%22%22%0AGenerate configuration blocks%0A%22%22%22%0A%0A%0Adef make_block(name, content, pattern=%22%22):%0A    return Template(%22%22%22%0A        $%7Bname%7D $%7Bpattern%7D %7B%0A            $%7Bcontent%7D%0A        %7D%0A    %22%22%22).safe_substitute(name=name, content=content, pattern=pattern)%0A%0A
 %0A%22%22%22%0ATak
@@ -423,10 +423,10 @@
 e = 
-''
+%22%22
 %0A   
@@ -538,16 +538,20 @@
 n value.
+iter
 items():
@@ -612,24 +612,25 @@
  key2, value
+2
 )%0A        el

refactor and added make_block to create dynamic config blocks