Determine the URL corresponding to Python object
(domain, info)
| 346 | |
| 347 | # based on numpy doc/source/conf.py |
| 348 | def linkcode_resolve(domain, info): |
| 349 | """ |
| 350 | Determine the URL corresponding to Python object |
| 351 | """ |
| 352 | if domain != "py": |
| 353 | return None |
| 354 | |
| 355 | modname = info["module"] |
| 356 | fullname = info["fullname"] |
| 357 | |
| 358 | submod = sys.modules.get(modname) |
| 359 | if submod is None: |
| 360 | return None |
| 361 | |
| 362 | obj = submod |
| 363 | for part in fullname.split("."): |
| 364 | try: |
| 365 | obj = getattr(obj, part) |
| 366 | except AttributeError: |
| 367 | return None |
| 368 | |
| 369 | try: |
| 370 | fn = inspect.getsourcefile(inspect.unwrap(obj)) |
| 371 | except TypeError: |
| 372 | fn = None |
| 373 | if not fn: |
| 374 | return None |
| 375 | |
| 376 | try: |
| 377 | source, lineno = inspect.getsourcelines(obj) |
| 378 | except OSError: |
| 379 | lineno = None |
| 380 | |
| 381 | if lineno: |
| 382 | linespec = f"#L{lineno}-L{lineno + len(source) - 1}" |
| 383 | else: |
| 384 | linespec = "" |
| 385 | |
| 386 | fn = os.path.relpath(fn, start=os.path.dirname(xarray.__file__)) |
| 387 | |
| 388 | return f"https://github.com/pydata/xarray/blob/{source_ref}/xarray/{fn}{linespec}" |
| 389 | |
| 390 | |
| 391 | def html_page_context(app, pagename, templatename, context, doctree): |