-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappings.py
151 lines (133 loc) · 5.85 KB
/
mappings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
from pymod.index import modules
py3b = 'https://docs.python.org/3'
py2b = 'https://docs.python.org/2'
from os.path import join
make = lambda v, *segs: v == 3 and join(py3b, *segs) or join(py2b, *segs)
lib3 = lambda page: make(3, 'library', page)
lib2 = lambda page: make(2, 'library', page)
expanded = {
'2': {
'abc': lib2('abc.html'),
'itertools': lib2('itertools.html'),
'ifilterfalse': lib2('itertools.html#itertools.ifilterfalse'),
'izip_longest': lib2('itertools.html#itertools.izip_longest'),
},
'3': {
'abc': lib3('abc.html'),
'ABC': lib3('abc.html#abc.ABC'),
'abc.ABC': lib3('abc.html#abc.ABC'),
'itertools': lib3('itertools.html'),
'accumulate': lib3('itertools.html#itertools.accumulate'),
'zip_longest': lib3('itertools.html#itertools.zip_longest'),
}
}
def abc_same(name):
expanded['2'][name] = lib2('abc.html#abc.'+name)
expanded['3'][name] = lib3('abc.html#abc.'+name)
for name in ('ABCMeta', 'ABCMeta.register', 'ABCMeta.__subclasshook__',
'abstractmethod', 'abstractstaticmethod', 'abstractclassmethod',
'abstractproperty', 'get_cache_token'):
abc_same(name)
def itertools_same(name):
expanded['2'][name] = lib2('itertools.html#itertools.'+name)
expanded['3'][name] = lib3('itertools.html#itertools.'+name)
for name in ('chain', 'chain.from_iterable', 'combinations',
'combinations_with_replacement', 'groupby', 'islice',
'compress', 'count', 'cycle', 'dropwhile', 'filterfalse',
'permutations', 'product', 'repeat', 'starmap', 'takewhile',
'tee'):
itertools_same(name)
prio = ['itertools', 'functools', 'string', 'math', 'os', 'sys', 'pdb', 'operator',
'random', 're', 'shutil', 'struct', 'ssl', 'abc', 'argparse', 'errno',
'tempfile', 'textwrap', 'time', 'types', 'uuid', 'warnings', 'xml.dom']
const = lambda n: {
'3': 'https://docs.python.org/3/library/constants.html#{}'.format(n),
'2': 'https://docs.python.org/2/library/constants.html#{}'.format(n) }
constants = {
'False': const('False'),
'True': const('True'),
'None': const('None'),
'NotImplemented': const('NotImplemented'),
'Ellipsis': const('Ellipsis'),
'__debug__': const('__debug__'),
'quit': const('quit'),
'exit': const('exit'),
'copyright': const('copyright'),
'license': const('license'),
'credits': const('credits') }
mods = modules()
funcs = {'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
'callable', 'chr', 'classmethod', 'compile', 'complex', 'delattr',
'dir', 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float',
'format', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex',
'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',
'locals', 'map', 'max', 'min', 'next', 'object', 'oct', 'open',
'ord', 'pow', 'print', 'property', 'repr', 'reversed', 'round',
'setattr', 'slice', 'sorted', 'staticmethod', 'sum', 'super',
'type', 'vars', 'zip', '__import__'}
structs = {
'dict': 'dictionaries',
'set': 'sets',
'tuple': 'tuples-and-sequences',
'list': 'more-on-lists'
}
excs = { 'BaseException', 'BaseException.args', 'Exception', 'StandardError',
'ArithmeticError', 'BufferError', 'LookupError', 'EnvironmentError',
'AssertionError', 'AttributeError', 'EOFError', 'FloatingPointError',
'GeneratorExit', 'IOError', 'ImportError', 'IndexError', 'KeyError',
'KeyboardInterrupt', 'MemoryError', 'NameError', 'NotImplementedError',
'OSError', 'OverflowError', 'ReferenceError', 'RuntimeError',
'StopIteration', 'SyntaxError', 'IndentationError', 'TabError',
'SystemError', 'SystemExit', 'TypeError', 'UnboundLocalError',
'UnicodeError', 'UnicodeError.encoding', 'UnicodeError.reason',
'UnicodeError.object', 'UnicodeError.start', 'UnicodeError.end',
'UnicodeEncodeError', 'UnicodeDecodeError', 'UnicodeTranslateError',
'ValueError', 'VMSError', 'WindowsError', 'ZeroDivisionError',
'Warning', 'UserWarning', 'DeprecationWarning',
'PendingDeprecationWarning', 'SyntaxWarning', 'RuntimeWarning',
'FutureWarning', 'ImportWarning', 'UnicodeWarning', }
fn3base = 'https://docs.python.org/3/library/functions.html#{}'
structbase = 'https://docs.python.org/{}/tutorial/datastructures.html#{}'
ex3base = 'https://docs.python.org/3/library/exceptions.html#{}'
fn2base = 'https://docs.python.org/2/library/functions.html#{}'
ex2base = 'https://docs.python.org/2/library/exceptions.html#exceptions.{}'
specials = {
'zen': 'https://www.python.org/dev/peps/pep-0020/',
'antigravity': 'http://xkcd.com/353/'
}
def url(v, term):
if term in constants:
return constants[term][v]
if term in funcs and v == '3':
return fn3base.format(term)
elif term in funcs and v == '2':
return fn2base.format(term)
if term in structs:
return structbase.format(v, structs[term])
return structbase.format(term)
if term in excs and v == '3':
return ex3base.format(term)
elif term in excs and v == '2':
return ex2base.format(term)
elif term in specials:
return specials[term]
if v == '2' and term == 'configparser':
term = 'ConfigParser'
try:
return mods[term][v]['M'].replace('ConfigParser.html',
'configparser.html')
except KeyError:
pass
modn = term.split('.')[0]
if modn:
mod = mods.get(modn)
if mod is None:
for pname in prio:
if pname in mods:
cand = url(v, '{}.{}'.format(pname, term))
if cand:
return cand
return None
return v in mod and mod[v].get(term) or None
return None