Created
December 28, 2014 05:02
-
-
Save paulproteus/35de578ad696bab7ab01 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
def process_filename(filename): | |
lines = open(filename).readlines() | |
return process_string(lines) | |
def process_string(lines): | |
# Find first line, rofl | |
output_index = None | |
for i, line in enumerate(lines): | |
if output_index is not None: | |
return output_index | |
if line.startswith("#"): | |
pass | |
elif line.startswith("'''"): | |
output_index = i | |
elif line.startswith('"""'): | |
output_index = i | |
elif line.startswith('import '): | |
output_index = i | |
elif line.startswith('from '): | |
output_index = i | |
def fix_filename(filename): | |
# OK let's rock it. | |
lines = open(filename).readlines() | |
# Number | |
number = process_filename(filename) | |
lines[number:number+1] = ['from __future__ import absolute_import\n\n', lines[number]] | |
# Write it | |
s = ''.join(lines) | |
open(filename, 'w').write(s) | |
def main(): | |
import sys | |
fix_filename(sys.argv[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment