Skip to content

Instantly share code, notes, and snippets.

@megies
Created February 12, 2016 13:55
Show Gist options
  • Select an option

  • Save megies/24582c5996ee8ccd9be1 to your computer and use it in GitHub Desktop.

Select an option

Save megies/24582c5996ee8ccd9be1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import inspect
from inspect import ismodule, isclass, ismethod
import os
import re
from importlib import import_module
from pkgutil import walk_packages
import obspy
from future.builtins import __all__ as future_builtins_names
def recursive_list(name, module, obspy_imports, root=False):
if name in future_builtins_names:
return
# if we end up in obspy top-level from inside obspy, just ignore
if not root and module == obspy:
return
try:
obj = getattr(module, name)
except:
# getting tests from modules somehow not works (without importing the
# module again first, but we're not concerned about this particular
# import..)
if name == "tests" or module.__name__ == "obspy.lib":
return
# ignore precompiled extensions
if module.__name__ == "obspy.lib":
return
obj = import_module("." + name, module.__name__)
# check if the source file is in obspy package tree
try:
sourcefile = inspect.getsourcefile(obj) or inspect.getsourcefile(module)
except:
return
if not sourcefile:
return
if not re.match(r'/home/megies/anaconda/envs/stable/lib/python2.7/site-packages/obspy-0.10.2-py2.7-linux-x86_64.egg', sourcefile):
return
# recurse
is_module = ismodule(obj)
if is_module:
names = dir(obj)
if "__path__" in names:
names += [pkg[1].split(".")[0]
for pkg in walk_packages(obj.__path__)]
for name_ in sorted(set(names)):
if "." in name_:
from IPython.core.debugger import Tracer; Tracer(colors="Linux")()
recursive_list(name_, obj, obspy_imports)
# print info on object
if isclass(obj):
methods = inspect.getmembers(obj, predicate=ismethod)
method_list = []
for method_name, method in methods:
# skip private/special methods
if method_name.startswith("_"):
continue
method_list.append(method_name)
if method_list:
obspy_imports.add((module.__name__, name, ",".join(method_list)))
if __name__ == "__main__":
num = len(dir(obspy))
names = (dir(obspy) +
[pkg[1].split(".")[0]
for pkg in walk_packages(obspy.__path__)])
obspy_imports = set()
for i_, name in enumerate(sorted(set(names))):
# somehow we end up in the obspy root again, so explicitely mark this
# as only allowed top-level
# n.b. this is coming from obspy.station.stationxml.py
# which has an 'import obspy' line..
recursive_list(name, obspy, obspy_imports, root=True)
outfile = os.path.join(os.path.dirname(__file__), "method_list.txt_new")
with open(outfile, "wb") as fh:
for obspy_import in sorted(obspy_imports):
fh.write("{};{};{}\n".format(*obspy_import))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment