-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrello_functions.py
409 lines (331 loc) · 10.8 KB
/
trello_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
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
"""
trello_functions.py
Helper functions for the trello_controller program. User 'pytrello' module
"""
import sys
import trello
import config
def connect_with_trello():
"""
Connects with Trello using config.py values.
"""
# .- stablish connection
print("Connecting with Trello...")
client = trello.TrelloClient(
api_key=config.API_KEY,
api_secret=config.API_SECRET
)
print("Connection stablished")
# .- return TrelloClient object
return client
def get_board_by_name(client=None, name=None):
"""
Returns board reference searched by name.
"""
# .- fail if name not provided
if name is None:
print("get_board_by_name(): a 'name' must be provided")
sys.exit(1)
else:
# .- compare lowercase
name = name.lower()
# .- stablish connection if not provided
if client is None:
client = connect_with_trello()
# .- get boards and obtain board object for name
board = None
for b in client.list_boards():
if (b.name.lower().find(name) != -1):
board = b
# .- return board reference
return board
def get_list_by_name(board=None, name=None):
"""
Returns list reference searched by name.
"""
# .- fail if no board provided
if board is None:
print("get_list_by_name(): a 'board' must be provided")
sys.exit(1)
# .- fail if no name provided
if name is None:
print("get_list_by_name(): a 'name' must be provided")
sys.exit(1)
else:
# .- lowercase to compare
name = name.lower()
# .- get lists and obtain list object for name
required_list = None
for l in board.all_lists():
if (l.name.lower().find(name) != -1):
required_list = l
# .- return required list reference
return required_list
def get_card_by_name(board=None, name=None):
"""
Returns card reference searched by name.
"""
# .- fail if no board provided
if board is None:
print("get_card_by_name(): a 'board' must be provided")
sys.exit(1)
# .- fail if no name provided
if name is None:
print("get_card_by_name(): a 'name' must be provided")
sys.exit(1)
else:
# .- lowercase to compare
name = name.lower()
# .- get cards and obtain card object for name
card = None
for c in board.all_cards():
if (c.name.lower().find(name) != -1):
card = c
# .- return required card reference
return card
def get_label_by_name(board=None, name=None):
"""
Returns label reference searched by name.
"""
# .- fail if no board provided
if board is None:
print("get_label_by_name(): a 'board' must be provided")
sys.exit(1)
if name is None:
print("get_label_by_name(): a label 'name' must be provided")
sys.exit(1)
required_label = None
for label in board.get_labels():
if label.name.upper() == name.upper():
required_label = label
return required_label
def get_card_by_id(board=None, cid=None):
"""
Returns card reference searched by id.
"""
# .- fail if no board provided
if board is None:
print("get_card_by_id(): a 'board' must be provided")
sys.exit(1)
# .- fail if no name provided
if cid is None:
print("get_card_by_id(): an 'id' must be provided")
sys.exit(1)
else:
# .- get only id number
short_id = int(str(cid).lstrip("T").lstrip("0"))
# .- get cards and obtain card object for name
card = None
for c in board.all_cards():
if (int(c.idShort) == short_id):
card = c
# .- return required card reference
return card
def get_label_by_id(board=None, lid=None):
"""
Returns label reference searched by id.
"""
# .- fail if no board provided
if board is None:
print("get_label_by_id(): a 'board' must be provided")
sys.exit(1)
if lid is None:
print("get_label_by_id(): a label 'id' must be provided")
sys.exit(1)
required_label = None
for label in board.get_labels():
if label.id == lid:
required_label = label
return required_label
def create_card(client=None, board=None, prefix="", values=None):
"""
Creates new card in a Trello 'board'.
"""
_create_card = True
# .- fail if no board provided
if board is None:
print("create_card(): a 'board' must be provided")
sys.exit(1)
else:
board = get_board_by_name(client, board)
# .- Check if name provided
if "name" not in values:
msg = "Card must have a 'name'"
print(msg)
_create_card = False
# .- fail if destination list not provided or not exists
if ("list" not in values) or (values["list"] is None):
print("create_card(): a destination 'list' must be provided")
sys.exit(1)
else:
dest_list = get_list_by_name(board, values["list"])
# .- no 'labels' in values or empty
if ("labels" not in values) or \
(values["labels"] is None) or \
(len(values["labels"]) == 0):
add_labels = False
else:
add_labels = True
# .- no 'description'
if "description" not in values:
values["description"] = None
# .- do not create card if exists
new_card = None
card_found = get_card_by_name(
board=board,
name=values["name"]
)
if (card_found is None) and (_create_card is True):
new_card = dest_list.add_card(
name=values["name"],
desc=values["description"],
)
# .- rename card with '[{prefix}{id_number}]'
task_id = "%04d" % new_card.idShort
short_id = "[{p}{ids}]".format(p=prefix, ids=task_id)
new_name = "{ids} {n}".format(ids=short_id, n=new_card.name)
new_card.set_name(new_name)
# .- add labels
if add_labels is True:
for label in values["labels"]:
# .- get label object
board_label = get_label_by_name(board, label)
# .- skip if label do not exists
if board_label is None:
print("Label '{label}' do not exists".format(label=label))
continue
else:
new_card.add_label(board_label)
else:
msg = "Card already exists with id: {id}".format(id=card_found.idShort)
print(msg)
# .- return generated card
return new_card
def create_board(client=None, values=None):
"""
Creates new board in Trello.
"""
# .- Connect with Trello if necessary
if client is None:
client = connect_with_trello()
# .- Check user input
if ("name" not in values) or (values["name"] is None):
print("You can not create an unnamed board")
sys.exit(1)
if ("permission_level" not in values):
values["permission_level"] = "private"
if "remove_labels" not in values:
values["remove_labels"] = True
flag_create_lists = True
if ("lists" not in values) or \
(len(values["lists"]) == 0):
flag_create_lists = False
# .- Check if board name exists
if get_board_by_name(client, values["name"]) is not None:
msg = "Board '{b}' already exists".format(b=values["name"])
print(msg)
else:
msg = "Creating board '{b}'...".format(b=values["name"])
print(msg)
board = client.add_board(
board_name=values["name"],
permission_level=values["permission_level"],
default_lists=False
)
# .- create lists if present
if flag_create_lists is True:
create_lists(
board=board,
lists_to_create=values["lists"]
)
# .- remove default labels
if values["remove_labels"] is True:
for label in board.get_labels():
print("Removing default label '{id}'".format(id=label.id))
board.delete_label(label.id)
print("Created board successfully")
def create_lists(board=None, lists_to_create=None, pos=-1):
"""
Creates multiple lists in a Trello 'board'.
"""
if board is None:
msg = "Cannot create list for board 'None'"
print(msg)
elif (lists_to_create is None) or \
(len(lists_to_create) == 0):
pass
else:
# .- generate list and place them from left to right on board
if pos <= 0:
pos = 1
for li in lists_to_create:
if get_list_by_name(board, li) is None:
print("Creating list: '{li}'".format(li=li))
board.add_list(li, pos)
pos += 1
def create_labels(client=None, board=None, labels=None):
"""
Creates multiple labels in a Trello 'board'.
"""
# .- fail if no board provided
if board is None:
print("create_labels(): a 'board' must be provided")
sys.exit(1)
else:
board = get_board_by_name(client, board)
# .- fail if destination list not provided or not exists
if labels is None:
print("create_labels(): a 'labels' list must be provided")
sys.exit(1)
available_labels = get_labels(board)
print("create_labels(): {}".format(labels))
for label in labels:
create_label(board, label, available_labels)
def create_label(board=None, values=None, available_labels=None):
"""
Creates label in a Trello 'board'.
"""
add_label = False
if available_labels is None:
available_labels = {}
# .- check label is valid
if "name" not in values:
print("create_label(): label must set a 'name'")
sys.exit(1)
elif values["name"].upper() not in available_labels:
add_label = True
if "color" not in values:
print("create_label(): setting default color to 'green'")
values["color"] = "green"
# .- create label
if add_label is True:
new_label = board.add_label(
name=values["name"],
color=values["color"]
)
msg = "Created label '{n}' with id: '{id}'". format(
n=new_label.name,
id=new_label.id
)
print(msg)
else:
msg = "Label named '{n}' already exists with id '{lid}'"
msg = msg.format(
n=values["name"],
lid=available_labels[values["name"].upper()]
)
print(msg)
def get_labels(board=None):
"""
Returns a Python dict with labels in a Trello 'board'.
"""
# .- fail if no board provided
if board is None:
print("get_labels(): a 'board' must be provided")
sys.exit(1)
else:
labels = board.get_labels()
labels_dict = {}
for label in labels:
labels_dict[label.name.upper()] = label.id
return labels_dict