Commits

Anonymous committed 896ee9a

Python code should be striped()

Comments (0)

Files changed (2)

 quotes = ("'", '"')
 string_on_html = 'response.write("%s", escape=False)'
 indentation = '    '
+unindentation_words_before = ('else', 'elif', 'except')
+unindentation_words_after = ('pass', 'break', 'continue', 'return')
 
+    
 def parse_string(text):
     return text.replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
 
     else:
         return line
 
+
 def should_unindent_before(parsed_line):
-    unindentation_words = ('else', 'elif', 'except')
     if parsed_line.strip():
-        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words
+        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_before
     else:
         return ''
 
 
 def should_unindent_after(parsed_line):
-    unindentation_words = ('pass', 'break', 'continue', 'return')
     if parsed_line.strip():
-        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words
+        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_after
     else:
         return ''
 
             in_string = not in_string
             if not in_string:
                 last_quote = ''
-        if character == '\n' and not in_string and last_character != '\\' and opened == 0:
+        if character == '\n' and not in_string and last_character != '\\' and not opened:
             parsed_line = parse_line(''.join(line[:-1])).strip()
             if parsed_line:
                 if should_unindent_before(parsed_line):
         last_character = character
 
     if line:
-        parsed_line = parse_line(''.join(line))
+        parsed_line = parse_line(''.join(line)).strip()
         if parsed_line:
             if should_unindent_before(parsed_line):
                 indentation_level -= 1
-            if parsed_line.strip() != 'pass' and parsed_line.strip():
+            if parsed_line and parsed_line != 'pass':
                 response.append(indentation_level * indentation + parsed_line)
             if should_unindent_after(parsed_line):
                 indentation_level -= 1
 
         line.append(character)
 
-        if in_python_code and character in quotes and not backslash and (not last_quote or character == last_quote):
+        if in_python_code and character in quotes and not backslash \
+           and (not last_quote or character == last_quote):
             last_quote = character
             in_string = not in_string
             if not in_string:
                 last_quote = ''
         elif character == '{':
-            if in_python_code and open_tag_count == 0 and not in_string:
+            if in_python_code and not open_tag_count and not in_string:
                 dict_count += 1
                 last_character = character
                 continue
                 in_python_code = True
                 line = line[:-2]
                 if line:
-                    response.append(indentation_level * indentation + string_on_html % parse_string(''.join(line)))
+                    response.append(indentation_level * indentation + \
+                                    string_on_html % parse_string(''.join(line)))
                     line = []
         elif character == '}':
             if in_python_code and dict_count > 0 and not in_string:
                 in_python_code = False
                 close_tag_count = 0
                 python_code = ''.join(line[:-2]).strip()
-                indentation_level, parsed_code = parse_python_code(python_code, indentation_level)
+                indentation_level, parsed_code = parse_python_code(python_code,
+                        indentation_level)
                 response.extend(parsed_code)
                 line = []
         if character == '\\':
 
     if line:
         if in_python_code:
-            indentation_level, parsed_code = parse_python_code(''.join(line), indentation_level)
+            indentation_level, parsed_code = parse_python_code(''.join(line),
+                    indentation_level)
             response.extend(parsed_code)
         else:
-            response.append(indentation_level * '    ' + string_on_html % parse_string(''.join(line)))
+            response.append(indentation_level * indentation + \
+                            string_on_html % parse_string(''.join(line)))
     return '\n'.join(response)
     
 

test_new_template.py

     def test_create_a_lot_of_dictionaries_in_python_code_with_unmatching_brackets(self):
         self.input_ = 'A{{ {{{}} }}B{{=a}}'
         self.expected_output = """response.write("A", escape=False)
- {{{}} }}B{{=a}}"""
+{{{}} }}B{{=a}}"""
         self.do_it()