Created
May 18, 2017 09:47
-
-
Save davidszotten/4c103815e2dfa6b5f0bdabf2969f3e19 to your computer and use it in GitHub Desktop.
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 re | |
import sys | |
def finder(sourcelines): | |
result = [] | |
inside = False | |
for line in sourcelines: | |
match = re.match(r"""(\w+) = patterns\(""", line) | |
if match: | |
inside = True | |
name = match.groups()[0] | |
result.append('{} = ['.format(name)) | |
elif inside and re.match(r'\s*\)', line): | |
result.append(']') | |
inside = False | |
elif inside and re.match(r"""\s*(''|""),""", line): | |
pass | |
elif line == 'from django.conf.urls import patterns, url': | |
result.append('from django.conf.urls import url') | |
else: | |
result.append(line) | |
if inside: | |
result.append('qqDS') # find and fix manually | |
return result | |
def main(): | |
filename = sys.argv[1] | |
with open(filename) as handle: | |
source = handle.read() | |
sourcelines = source.splitlines() | |
with open(filename, 'w') as handle: | |
handle.write('\n'.join(finder(sourcelines)) + '\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment