|
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 |
|
| 9 | +import tmuxp |
| 10 | + |
8 | 11 | # Get the project root dir, which is the parent dir of this |
9 | 12 | cwd = os.getcwd() |
10 | 13 | project_root = os.path.dirname(cwd) |
|
23 | 26 | 'sphinx.ext.intersphinx', |
24 | 27 | 'sphinx.ext.todo', |
25 | 28 | 'sphinxcontrib.napoleon', |
| 29 | + 'sphinx.ext.linkcode', |
26 | 30 | 'aafig', |
27 | 31 | 'releases', |
28 | 32 | 'alagitpull', |
|
121 | 125 | aafig_format = dict(latex='pdf', html='gif') |
122 | 126 |
|
123 | 127 | aafig_default_options = dict(scale=.75, aspect=0.5, proportional=True) |
| 128 | + |
| 129 | + |
| 130 | +def linkcode_resolve(domain, info): # NOQA: C901 |
| 131 | + """ |
| 132 | + Determine the URL corresponding to Python object |
| 133 | +
|
| 134 | + Notes |
| 135 | + ----- |
| 136 | + From https://github.com/numpy/numpy/blob/v1.15.1/doc/source/conf.py, 7c49cfa |
| 137 | + on Jul 31. License BSD-3. https://github.com/numpy/numpy/blob/v1.15.1/LICENSE.txt |
| 138 | + """ |
| 139 | + if domain != 'py': |
| 140 | + return None |
| 141 | + |
| 142 | + modname = info['module'] |
| 143 | + fullname = info['fullname'] |
| 144 | + |
| 145 | + submod = sys.modules.get(modname) |
| 146 | + if submod is None: |
| 147 | + return None |
| 148 | + |
| 149 | + obj = submod |
| 150 | + for part in fullname.split('.'): |
| 151 | + try: |
| 152 | + obj = getattr(obj, part) |
| 153 | + except Exception: |
| 154 | + return None |
| 155 | + |
| 156 | + # strip decorators, which would resolve to the source of the decorator |
| 157 | + # possibly an upstream bug in getsourcefile, bpo-1764286 |
| 158 | + try: |
| 159 | + unwrap = inspect.unwrap |
| 160 | + except AttributeError: |
| 161 | + pass |
| 162 | + else: |
| 163 | + obj = unwrap(obj) |
| 164 | + |
| 165 | + try: |
| 166 | + fn = inspect.getsourcefile(obj) |
| 167 | + except Exception: |
| 168 | + fn = None |
| 169 | + if not fn: |
| 170 | + return None |
| 171 | + |
| 172 | + try: |
| 173 | + source, lineno = inspect.getsourcelines(obj) |
| 174 | + except Exception: |
| 175 | + lineno = None |
| 176 | + |
| 177 | + if lineno: |
| 178 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 179 | + else: |
| 180 | + linespec = "" |
| 181 | + |
| 182 | + fn = relpath(fn, start=dirname(tmuxp.__file__)) |
| 183 | + |
| 184 | + if 'dev' in about['__version__']: |
| 185 | + return "%s/blob/master/%s/%s%s" % ( |
| 186 | + about['__github__'], |
| 187 | + about['__package_name__'], |
| 188 | + fn, |
| 189 | + linespec, |
| 190 | + ) |
| 191 | + else: |
| 192 | + return "%s/blob/v%s/%s/%s%s" % ( |
| 193 | + about['__github__'], |
| 194 | + about['__version__'], |
| 195 | + about['__package_name__'], |
| 196 | + fn, |
| 197 | + linespec, |
| 198 | + ) |
0 commit comments