Last active
February 13, 2016 09:35
-
-
Save megies/a8c0dc1ba7e2b6e5ebaf 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 inspect | |
import sys | |
from importlib import import_module | |
import numpy as np | |
#62 @deprecated("'get_waveform' has been renamed to 'get_waveforms'. Use " | |
#63 "that instead.") | |
#64 def get_waveform(self, *args, **kwargs): | |
#65 return self.get_waveforms(*args, **kwargs) | |
def get_deprecation_lines(indent, old_name, new_name): | |
short_indent = " " * indent | |
long_indent = " " * (indent + 4) | |
lines = [ | |
"{}@deprecated(\n".format(short_indent), | |
"{}\"'{}' has been renamed to \" # noqa\n".format( | |
long_indent, old_name), | |
"{}\"'{}'. Use that instead.\")\n".format(long_indent, new_name), | |
"{}def {}(self, *args, **kwargs):\n".format(short_indent, old_name), | |
"{}return self.{}(*args, **kwargs)\n".format(long_indent, new_name), | |
"\n", | |
] | |
return lines | |
# module_name = "obspy.core.event" | |
# obj_name = "WaveformStreamID" | |
# old_name = "getSEEDString" | |
# new_name = "get_seed_string" | |
module_name, obj_name, old_name, new_name = sys.argv[1].split(";") | |
# renames = np.loadtxt("method_list2.txt", delimiter=";", dtype="S") | |
# for module_name, obj_name, old_name, new_name in renames: | |
mod = import_module(module_name) | |
obj = getattr(mod, obj_name) | |
meth = getattr(obj, new_name) | |
source_file = inspect.getabsfile(obj) | |
source, lineno = inspect.findsource(meth) | |
first_line = source[lineno] | |
indent = len(first_line) - len(first_line.lstrip()) | |
deprecation_lines = get_deprecation_lines(indent, old_name, new_name) | |
new_source = source[:lineno] + deprecation_lines + source[lineno:] | |
# print("".join(deprecation_lines)) | |
with open(source_file, "wb") as fh: | |
fh.writelines(new_source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment