-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlocation.py
More file actions
370 lines (342 loc) · 12.2 KB
/
location.py
File metadata and controls
370 lines (342 loc) · 12.2 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from __future__ import annotations
from typing import *
from dataclasses import dataclass
import inspect
import linecache
import dis
import ast
import ansi
import utils
import myLogging
import sys
import abc
import parsecache
from parsecache import FunMatcher
import paths
import tokenize
import os
@dataclass
class EncodedBytes:
bytes: bytes
encoding: str
def __len__(self):
return len(self.bytes)
def countLeadingSpaces(self) -> int:
return len(self.bytes) - len(self.bytes.lstrip())
def decoded(self) -> str:
return self.bytes.decode(self.encoding, errors='replace')
@overload
def __getitem__(self, key: int) -> int: ...
@overload
def __getitem__(self, key: slice) -> str: ...
def __getitem__(self, key: int | slice) -> int | str:
if isinstance(key, int):
return self.bytes[key]
else:
b = self.bytes[key]
return b.decode(self.encoding, errors='replace')
@dataclass
class EncodedByteLines:
bytes: list[bytes]
encoding: str
_cache: dict[str, EncodedByteLines] = {}
def getline(filename, lineno):
"""
Returns a line of some source file as a bytearray. We use byte arrays because
location offsets are byte offsets.
"""
p = os.path.normpath(os.path.abspath(filename))
if p in _cache:
lines = _cache[p]
else:
try:
with open(filename, 'rb') as f:
byteLines = f.readlines()
except Exception:
byteLines = []
i = 0
def nextLine() -> bytes:
nonlocal i
if i < len(byteLines):
x = byteLines[i]
i = i + 1
return x
else:
return b''
encoding, _ = tokenize.detect_encoding(nextLine)
lines = EncodedByteLines(byteLines, encoding)
if 1 <= lineno <= len(lines.bytes):
x = lines.bytes[lineno - 1].rstrip(b'\n')
else:
x = b''
return EncodedBytes(x, encoding)
@dataclass
class Loc:
filename: str
startLine: int
startCol: Optional[int]
endLine: Optional[int]
endCol: Optional[int]
def __post_init__(self):
self.filename = paths.canonicalizePath(self.filename)
def fullSpan(self) -> Optional[tuple[int, int, int, int]]:
if self.startCol and self.endLine and self.endCol:
return (self.startLine, self.startCol, self.endLine, self.endCol)
else:
return None
def code(self) -> Optional[str]:
match self.fullSpan():
case None:
return None
case (startLine, startCol, endLine, endCol):
result = []
for lineNo in range(startLine, startLine+1):
line = getline(self.filename, lineNo)
c1 = startCol if lineNo == startLine else 0
c2 = endCol if lineNo == endLine else len(line)
result.append(line[c1:c2])
return '\n'.join(result)
@staticmethod
def fromFrameInfo(fi: inspect.FrameInfo) -> Loc:
default = Loc(fi.filename, fi.lineno, None, None, None)
if fi.positions is None:
return default
p: dis.Positions = fi.positions
startLine = p.lineno
endLine = p.end_lineno
startCol = p.col_offset
endCol = p.end_col_offset
if startLine is None or endLine is None or startCol is None or endCol is None:
return default
else:
return Loc(fi.filename, startLine, startCol, endLine, endCol)
HIGHLIGHTING_ENV_VAR = 'WYPP_HIGHLIGHTING'
type HighlightMode = Literal['color', 'text', 'off']
def getHighlightMode(mode: HighlightMode | Literal['fromEnv']) -> HighlightMode:
if mode == 'fromEnv':
fromEnv = utils.getEnv(HIGHLIGHTING_ENV_VAR, lambda x: x, None)
match fromEnv:
case 'color': return 'color'
case 'text': return 'text'
case 'off': return 'off'
case None: return 'color'
case _:
myLogging.warn(f'Invalid highlighting mode in environment variable {HIGHLIGHTING_ENV_VAR}: {fromEnv} (supported: color, text, off)')
return 'off'
else:
return mode
def highlight(s: str, mode: HighlightMode) -> str:
match mode:
case 'color': return ansi.red(s)
case 'off': return s
case 'text': return f'<<{s}>>'
@dataclass
class SourceLine:
line: EncodedBytes # without trailing \n
span: Optional[tuple[int, int]] # (inclusive, exclusive)
def highlight(self, mode: HighlightMode | Literal['fromEnv'] = 'fromEnv') -> str:
mode = getHighlightMode(mode)
if self.span:
l = self.line
return l[:self.span[0]] + highlight(l[self.span[0]:self.span[1]], mode) + l[self.span[1]:]
else:
return self.line.decoded()
def highlightedLines(loc: Loc) -> list[SourceLine]:
match loc.fullSpan():
case None:
line = getline(loc.filename, loc.startLine)
return [SourceLine(line, None)]
case (startLine, startCol, endLine, endCol):
result = []
for lineNo in range(startLine, startLine+1):
line = getline(loc.filename, lineNo)
leadingSpaces = line.countLeadingSpaces()
c1 = startCol if lineNo == startLine else leadingSpaces
c2 = endCol if lineNo == endLine else len(line)
result.append(SourceLine(line, (c1, c2)))
return result
@dataclass
class ClassMember:
kind: Literal['method', 'recordConstructor']
className: str
type CallableKind = Literal['function'] | ClassMember
@dataclass
class CallableName:
name: str
kind: CallableKind
@staticmethod
def mk(c: CallableInfo) -> CallableName:
return CallableName(c.name, c.kind)
class CallableInfo(abc.ABC):
"""
Class giving access to various properties of a function, method or constructor.
"""
def __init__(self, kind: CallableKind):
self.kind: CallableKind = kind
@property
@abc.abstractmethod
def name(self) -> str:
pass
@abc.abstractmethod
def getResultTypeLocation(self) -> Optional[Loc]:
pass
@abc.abstractmethod
def getParamSourceLocation(self, paramName: str) -> Optional[Loc]:
pass
@property
@abc.abstractmethod
def isAsync(self) -> bool:
pass
class StdCallableInfo(CallableInfo):
"""
Class giving access to various properties of a function
(arguments, result type etc.)
"""
def __init__(self, f: Callable, kind: CallableKind):
super().__init__(kind)
self.file = f.__code__.co_filename
self.__lineno = f.__code__.co_firstlineno
self.__name = f.__name__
self.__ast = parsecache.getAST(self.file)
self.__async = None
def __repr__(self):
return f'StdCallableInfo({self.name}, {self.kind})'
@property
def name(self):
return self.__name
def _findDef(self) -> Optional[ast.FunctionDef | ast.AsyncFunctionDef]:
m = FunMatcher(self.__name, self.__lineno)
match self.kind:
case 'function':
return self.__ast.getFunDef(m)
case ClassMember('method', clsName):
return self.__ast.getMethodDef(clsName, m)
case k:
raise ValueError(f'Unexpected CallableKind {k} in StdCallableInfo')
def getResultTypeLocation(self) -> Optional[Loc]:
"""
Returns the location of the result type
"""
node = self._findDef()
if not node:
return None
r = node.returns
if r:
return Loc(self.file,
r.lineno,
r.col_offset,
r.end_lineno,
r.end_col_offset)
else:
# There is no return type annotation
return None
def getParamSourceLocation(self, paramName: str) -> Optional[Loc]:
"""
Returns the location of the parameter with the given name.
"""
node = self._findDef()
if not node:
return None
res = None
for arg in node.args.args + node.args.kwonlyargs:
if arg.arg == paramName:
res = arg
break
if res is None:
# Look in vararg and kwarg
if node.args.vararg and node.args.vararg.arg == paramName:
res = node.args.vararg
if node.args.kwarg and node.args.kwarg.arg == paramName:
res = node.args.kwarg
if res is None:
return None
else:
return Loc(self.file,
res.lineno,
res.col_offset,
res.end_lineno,
res.end_col_offset)
@property
def isAsync(self) -> bool:
if self.__async is None:
node = self._findDef()
self.__async = isinstance(node, ast.AsyncFunctionDef)
return self.__async
def classFilename(cls) -> str | None:
"""Best-effort path to the file that defined `cls`."""
try:
fn = inspect.getsourcefile(cls) or inspect.getfile(cls)
if fn:
return fn
except TypeError:
pass
# Fallback via the owning module (works for some frozen/zip cases)
mod = sys.modules.get(cls.__module__)
if mod is not None:
return getattr(mod, "__file__", None) or getattr(getattr(mod, "__spec__", None), "origin", None)
return None
class RecordConstructorInfo(CallableInfo):
"""
Class giving access to various properties of a record constructor.
"""
def __init__(self, cls: type):
super().__init__(ClassMember('recordConstructor', cls.__name__))
self.__cls = cls
def __repr__(self):
return f'RecordConstructorInfo({self.name})'
@property
def name(self):
return self.__cls.__name__
def getResultTypeLocation(self) -> Optional[Loc]:
return None
def getParamSourceLocation(self, paramName: str) -> Optional[Loc]:
file = classFilename(self.__cls)
if not file:
return None
ast = parsecache.getAST(file)
node = ast.getRecordAttr(self.name, paramName)
if node:
return Loc(file, node.lineno, node.col_offset, node.end_lineno, node.end_col_offset)
else:
return None
@property
def isAsync(self) -> bool:
return False
def locationOfArgument(fi: inspect.FrameInfo, idxOrName: int | str) -> Optional[Loc]:
"""
Given a stack frame with a function call f(arg1, arg2, ..., argN), returns
the source code location of the i-th argument.
"""
loc = Loc.fromFrameInfo(fi)
match loc.fullSpan():
case (startLine, startCol, _endLine, _endCol):
codeOfCall = loc.code()
if codeOfCall is None:
return loc
try:
tree = ast.parse(codeOfCall)
except SyntaxError:
return loc
match tree:
case ast.Module([ast.Expr(ast.Call(_fun, args, kwArgs))]):
arg = None
if isinstance(idxOrName, int):
idx = idxOrName
if idx >= 0 and idx < len(args):
arg = args[idx]
else:
matching = [k.value for k in kwArgs if k.arg == idxOrName]
if matching:
arg = matching[0]
if arg is not None:
if arg.end_lineno is not None and arg.end_col_offset is not None:
callStartLine = startLine + arg.lineno - 1
callStartCol = startCol + arg.col_offset
callEndLine = startLine + arg.end_lineno - 1
if arg.lineno != arg.end_lineno:
callEndCol = arg.end_col_offset
else:
callEndCol = startCol + arg.end_col_offset
return Loc(loc.filename, callStartLine, callStartCol,
callEndLine, callEndCol)
return loc