-
Notifications
You must be signed in to change notification settings - Fork 952
/
Copy pathutils.py
432 lines (345 loc) · 14 KB
/
utils.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Copyright 2019 Ram Rachum and collaborators.
# This program is distributed under the MIT license.
import os
import re
import abc
import inspect
import sys
from pysnooper.utils import DEFAULT_REPR_RE
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
from . import mini_toolbox
import pysnooper.pycompat
def get_function_arguments(function, exclude=()):
try:
getfullargspec = inspect.getfullargspec
except AttributeError:
result = inspect.getargspec(function).args
else:
result = getfullargspec(function).args
for exclude_item in exclude:
result.remove(exclude_item)
return result
class _BaseEntry(pysnooper.pycompat.ABC):
def __init__(self, prefix='', min_python_version=None, max_python_version=None):
self.prefix = prefix
self.min_python_version = min_python_version
self.max_python_version = max_python_version
@abc.abstractmethod
def check(self, s):
pass
def is_compatible_with_current_python_version(self):
compatible = True
if self.min_python_version and self.min_python_version > sys.version_info:
compatible = False
if self.max_python_version and self.max_python_version < sys.version_info:
compatible = False
return compatible
def __repr__(self):
init_arguments = get_function_arguments(self.__init__,
exclude=('self',))
attributes = {
key: repr(getattr(self, key)) for key in init_arguments
if getattr(self, key) is not None
}
return '%s(%s)' % (
type(self).__name__,
', '.join('{key}={value}'.format(**locals()) for key, value
in attributes.items())
)
class _BaseValueEntry(_BaseEntry):
def __init__(self, prefix='', min_python_version=None,
max_python_version=None):
_BaseEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
self.line_pattern = re.compile(
r"""^%s(?P<indent>(?: {4})*)(?P<preamble>[^:]*):"""
r"""\.{2,7} (?P<content>.*)$""" % (re.escape(self.prefix),)
)
@abc.abstractmethod
def _check_preamble(self, preamble):
pass
@abc.abstractmethod
def _check_content(self, preamble):
pass
def check(self, s):
match = self.line_pattern.match(s)
if not match:
return False
_, preamble, content = match.groups()
return (self._check_preamble(preamble) and
self._check_content(content))
class ElapsedTimeEntry(_BaseEntry):
def __init__(self, elapsed_time_value=None, tolerance=0.2, prefix='',
min_python_version=None, max_python_version=None):
_BaseEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
self.line_pattern = re.compile(
r"""^%s(?P<indent>(?: {4})*)Elapsed time: (?P<time>.*)""" % (
re.escape(self.prefix),
)
)
self.elapsed_time_value = elapsed_time_value
self.tolerance = tolerance
def check(self, s):
match = self.line_pattern.match(s)
if not match:
return False
timedelta = pysnooper.pycompat.timedelta_parse(match.group('time'))
if self.elapsed_time_value:
return abs(timedelta.total_seconds() - self.elapsed_time_value) \
<= self.tolerance
else:
return True
class CallEndedByExceptionEntry(_BaseEntry):
# Todo: Looking at this class, we could rework the hierarchy.
def __init__(self, prefix=''):
_BaseEntry.__init__(self, prefix=prefix)
def check(self, s):
return re.match(
r'''(?P<indent>(?: {4})*)Call ended by exception''',
s
)
class VariableEntry(_BaseValueEntry):
def __init__(self, name=None, value=None, stage=None, prefix='',
name_regex=None, value_regex=None, min_python_version=None,
max_python_version=None):
_BaseValueEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if name is not None:
assert name_regex is None
if value is not None:
assert value_regex is None
assert stage in (None, 'starting', 'new', 'modified')
self.name = name
self.value = value
self.stage = stage
self.name_regex = (None if name_regex is None else
re.compile(name_regex))
self.value_regex = (None if value_regex is None else
re.compile(value_regex))
_preamble_pattern = re.compile(
r"""^(?P<stage>New|Modified|Starting) var$"""
)
def _check_preamble(self, preamble):
match = self._preamble_pattern.match(preamble)
if not match:
return False
stage = match.group('stage')
return self._check_stage(stage)
_content_pattern = re.compile(
r"""^(?P<name>.+?) = (?P<value>.+)$"""
)
def _check_content(self, content):
match = self._content_pattern.match(content)
if not match:
return False
name, value = match.groups()
return self._check_name(name) and self._check_value(value)
def _check_name(self, name):
if self.name is not None:
return name == self.name
elif self.name_regex is not None:
return self.name_regex.match(name)
else:
return True
def _check_value(self, value):
if self.value is not None:
return value == self.value
elif self.value_regex is not None:
return self.value_regex.match(value)
else:
return True
def _check_stage(self, stage):
stage = stage.lower()
if self.stage is None:
return stage in ('starting', 'new', 'modified')
else:
return stage == self.stage
class _BaseSimpleValueEntry(_BaseValueEntry):
def __init__(self, value=None, value_regex=None, prefix='',
min_python_version=None, max_python_version=None):
_BaseValueEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if value is not None:
assert value_regex is None
self.value = value
self.value_regex = (None if value_regex is None else
re.compile(value_regex))
def _check_preamble(self, preamble):
return bool(self._preamble_pattern.match(preamble))
def _check_content(self, content):
return self._check_value(content)
def _check_value(self, value):
if self.value is not None:
return value == self.value
elif self.value_regex is not None:
return self.value_regex.match(value)
else:
return True
class ReturnValueEntry(_BaseSimpleValueEntry):
_preamble_pattern = re.compile(
r"""^Return value$"""
)
class ExceptionValueEntry(_BaseSimpleValueEntry):
_preamble_pattern = re.compile(
r"""^Exception$"""
)
class SourcePathEntry(_BaseValueEntry):
def __init__(self, source_path=None, source_path_regex=None, prefix=''):
_BaseValueEntry.__init__(self, prefix=prefix)
if source_path is not None:
assert source_path_regex is None
self.source_path = source_path
self.source_path_regex = (None if source_path_regex is None else
re.compile(source_path_regex))
_preamble_pattern = re.compile(
r"""^Source path$"""
)
def _check_preamble(self, preamble):
return bool(self._preamble_pattern.match(preamble))
def _check_content(self, source_path):
if self.source_path is not None:
return source_path == self.source_path
elif self.source_path_regex is not None:
return self.source_path_regex.match(source_path)
else:
return True
class _BaseEventEntry(_BaseEntry):
def __init__(self, source=None, source_regex=None, thread_info=None,
thread_info_regex=None, prefix='', min_python_version=None,
max_python_version=None):
_BaseEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if type(self) is _BaseEventEntry:
raise TypeError
if source is not None:
assert source_regex is None
self.line_pattern = re.compile(
r"""^%s(?P<indent>(?: {4})*)(?:(?:[0-9:.]{15})|(?: {15})) """
r"""(?P<thread_info>[0-9]+-[0-9A-Za-z_-]+[ ]+)?"""
r"""(?P<event_name>[a-z_]*) +(?P<line_number>[0-9]*) """
r"""+(?P<source>.*)$""" % (re.escape(self.prefix,))
)
self.source = source
self.source_regex = (None if source_regex is None else
re.compile(source_regex))
self.thread_info = thread_info
self.thread_info_regex = (None if thread_info_regex is None else
re.compile(thread_info_regex))
@property
def event_name(self):
return re.match('^[A-Z][a-z_]*', type(self).__name__).group(0).lower()
def _check_source(self, source):
if self.source is not None:
return source == self.source
elif self.source_regex is not None:
return self.source_regex.match(source)
else:
return True
def _check_thread_info(self, thread_info):
if self.thread_info is not None:
return thread_info == self.thread_info
elif self.thread_info_regex is not None:
return self.thread_info_regex.match(thread_info)
else:
return True
def check(self, s):
match = self.line_pattern.match(s)
if not match:
return False
_, thread_info, event_name, _, source = match.groups()
return (event_name == self.event_name and
self._check_source(source) and
self._check_thread_info(thread_info))
class CallEntry(_BaseEventEntry):
pass
class LineEntry(_BaseEventEntry):
pass
class ReturnEntry(_BaseEventEntry):
pass
class ExceptionEntry(_BaseEventEntry):
pass
class OpcodeEntry(_BaseEventEntry):
pass
class OutputFailure(Exception):
pass
def verify_normalize(lines, prefix):
time_re = re.compile(r"[0-9:.]{15}")
src_re = re.compile(r'^(?: *)Source path:\.\.\. (.*)$')
for line in lines:
if DEFAULT_REPR_RE.search(line):
msg = "normalize is active, memory address should not appear"
raise OutputFailure(line, msg)
no_prefix = line.replace(prefix if prefix else '', '').strip()
if time_re.match(no_prefix):
msg = "normalize is active, time should not appear"
raise OutputFailure(line, msg)
m = src_re.match(line)
if m:
if not os.path.basename(m.group(1)) == m.group(1):
msg = "normalize is active, path should be only basename"
raise OutputFailure(line, msg)
def assert_output(output, expected_entries, prefix=None, normalize=False):
lines = tuple(filter(None, output.split('\n')))
if expected_entries and not lines:
raise OutputFailure("Output is empty")
if prefix is not None:
for line in lines:
if not line.startswith(prefix):
raise OutputFailure(line)
if normalize:
verify_normalize(lines, prefix)
# Filter only entries compatible with the current Python
filtered_expected_entries = []
for expected_entry in expected_entries:
if isinstance(expected_entry, _BaseEntry):
if expected_entry.is_compatible_with_current_python_version():
filtered_expected_entries.append(expected_entry)
else:
filtered_expected_entries.append(expected_entry)
expected_entries_count = len(filtered_expected_entries)
any_mismatch = False
result = ''
template = u'\n{line!s:%s} {expected_entry} {arrow}' % max(map(len, lines))
for expected_entry, line in zip_longest(filtered_expected_entries, lines, fillvalue=""):
mismatch = not (expected_entry and expected_entry.check(line))
any_mismatch |= mismatch
arrow = '<===' * mismatch
result += template.format(**locals())
if len(lines) != expected_entries_count:
result += '\nOutput has {} lines, while we expect {} lines.'.format(
len(lines), len(expected_entries))
if any_mismatch:
raise OutputFailure(result)
def assert_sample_output(module):
with mini_toolbox.OutputCapturer(stdout=False,
stderr=True) as output_capturer:
module.main()
placeholder_time = '00:00:00.000000'
time_pattern = '[0-9:.]{15}'
def normalise(out):
out = re.sub(time_pattern, placeholder_time, out).strip()
out = re.sub(
r'^( *)Source path:\.\.\. .*$',
r'\1Source path:... Whatever',
out,
flags=re.MULTILINE
)
return out
output = output_capturer.string_io.getvalue()
try:
assert (
normalise(output) ==
normalise(module.expected_output)
)
except AssertionError:
print('\n\nActual Output:\n\n' + output) # to copy paste into expected_output
raise # show pytest diff (may need -vv flag to see in full)