-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
153 lines (109 loc) · 4.25 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
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
""" application helpers """
import logging as log
def log_info(identifier, message):
"""
Log an info-level item containing an identifier and a message
Parameters:
identifier (string): Function or route identifier.
message (string or dict): String or Dict of info-level message.
Returns:
bool: Boolean True
"""
# if `message` is a dict, print the individual keys and values
if isinstance(message, dict):
log.info("[%s] \n%s\n", identifier, ", ".join(f"\n{key}: {value}" for key, value in message.items()))
else:
log.info("[%s] %s", identifier, message)
return True
def log_debug(identifier, message):
"""
Log a debug-level item containing an identifier and a message
Parameters:
identifier (string): Function or route identifier.
message (string or dict): String or Dict of debug-level message.
Returns:
bool: Boolean True
"""
# if `message` is a dict, print the individual keys and values
if isinstance(message, dict):
log.debug("[%s] \n%s\n", identifier, ", ".join(f"\n{key}: {value}" for key, value in message.items()))
else:
log.debug("[%s] %s", identifier, message)
return True
def log_error(identifier, message):
"""
Log an error-level item containing an identifier and a message
Parameters:
identifier (string): Function or route identifier.
message (string or dict): String of error-level message.
Returns:
bool: Boolean True
"""
log.error("[%s] %s", identifier, message)
return True
def log_exception(identifier, exception):
"""
Log an exception-level item containing an identifier and an exception
Parameters:
identifier (string): Function or route identifier.
exception (exception): Exception.
Returns:
bool: Boolean True
"""
log.error("[%s] exception occurred", identifier)
log.exception(exception)
return True
def generate_relative_coordinates(height, width, offset, object_size):
"""
Log an exception-level item containing an identifier and an exception
Parameters:
height (int): Height of the eInk screen.
width (int): Width of the eInk screen.
offset (int): Offset of the object.
object_size (int): Size of the object.
Returns:
bool: Boolean True
"""
longer_side = max(height, width)
shorter_side = min(height, width)
log_debug('generate_relative_coordinates', f'longer_side: {longer_side}, shorter_side: {shorter_side}')
x = 0
y = 0
# if offset is negative, place object relative to bottom right corner
if offset < 0:
# calculate position of object by substracting offset and object size from bottom right coordinates
# bottom right coordinates are the edge of the (height x width)
x = int(longer_side - (abs(offset) * longer_side) - object_size[0])
y = int(shorter_side - (abs(offset) * shorter_side) - object_size[1])
else:
# offset is positive, place object relative to upper left corner
x = int(offset * longer_side)
y = int(offset * shorter_side)
print(x, y)
return x, y
def format_url_map(url_map, hidden_routes, visible_methods, visible_prefix):
"""
Format a (Flask) URL Map to meet certain conditions
Parameters:
url_map (object): Object of Flask URL Maps
hidden_routes (list): List of Routes that should be hidden (omitted)
visible_methods (list): List of (HTTP) Methods that should be visible
visible_prefix (string): Prefix of the visible routes
Returns:
object: Object containing formatted routes
"""
routes = {
method: [] for method in visible_methods
}
# iterate over all received URLs
for route in url_map:
# only consider rules that start with `visible_prefix`
if str(route).startswith(visible_prefix):
methods = [
method for method in route.methods if method in visible_methods
]
for method in methods:
log_debug('format_url_map', f'route: {route}, method: {method}')
routes[method].append(str(route))
print(routes)
return routes