|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| - |
| 2 | +import inspect |
3 | 3 | import os
|
4 | 4 | import sys
|
| 5 | +from os.path import dirname, relpath |
5 | 6 |
|
6 | 7 | import alagitpull
|
7 | 8 |
|
|
25 | 26 | 'sphinx.ext.autodoc',
|
26 | 27 | 'sphinx.ext.intersphinx',
|
27 | 28 | 'sphinx.ext.todo',
|
| 29 | + 'sphinx.ext.linkcode', |
28 | 30 | 'sphinxcontrib.napoleon',
|
29 | 31 | 'releases',
|
30 | 32 | 'alagitpull',
|
|
116 | 118 | ]
|
117 | 119 |
|
118 | 120 | intersphinx_mapping = {'http://docs.python.org/': None}
|
| 121 | + |
| 122 | + |
| 123 | +def linkcode_resolve(domain, info): # NOQA: C901 |
| 124 | + """ |
| 125 | + Determine the URL corresponding to Python object |
| 126 | +
|
| 127 | + Notes |
| 128 | + ----- |
| 129 | + From https://github.com/numpy/numpy/blob/v1.15.1/doc/source/conf.py, 7c49cfa |
| 130 | + on Jul 31. License BSD-3. https://github.com/numpy/numpy/blob/v1.15.1/LICENSE.txt |
| 131 | + """ |
| 132 | + if domain != 'py': |
| 133 | + return None |
| 134 | + |
| 135 | + modname = info['module'] |
| 136 | + fullname = info['fullname'] |
| 137 | + |
| 138 | + submod = sys.modules.get(modname) |
| 139 | + if submod is None: |
| 140 | + return None |
| 141 | + |
| 142 | + obj = submod |
| 143 | + for part in fullname.split('.'): |
| 144 | + try: |
| 145 | + obj = getattr(obj, part) |
| 146 | + except Exception: |
| 147 | + return None |
| 148 | + |
| 149 | + # strip decorators, which would resolve to the source of the decorator |
| 150 | + # possibly an upstream bug in getsourcefile, bpo-1764286 |
| 151 | + try: |
| 152 | + unwrap = inspect.unwrap |
| 153 | + except AttributeError: |
| 154 | + pass |
| 155 | + else: |
| 156 | + obj = unwrap(obj) |
| 157 | + |
| 158 | + try: |
| 159 | + fn = inspect.getsourcefile(obj) |
| 160 | + except Exception: |
| 161 | + fn = None |
| 162 | + if not fn: |
| 163 | + return None |
| 164 | + |
| 165 | + try: |
| 166 | + source, lineno = inspect.getsourcelines(obj) |
| 167 | + except Exception: |
| 168 | + lineno = None |
| 169 | + |
| 170 | + if lineno: |
| 171 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 172 | + else: |
| 173 | + linespec = "" |
| 174 | + |
| 175 | + fn = relpath(fn, start=dirname(libtmux.__file__)) |
| 176 | + |
| 177 | + if 'dev' in about['__version__']: |
| 178 | + return "%s/blob/master/%s/%s%s" % ( |
| 179 | + about['__github__'], |
| 180 | + about['__package_name__'], |
| 181 | + fn, |
| 182 | + linespec, |
| 183 | + ) |
| 184 | + else: |
| 185 | + return "%s/blob/v%s/%s/%s%s" % ( |
| 186 | + about['__github__'], |
| 187 | + about['__version__'], |
| 188 | + about['__package_name__'], |
| 189 | + fn, |
| 190 | + linespec, |
| 191 | + ) |
0 commit comments