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