Last active
August 29, 2015 14:11
-
-
Save cwurld/4f58ff694f3bbca3842e to your computer and use it in GitHub Desktop.
Converts a Django page template into a page template for a Django project template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
__author__ = 'Chuck Martin' | |
import os | |
import Tkinter | |
import tkFileDialog | |
def convert_template(file_path=None, out_dir=None): | |
""" | |
Converts a django page template into a page template for a django project template by escaping codes | |
with templatetag. For more info: | |
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag | |
file_path: absolute path to django page template. If you do not provide one, a GUI will pop up and ask you | |
for one. | |
out_dir: absolute path to the dir to write the output to. If not set, a GUI pop up will ask you for one. | |
If output dir is the same as the input dir, then the output file name will be the input file | |
name with "_converted" added to it. | |
""" | |
root = Tkinter.Tk() | |
root.withdraw() | |
if file_path is None: | |
file_path = tkFileDialog.askopenfilename(title='File to convert') | |
if out_dir is None: | |
out_dir = tkFileDialog.askdirectory(title='Output dir') | |
in_dir, filename = os.path.split(file_path) | |
if in_dir == out_dir: | |
filename = filename.replace('.', '_converted.') | |
infile = open(file_path) | |
outfile = open(os.path.join(out_dir, filename), 'w') | |
replacements = [['{%', '{% templatetag openblock '], | |
['%}', '{% templatetag closeblock %}'], | |
[' openblock ', ' openblock %}'], | |
['{{', '{% templatetag openvariable %}'], | |
['}}', '{% templatetag closevariable %}']] | |
for line in infile: | |
for old, new in replacements: | |
line = line.replace(old, new) | |
outfile.write(line) | |
infile.close() | |
outfile.close() | |
#convert_template(file_path='/home/chuck/sqdb/roi_template_meta/convert_template_test.html') | |
files = ['/home/chuck/sqdb/thanks/thanks/templates/account/logout.html', | |
'/home/chuck/sqdb/thanks/thanks/templates/account/password_reset.html', | |
'/home/chuck/sqdb/thanks/thanks/templates/account/password_reset_from_key.html', | |
'/home/chuck/sqdb/thanks/thanks/templates/account/password_reset_from_key_done.html'] | |
for f in files: | |
convert_template(file_path=f, out_dir='/home/chuck/sqdb/roi_template/project_name/templates/account') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment