This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
90 lines (66 loc) · 2.02 KB
/
helpers.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
import inspect
import sys
from collections import OrderedDict
from colors import red, yellow, magenta
from config import TESTING, DEBUG
def debug(*args):
"""
Debug output
"""
if DEBUG:
print(magenta(('{} ' * len(args)).format(*args)))
def neighborhood(iterable):
"""
Yield the list as (item, next item).
"""
iterator = iter(iterable)
item = next(iterator) # throws StopIteration if empty.
for _next in iterator:
yield (item, _next)
item = _next
yield (item, None)
def get_ordered_annotations(func):
"""
Get annotations for function as an OrderedDict
"""
annotations = OrderedDict()
signature = inspect.signature(func)
for parameter in signature.parameters.values():
annotations[parameter.name] = parameter.annotation
if signature.return_annotation != signature.empty:
annotations['return'] = signature.return_annotation
return annotations
###############################################################################
# ERROR HANDLING
###############################################################################
# FIXME: Unify
def fatal_error(msg, exc_class, line=None, exit_func=lambda: sys.exit(1)):
if TESTING:
raise exc_class(msg) from None
else:
if line:
msg = line_error(msg, line)
print(red('FATAL ERROR:'), msg)
exit_func()
def syntax_error(msg, exc_class, line=None, exit_func=lambda: sys.exit(1)):
if TESTING:
raise exc_class(msg) from None
else:
if line:
msg = line_error(msg, line)
print(red('SYNTAX ERROR:'), msg)
exit_func()
def warn(msg, exc_class, line=None):
if TESTING:
raise exc_class(msg) from None
else:
if line:
msg = line_error(msg, line)
print(yellow('WARNING:'), msg)
def line_error(msg, line):
"""
:type line: Line
"""
return '{}\n\nIn {}\n{:02} {}'.format(
msg, line.filename, line.lineno + 1, line.original_contents,
)