1''' 2Helper module to do refactoring to convert names to pep8. 3''' 4import re 5import os 6import names_to_rename 7 8_CAMEL_RE = re.compile(r'(?<=[a-z])([A-Z])') 9_CAMEL_DEF_RE = re.compile(r'(def )((([A-Z0-9]+|[a-z0-9])[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*)') 10 11def _normalize(name): 12 return _CAMEL_RE.sub(lambda x: '_' + x.group(1).lower(), name).lower() 13 14def find_matches_in_contents(contents): 15 return [x[1] for x in re.findall(_CAMEL_DEF_RE, contents)] 16 17def iter_files_in_dir(dirname): 18 for root, dirs, files in os.walk(dirname): 19 for name in ('pydevd_attach_to_process', '.git', 'stubs', 'pydev_ipython', 'third_party', 'pydev_ipython'): 20 try: 21 dirs.remove(name) 22 except: 23 pass 24 for filename in files: 25 if filename.endswith('.py') and filename not in ('rename_pep8.py', 'names_to_rename.py'): 26 path = os.path.join(root, filename) 27 with open(path, 'rb') as stream: 28 initial_contents = stream.read() 29 30 yield path, initial_contents 31 32def find_matches(): 33 found = set() 34 for path, initial_contents in iter_files_in_dir(os.path.dirname(os.path.dirname(__file__))): 35 found.update(find_matches_in_contents(initial_contents)) 36 print '\n'.join(sorted(found)) 37 print 'Total', len(found) 38 39def substitute_contents(re_name_to_new_val, initial_contents): 40 contents = initial_contents 41 for key, val in re_name_to_new_val.iteritems(): 42 contents = re.sub(key, val, contents) 43 return contents 44 45def make_replace(): 46 re_name_to_new_val = load_re_to_new_val(names_to_rename.NAMES) 47 # traverse root directory, and list directories as dirs and files as files 48 for path, initial_contents in iter_files_in_dir(os.path.dirname(os.path.dirname(__file__))): 49 contents = substitute_contents(re_name_to_new_val, initial_contents) 50 if contents != initial_contents: 51 print 'Changed something at: %s' % (path,) 52 53 for val in re_name_to_new_val.itervalues(): 54 # Check in initial contents to see if it already existed! 55 if re.findall(r'\b%s\b' % (val,), initial_contents): 56 raise AssertionError('Error in:\n%s\n%s is already being used (and changes may conflict).' % (path, val,)) 57 58 with open(path, 'wb') as stream: 59 stream.write(contents) 60 61 62def load_re_to_new_val(names): 63 name_to_new_val = {} 64 for n in names.splitlines(): 65 n = n.strip() 66 if not n.startswith('#') and n: 67 name_to_new_val[r'\b'+n+r'\b'] = _normalize(n) 68 return name_to_new_val 69 70def test(): 71 assert _normalize('RestoreSysSetTraceFunc') == 'restore_sys_set_trace_func' 72 assert _normalize('restoreSysSetTraceFunc') == 'restore_sys_set_trace_func' 73 assert _normalize('Restore') == 'restore' 74 matches = find_matches_in_contents(''' 75 def CamelCase() 76 def camelCase() 77 def ignore() 78 def ignore_this() 79 def Camel() 80 def CamelCaseAnother() 81 ''') 82 assert matches == ['CamelCase', 'camelCase', 'Camel', 'CamelCaseAnother'] 83 re_name_to_new_val = load_re_to_new_val(''' 84# Call -- skip 85# Call1 -- skip 86# Call2 -- skip 87# Call3 -- skip 88# Call4 -- skip 89CustomFramesContainerInit 90DictContains 91DictItems 92DictIterItems 93DictIterValues 94DictKeys 95DictPop 96DictValues 97''') 98 assert re_name_to_new_val == {'\\bDictPop\\b': 'dict_pop', '\\bDictItems\\b': 'dict_items', '\\bDictIterValues\\b': 'dict_iter_values', '\\bDictKeys\\b': 'dict_keys', '\\bDictContains\\b': 'dict_contains', '\\bDictIterItems\\b': 'dict_iter_items', '\\bCustomFramesContainerInit\\b': 'custom_frames_container_init', '\\bDictValues\\b': 'dict_values'} 99 assert substitute_contents(re_name_to_new_val, ''' 100CustomFramesContainerInit 101DictContains 102DictItems 103DictIterItems 104DictIterValues 105DictKeys 106DictPop 107DictValues 108''') == ''' 109custom_frames_container_init 110dict_contains 111dict_items 112dict_iter_items 113dict_iter_values 114dict_keys 115dict_pop 116dict_values 117''' 118 119if __name__ == '__main__': 120# find_matches() 121 make_replace() 122# test() 123 124