Refactors two similar django template files to use {% block %}
tags.
Takes two template files, where one is a modified version of the other, and looks for differences and similiarities between the two.
It then generates refactored (and renamed) versions of each file and a third 'base' file, adding an {% extends %}
tag as appropriate.
Block tags are named with matching numbers in all 3 files, making it easy to do a search and replace with more meaningful labels.
sample_data_in_base
controls whether or not the content from file 1 is copied into place in the base file as an example.
Problems: it doesn't identify open {% for %}
or {% if %}
tags, so this needs some manual fixing (moving the {% endfor %}
and {% endif %}
tags into the proper blocks). It doesn't add the correct path for the {% extends %}
tag either (ie. "app/base.html"
). collapse_whitespace
is not working at all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import difflib
import os.path
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
sample_data_in_base = False
collapse_whitespace = True
base_file_name = "base.html"
def ignore_ws(c):
return c in " \t\n\r"
filename1 = tkFileDialog.askopenfilename(title= "First file?")
f1 = file(filename1,'r').readlines()
filename2 = tkFileDialog.askopenfilename(title= "Second file?")
f2 = file(filename2,'r').readlines()
d = difflib.ndiff(f1,f2, charjunk=ignore_ws)
base = ""
r1 = r2 = '{% extends "'+base_file_name+'" %}'
block_open = False
block = 0
for line in d:
# equal line, goes into base, not r1 or r2
if line[:2] == " ":
#block tag open? close it
if block_open:
tag = "{% endblock " + str(block) + " %}\n"
base += tag
r1 += tag
r2 += tag
block += 1
block_open = False
base += line[2:]
continue
# else: different line
# block tag closed? open it
if not block_open:
tag = "{% block " + str(block) + " %}\n"
base += tag
r1 += tag
r2 += tag
block_open = True
# line from f1? put into r1 (and maybe base)
if line[:2] == "- ":
r1 += line[2:]
if sample_data_in_base:
base += line[2:]
# line from f2? put into r2
if line[:2] == "+ ":
r2 += line[2:]
for name,text in [(base_file_name,base),(filename1,r1),(filename2,r2)]:
root,ext = os.path.splitext(name)
if text!=base:
name = root+"_refactored"+ext
f=file(name,'w')
f.writelines(text)
f.close()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
WARNING
I already had a base.html and it overwrote it without a prompt. Now that will teach me about reading the instructions and remembering to use version control!
#
Woops. Sorry!
#
Please login first before commenting.