-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathformatters.py
83 lines (56 loc) · 1.9 KB
/
formatters.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
import functools
import html
import re
MYPY = False
if MYPY:
from typing import Callable, List
def indent(level):
indentation = level * ' '
return lambda t: indentation + t
def reduced_indent(level):
level = max(4, level)
def dedent(t):
return re.sub(r'^(E\s+)', 'E' + (level - 1) * ' ', t)
return dedent
def escape(t):
return html.escape(t, quote=False)
def replace_spaces(t):
return t.replace(' ', ' ')
def format_line(fns, line):
# type: (List[Callable[[str], str]], str) -> str
return functools.reduce(lambda t, fn: fn(t), fns, line)
def line_formatter(fns):
# type: (List[Callable[[str], str]]) -> Callable[[str], str]
return functools.partial(format_line, fns)
def format_text(formatter, text):
return '<br />'.join(map(formatter, text.split("\n")))
def _format_text(formatter, text):
return format_text(line_formatter(formatter), text)
class ShortTraceback:
@classmethod
def formatter(cls, indentation_level):
return (reduced_indent(indentation_level), escape,
replace_spaces)
@classmethod
def format_text(cls, text, indentation_level):
return _format_text(cls.formatter(indentation_level), text)
class LineTraceback:
@classmethod
def formatter(cls, indentation_level):
return (indent(indentation_level), escape, replace_spaces)
@classmethod
def format_text(cls, text, indentation_level):
return _format_text(cls.formatter(indentation_level), text)
class LongTraceback:
@classmethod
def formatter(cls, indentation_level):
return (reduced_indent(indentation_level), escape, replace_spaces)
@classmethod
def format_text(cls, text, indentation_level):
return _format_text(cls.formatter(indentation_level), text)
TB_MODES = {
'line': LineTraceback,
'short': ShortTraceback,
'long': LongTraceback,
'auto': LongTraceback
}