diff --git a/doc/source/conf.py b/doc/source/conf.py index a1b71f0279c7a..fd3a2493a53e8 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -13,6 +13,7 @@ import sys import os import re +import inspect from pandas.compat import u, PY3 # If extensions (or modules to document with autodoc) are in another directory, @@ -47,6 +48,7 @@ 'sphinx.ext.coverage', 'sphinx.ext.pngmath', 'sphinx.ext.ifconfig', + 'sphinx.ext.linkcode', ] @@ -424,6 +426,55 @@ def get_items(self, names): return items +# based on numpy doc/source/conf.py +def linkcode_resolve(domain, info): + """ + Determine the URL corresponding to Python object + """ + if domain != 'py': + return None + + modname = info['module'] + fullname = info['fullname'] + + submod = sys.modules.get(modname) + if submod is None: + return None + + obj = submod + for part in fullname.split('.'): + try: + obj = getattr(obj, part) + except: + return None + + try: + fn = inspect.getsourcefile(obj) + except: + fn = None + if not fn: + return None + + try: + source, lineno = inspect.getsourcelines(obj) + except: + lineno = None + + if lineno: + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) + else: + linespec = "" + + fn = os.path.relpath(fn, start=os.path.dirname(pandas.__file__)) + + if '+' in pandas.__version__: + return "http://github.com/pydata/pandas/blob/master/pandas/%s%s" % ( + fn, linespec) + else: + return "http://github.com/pydata/pandas/blob/v%s/pandas/%s%s" % ( + pandas.__version__, fn, linespec) + + # remove the docstring of the flags attribute (inherited from numpy ndarray) # because these give doc build errors (see GH issue 5331) def remove_flags_docstring(app, what, name, obj, options, lines):