|
13 | 13 | import sys
|
14 | 14 | import os
|
15 | 15 | import re
|
| 16 | +import inspect |
16 | 17 | from pandas.compat import u, PY3
|
17 | 18 |
|
18 | 19 | # If extensions (or modules to document with autodoc) are in another directory,
|
|
47 | 48 | 'sphinx.ext.coverage',
|
48 | 49 | 'sphinx.ext.pngmath',
|
49 | 50 | 'sphinx.ext.ifconfig',
|
| 51 | + 'sphinx.ext.linkcode', |
50 | 52 | ]
|
51 | 53 |
|
52 | 54 |
|
@@ -424,6 +426,55 @@ def get_items(self, names):
|
424 | 426 | return items
|
425 | 427 |
|
426 | 428 |
|
| 429 | +# based on numpy doc/source/conf.py |
| 430 | +def linkcode_resolve(domain, info): |
| 431 | + """ |
| 432 | + Determine the URL corresponding to Python object |
| 433 | + """ |
| 434 | + if domain != 'py': |
| 435 | + return None |
| 436 | + |
| 437 | + modname = info['module'] |
| 438 | + fullname = info['fullname'] |
| 439 | + |
| 440 | + submod = sys.modules.get(modname) |
| 441 | + if submod is None: |
| 442 | + return None |
| 443 | + |
| 444 | + obj = submod |
| 445 | + for part in fullname.split('.'): |
| 446 | + try: |
| 447 | + obj = getattr(obj, part) |
| 448 | + except: |
| 449 | + return None |
| 450 | + |
| 451 | + try: |
| 452 | + fn = inspect.getsourcefile(obj) |
| 453 | + except: |
| 454 | + fn = None |
| 455 | + if not fn: |
| 456 | + return None |
| 457 | + |
| 458 | + try: |
| 459 | + source, lineno = inspect.getsourcelines(obj) |
| 460 | + except: |
| 461 | + lineno = None |
| 462 | + |
| 463 | + if lineno: |
| 464 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 465 | + else: |
| 466 | + linespec = "" |
| 467 | + |
| 468 | + fn = os.path.relpath(fn, start=os.path.dirname(pandas.__file__)) |
| 469 | + |
| 470 | + if '+' in pandas.__version__: |
| 471 | + return "http://github.com/pydata/pandas/blob/master/pandas/%s%s" % ( |
| 472 | + fn, linespec) |
| 473 | + else: |
| 474 | + return "http://github.com/pydata/pandas/blob/v%s/pandas/%s%s" % ( |
| 475 | + pandas.__version__, fn, linespec) |
| 476 | + |
| 477 | + |
427 | 478 | # remove the docstring of the flags attribute (inherited from numpy ndarray)
|
428 | 479 | # because these give doc build errors (see GH issue 5331)
|
429 | 480 | def remove_flags_docstring(app, what, name, obj, options, lines):
|
|
0 commit comments