|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +import inspect |
2 | 3 | import os
|
| 4 | +import sys |
| 5 | +from os.path import dirname, relpath |
3 | 6 |
|
4 | 7 | import alagitpull
|
5 | 8 | from recommonmark.transform import AutoStructify
|
6 | 9 |
|
| 10 | +import libvcs |
| 11 | + |
7 | 12 | # Get the project root dir, which is the parent dir of this
|
8 | 13 | cwd = os.getcwd()
|
9 | 14 | project_root = os.path.dirname(cwd)
|
@@ -128,3 +133,74 @@ def setup(app):
|
128 | 133 | 'py': ('https://docs.python.org/2', None),
|
129 | 134 | 'pip': ('http://pip.readthedocs.io/en/latest/', None),
|
130 | 135 | }
|
| 136 | + |
| 137 | + |
| 138 | +def linkcode_resolve(domain, info): # NOQA: C901 |
| 139 | + """ |
| 140 | + Determine the URL corresponding to Python object |
| 141 | +
|
| 142 | + Notes |
| 143 | + ----- |
| 144 | + From https://github.com/numpy/numpy/blob/v1.15.1/doc/source/conf.py, 7c49cfa |
| 145 | + on Jul 31. License BSD-3. https://github.com/numpy/numpy/blob/v1.15.1/LICENSE.txt |
| 146 | + """ |
| 147 | + if domain != 'py': |
| 148 | + return None |
| 149 | + |
| 150 | + modname = info['module'] |
| 151 | + fullname = info['fullname'] |
| 152 | + |
| 153 | + submod = sys.modules.get(modname) |
| 154 | + if submod is None: |
| 155 | + return None |
| 156 | + |
| 157 | + obj = submod |
| 158 | + for part in fullname.split('.'): |
| 159 | + try: |
| 160 | + obj = getattr(obj, part) |
| 161 | + except Exception: |
| 162 | + return None |
| 163 | + |
| 164 | + # strip decorators, which would resolve to the source of the decorator |
| 165 | + # possibly an upstream bug in getsourcefile, bpo-1764286 |
| 166 | + try: |
| 167 | + unwrap = inspect.unwrap |
| 168 | + except AttributeError: |
| 169 | + pass |
| 170 | + else: |
| 171 | + obj = unwrap(obj) |
| 172 | + |
| 173 | + try: |
| 174 | + fn = inspect.getsourcefile(obj) |
| 175 | + except Exception: |
| 176 | + fn = None |
| 177 | + if not fn: |
| 178 | + return None |
| 179 | + |
| 180 | + try: |
| 181 | + source, lineno = inspect.getsourcelines(obj) |
| 182 | + except Exception: |
| 183 | + lineno = None |
| 184 | + |
| 185 | + if lineno: |
| 186 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 187 | + else: |
| 188 | + linespec = "" |
| 189 | + |
| 190 | + fn = relpath(fn, start=dirname(libvcs.__file__)) |
| 191 | + |
| 192 | + if 'dev' in about['__version__']: |
| 193 | + return "%s/blob/master/%s/%s%s" % ( |
| 194 | + about['__github__'], |
| 195 | + about['__package_name__'], |
| 196 | + fn, |
| 197 | + linespec, |
| 198 | + ) |
| 199 | + else: |
| 200 | + return "%s/blob/v%s/%s/%s%s" % ( |
| 201 | + about['__github__'], |
| 202 | + about['__version__'], |
| 203 | + about['__package_name__'], |
| 204 | + fn, |
| 205 | + linespec, |
| 206 | + ) |
0 commit comments