Skip to content

Instantly share code, notes, and snippets.

@devxoul
Last active December 29, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/7595692 to your computer and use it in GitHub Desktop.
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')
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