Last active
April 23, 2017 10:14
-
-
Save wezu/3634e8309053ff586ac88b1aaf232c44 to your computer and use it in GitHub Desktop.
Script to find all camelCaseFunctions and add a table for calling them as snake_case_functions
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, sys | |
def find_camel_functions(sourcefile): | |
return_dict={'__file__':set()} | |
current_set=return_dict['__file__'] | |
with open(sourcefile, 'r') as f: | |
for line in f: | |
if line.strip().startswith('class '): | |
class_name=line.strip().split(' ')[1] | |
if '(' in class_name: | |
class_name=class_name.split('(')[0] | |
return_dict[class_name]=set() | |
current_set=return_dict[class_name] | |
if line.strip().startswith('def '): | |
function_name=line.strip().split(' ')[1] | |
if '(' in function_name: | |
function_name=function_name.split('(')[0] | |
if function_name.lower() != function_name: | |
current_set.add(function_name) | |
return return_dict | |
def camel_to_snake(text): | |
snake_case='' | |
was_undersore=False | |
for i, char in enumerate(text): | |
if (char.isupper() or char in '0123456789') and i != 0: | |
if not was_undersore: | |
snake_case+="_" | |
was_undersore=True | |
elif char == '_': | |
was_undersore=True | |
else: | |
was_undersore=False | |
snake_case+=char.lower() | |
return snake_case | |
def find_last_class_line(sourcefile): | |
return_dict={'__file__':set()} | |
current_set=return_dict['__file__'] | |
with open(sourcefile, 'r') as f: | |
for line_nr, line in enumerate(f): | |
if line.strip().startswith('class '): | |
current_set.add(line_nr) | |
class_name=line.strip().split(' ')[1] | |
if '(' in class_name: | |
class_name=class_name.split('(')[0] | |
return_dict[class_name]=set() | |
current_set=return_dict[class_name] | |
current_set.add(line_nr+1) | |
return return_dict | |
def find_class_indentation(sourcefile): | |
return_dict={'__file__':set([0])} | |
with open(sourcefile, 'r') as f: | |
for line in f: | |
if line.strip().startswith('class '): | |
class_name=line.strip().split(' ')[1] | |
if '(' in class_name: | |
class_name=class_name.split('(')[0] | |
return_dict[class_name]=set() | |
current_set=return_dict[class_name] | |
indent=4+len(line) - len(line.lstrip()) | |
current_set.add(indent) | |
return return_dict | |
def make_snake_case_table(sourcefile): | |
functions_dict=find_camel_functions(sourcefile) | |
init_lines=find_last_class_line(sourcefile) | |
init_indent=find_class_indentation(sourcefile) | |
print (init_lines) | |
print (init_indent) | |
table={} | |
for class_name, functions in functions_dict.items(): | |
if init_lines[class_name]: | |
line_nr=max(init_lines[class_name]) | |
indent='' | |
text='' | |
for _ in range(max(init_indent[class_name])): | |
indent+=' ' | |
for f in functions: | |
if not f.startswith('_'): | |
text+=indent+camel_to_snake(f)+' = '+f+'\n' | |
if text != '': | |
table[line_nr]='\n'+indent+'#snake_case alias:\n'+text+'\n' | |
return table | |
def write_snake_case_table(sourcefile, targetfile): | |
assert sourcefile != targetfile | |
table=make_snake_case_table(sourcefile) | |
print("Adding to file",targetfile) | |
for line, text in table.items(): | |
print("line nr:",line,":") | |
print(text) | |
with open(targetfile, 'w', encoding='utf-8') as outfile, open(sourcefile, 'r') as infile: | |
for line_nr, line in enumerate(infile): | |
if line_nr in table: | |
outfile.write(table[line_nr]) | |
outfile.write(line) | |
if line_nr+1 in table: | |
outfile.write(table[line_nr+1]) | |
if __name__ == "__main__": | |
'''in_file=sys.argv[1] | |
out_file=sys.argv[2] | |
write_snake_case_table(in_file, out_file)''' | |
files=[] | |
for fname in os.listdir('.'): | |
if fname[-2:]=="py" and fname != 'snake_skin.py': | |
files.append((fname, fname.replace('.', '2.'))) | |
for in_file, out_file in files: | |
write_snake_case_table(in_file, out_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment