-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
168 lines (152 loc) · 5.74 KB
/
functions.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
import copy
TABLE_TEMPLATE = {
"object": "block",
"has_children": True,
"archived": False,
"type": "table",
"table": {
"table_width": 5,
"has_column_header": True,
"has_row_header": False,
"children": [
{
"object": "block",
"has_children": False,
"archived": False,
"type": "table_row",
"table_row": {
"cells": [
[
{
"type": "text",
"text": {"content": "Name", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "Name",
"href": None,
}
],
[
{
"type": "text",
"text": {"content": "Description", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "Description",
"href": None,
}
],
[
{
"type": "text",
"text": {"content": "Unit Price", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "Unit Price",
"href": None,
}
],
[
{
"type": "text",
"text": {"content": "Quantity", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "Quantity",
"href": None,
}
],
[
{
"type": "text",
"text": {"content": "Total", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "Total",
"href": None,
}
],
]
},
},
],
},
}
CELL_TEMPLATE = {
"type": "text",
"text": {"content": "FIXME", "link": None},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": False,
"code": False,
"color": "default",
},
"plain_text": "FIXME",
"href": None,
}
ROW_TEMPLATE = {
"object": "block",
"has_children": False,
"archived": False,
"type": "table_row",
"table_row": {"cells": []},
}
ORDER = ["Name", "Description", "Price", "Quantity", "Total"]
def _eval_prop(prop, eval_property_value):
value = eval_property_value(prop["type"], prop[prop["type"]])
return value
def _build_prop_map(match, eval_property_value=None):
return {
name: eval_property_value(prop["type"], prop[prop["type"]])
for name, prop in match.value["properties"].items()
}
def _prepare_cell(value):
cell = copy.deepcopy(CELL_TEMPLATE)
cell["text"]["content"] = str(value)
cell["plain_text"] = str(value)
return cell
def _prepare_row(match, eval_property_value):
row = copy.deepcopy(ROW_TEMPLATE)
prop_map = _build_prop_map(match, eval_property_value)
for column in ORDER:
cell = _prepare_cell(prop_map.get(column, ""))
row["table_row"]["cells"].append([cell])
return row
def line_items(matches, eval_property_value=None):
table = copy.deepcopy(TABLE_TEMPLATE)
for match in matches:
row = _prepare_row(match, eval_property_value)
table["table"]["children"].append(row)
return table