Last active
December 29, 2015 01:49
-
-
Save devxoul/7595692 to your computer and use it in GitHub Desktop.
Replace non-blueprint `url_for` to specific blueprint. e.g. url_for('login') -> url_for('blueprint.login')
This file contains 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
import os | |
import re | |
BLUEPRINT = 'blueprint' | |
TARGET_DIR = 'myproject/templates' | |
RECURSIVE = True | |
EXTENSIONS = ['py', 'html', 'htm'] | |
pat = "url_for\('(?![a-z]+\.)(?!static)([a-z_]+)" | |
rep = "url_for('%s.\g<1>" % BLUEPRINT | |
def replace_all(dirpath): | |
for filename in os.listdir(dirpath): | |
path = os.path.join(dirpath, filename) | |
if not os.path.isdir(path): | |
if filename.split('.')[-1] in EXTENSIONS: | |
f = open(path, 'r+') | |
rv = re.sub(pat, rep, f.read()) | |
f.seek(0) | |
f.write(rv) | |
f.close() | |
elif RECURSIVE: | |
if filename[0] != '.': | |
replace_all(path) | |
if __name__ == '__main__': | |
replace_all(TARGET_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment