1+ import importlib
2+ import inspect
3+ import os
4+ import sys
5+
6+ import xbox .webapi
7+
8+ # -- Linkcode resolver -----------------------------------------------------
9+
10+ # This is HEAVILY inspired by numpy's
11+ # https://github.com/numpy/numpy/blob/73fe877ff967f279d470b81ad447b9f3056c1335/doc/source/conf.py#L390
12+
13+ # Copyright (c) 2005-2020, NumPy Developers.
14+ # All rights reserved.
15+ #
16+ # Redistribution and use in source and binary forms, with or without
17+ # modification, are permitted provided that the following conditions are
18+ # met:
19+ #
20+ # * Redistributions of source code must retain the above copyright
21+ # notice, this list of conditions and the following disclaimer.
22+ #
23+ # * Redistributions in binary form must reproduce the above
24+ # copyright notice, this list of conditions and the following
25+ # disclaimer in the documentation and/or other materials provided
26+ # with the distribution.
27+ #
28+ # * Neither the name of the NumPy Developers nor the names of any
29+ # contributors may be used to endorse or promote products derived
30+ # from this software without specific prior written permission.
31+ #
32+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
44+
45+ def linkcode_resolve (domain , info ):
46+ """
47+ Determine the url corresponding to Python object
48+ """
49+ if domain != "py" :
50+ return None
51+
52+ modname = info ["module" ]
53+ fullname = info ["fullname" ]
54+
55+ try :
56+ importlib .import_module (modname )
57+ except Exception :
58+ return None
59+ submod = sys .modules .get (modname )
60+ if submod is None :
61+ return None
62+
63+ obj = submod
64+ for part in fullname .split ("." ):
65+ try :
66+ obj = getattr (obj , part )
67+ except Exception :
68+ return None
69+
70+ # strip decorators, which would resolve to the source of the decorator
71+ # possibly an upstream bug in getsourcefile, bpo-1764286
72+ try :
73+ unwrap = inspect .unwrap
74+ except AttributeError :
75+ pass
76+ else :
77+ obj = unwrap (obj )
78+
79+ fn = None
80+ lineno = None
81+
82+ try :
83+ fn = inspect .getsourcefile (obj )
84+ except Exception :
85+ fn = None
86+ if not fn :
87+ return None
88+
89+ try :
90+ source , lineno = inspect .getsourcelines (obj )
91+ except Exception :
92+ lineno = None
93+
94+ fn = os .path .relpath (fn , start = os .path .dirname (xbox .webapi .__file__ ))
95+
96+ if lineno :
97+ linespec = "#L%d-L%d" % (lineno , lineno + len (source ) - 1 )
98+ else :
99+ linespec = ""
100+
101+ url = "https://github.com/OpenXbox/xbox-webapi-python/blob/%s/xbox/webapi/%s%s"
102+ # url = "https://github.com/pyca/cryptography/blob/%s/src/cryptography/%s%s"
103+ if "dev" in xbox .webapi .__version__ :
104+ return url % ("master" , fn , linespec )
105+ else :
106+ version = f"v{ xbox .webapi .__version__ } "
107+ return url % (version , fn , linespec )
0 commit comments