/
__init__.py
461 lines (380 loc) · 12 KB
/
__init__.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
from httplib2 import Http
from urllib import urlencode
from datetime import datetime
import exceptions
import json
import oauth2 as oauth
import os
import random
import time
import urlparse
class ResourceUnavailable(Exception):
"""Exception representing a failed request to a resource"""
def __init__(self, msg, http_response):
Exception.__init__(self)
self._msg = msg
self._status = http_response.status
def __str__(self):
return "Resource unavailable: %s (HTTP status: %s)" % (self._msg, self._status)
class Unauthorized(ResourceUnavailable):
pass
class TrelloClient(object):
""" Base class for Trello API access """
def __init__(self, api_key, token, api_secret = None, token_secret = None):
"""
Constructor
:api_key: API key generated at https://trello.com/1/appKey/generate
:oauth_token: OAuth token generated by the user
"""
if api_key and api_secret and token and token_secret:
# oauth
self.oauth_consumer = oauth.Consumer(key = api_key, secret = api_secret)
self.oauth_token = oauth.Token(key = token, secret = token_secret)
self.client = oauth.Client(self.oauth_consumer, self.oauth_token)
elif api_key and token:
self.client = Http()
self.api_key = api_key
self.auth_token = token
def logout(self):
"""Log out of Trello. This method is idempotent."""
# TODO: refactor
pass
#if not self._cookie:
#return
#headers = {'Cookie': self._cookie, 'Accept': 'application/json'}
#response, content = self.client.request(
#'https://trello.com/logout',
#'GET',
#headers = headers,
#)
## TODO: error checking
#self._cookie = None
def build_url(self, path, query = {}):
"""
Builds a Trello URL.
:path: URL path
:params: dict of key-value pairs for the query string
"""
url = 'https://api.trello.com/1'
if path[0:1] != '/':
url += '/'
url += path
if hasattr(self, 'oauth_token'):
url += '?'
url += "key="+self.oauth_token.key
url += "&token="+self.oauth_consumer.key
else:
url += '?'
url += "key="+self.api_key
url += "&token="+self.auth_token
if len(query) > 0:
url += '&'+urlencode(query)
return url
def list_boards(self):
"""
Returns all boards for your Trello user
:return: a list of Python objects representing the Trello boards. Each board has the
following noteworthy attributes:
- id: the board's identifier
- name: Name of the board
- desc: Description of the board (optional - may be missing from the returned JSON)
- closed: Boolean representing whether this board is closed or not
- url: URL to the board
"""
json_obj = self.fetch_json('/members/me/boards/all')
boards = list()
for obj in json_obj:
boards.append(self._board_from_json(obj))
return boards
def get_board(self, board_id):
obj = self.fetch_json('/boards/' + board_id)
return self._board_from_json(obj)
def get_list(self, list_id):
obj = self.fetch_json('/lists/' + list_id)
list = List(self.get_board(obj['idBoard']), obj['id'], name=obj['name'].encode('utf-8'))
list.closed = obj['closed']
return list
def get_member(self, member_id):
return Member(self, member_id).fetch()
def fetch_json(
self,
uri_path,
http_method = 'GET',
headers = {},
query_params = {},
post_args = {}):
""" Fetch some JSON from Trello """
if http_method in ("POST", "PUT", "DELETE"):
headers['Content-type'] = 'application/json'
headers['Accept'] = 'application/json'
url = self.build_url(uri_path, query_params)
response, content = self.client.request(
url,
http_method,
headers = headers,
body = json.dumps(post_args))
# error checking
if response.status == 401:
raise Unauthorized(url)
if response.status != 200:
raise ResourceUnavailable(url, response)
return json.loads(content)
def _board_from_json(self, json):
board = Board(self, json['id'], name = json['name'].encode('utf-8'))
board.description = json.get('desc','').encode('utf-8')
board.closed = json['closed']
board.url = json['url']
return board
class Board(object):
"""Class representing a Trello board. Board attributes are stored as normal Python attributes;
access to all sub-objects, however, is always an API call (Lists, Cards).
"""
def __init__(self, client, board_id, name=''):
"""Constructor.
:trello: Reference to a Trello object
:board_id: ID for the board
"""
self.client = client
self.id = board_id
self.name = name
def __repr__(self):
return '<Board %s>' % self.name
def fetch(self):
"""Fetch all attributes for this board"""
json_obj = self.client.fetch_json('/boards/'+self.id)
self.name = json_obj['name'].encode('utf-8')
self.description = json_obj.get('desc','')
self.closed = json_obj['closed']
self.url = json_obj['url']
def save(self):
pass
def all_lists(self):
"""Returns all lists on this board"""
return self.get_lists('all')
def open_lists(self):
"""Returns all open lists on this board"""
return self.get_lists('open')
def closed_lists(self):
"""Returns all closed lists on this board"""
return self.get_lists('closed')
def get_lists(self, list_filter):
# error checking
json_obj = self.client.fetch_json(
'/boards/'+self.id+'/lists',
query_params = {'cards': 'none', 'filter': list_filter})
lists = list()
for obj in json_obj:
l = List(self, obj['id'], name=obj['name'].encode('utf-8'))
l.closed = obj['closed']
lists.append(l)
return lists
def add_list(self, name):
"""Add a card to this list
:name: name for the card
:return: the card
"""
obj = self.client.fetch_json(
'/lists',
http_method = 'POST',
post_args = {'name': name, 'idBoard': self.id},)
list = List(self, obj['id'], name=obj['name'].encode('utf-8'))
list.closed = obj['closed']
return list
def all_cards(self):
"""Returns all cards on this board"""
return self.get_cards('all')
def open_cards(self):
"""Returns all open cards on this board"""
return self.get_cards('open')
def closed_cards(self):
"""Returns all closed cards on this board"""
return self.get_cards('closed')
def get_cards(self, card_filter):
# error checking
json_obj = self.client.fetch_json(
'/boards/'+self.id+'/cards',
query_params = {'filter': card_filter})
cards = list()
for obj in json_obj:
card = Card(self, obj['id'], name=obj['name'].encode('utf-8'))
card.closed = obj['closed']
card.member_ids = obj['idMembers']
cards.append(card)
return cards
class List(object):
"""Class representing a Trello list. List attributes are stored on the object, but access to
sub-objects (Cards) require an API call"""
def __init__(self, board, list_id, name=''):
"""Constructor
:board: reference to the parent board
:list_id: ID for this list
"""
self.board = board
self.client = board.client
self.id = list_id
self.name = name
def __repr__(self):
return '<List %s>' % self.name
def fetch(self):
"""Fetch all attributes for this list"""
json_obj = self.client.fetch_json('/lists/'+self.id)
self.name = json_obj['name'].encode('utf-8')
self.closed = json_obj['closed']
def list_cards(self):
"""Lists all cards in this list"""
json_obj = self.client.fetch_json('/lists/'+self.id+'/cards')
cards = list()
for c in json_obj:
card = Card(self, c['id'], name = c['name'].encode('utf-8'))
card.description = c.get('desc','').encode('utf-8')
card.closed = c['closed']
card.url = c['url']
card.member_ids = c['idMembers']
cards.append(card)
return cards
def add_card(self, name, desc = None):
"""Add a card to this list
:name: name for the card
:return: the card
"""
json_obj = self.client.fetch_json(
'/lists/'+self.id+'/cards',
http_method = 'POST',
post_args = {'name': name, 'idList': self.id, 'desc': desc},)
card = Card(self, json_obj['id'])
card.name = json_obj['name']
card.description = json_obj.get('desc','')
card.closed = json_obj['closed']
card.url = json_obj['url']
card.member_ids = json_obj['idMembers']
return card
def fetch_actions(self, action_filter):
"""
Fetch actions for this list can give more argv to action_filter,
split for ',' json_obj is list
"""
json_obj = self.client.fetch_json(
'/lists/'+self.id+'/actions',
query_params = {'filter': action_filter})
self.actions = json_obj
class Card(object):
"""
Class representing a Trello card. Card attributes are stored on
the object
"""
def __init__(self, trello_list, card_id, name=''):
"""Constructor
:trello_list: reference to the parent list
:card_id: ID for this card
"""
self.trello_list = trello_list
self.client = trello_list.client
self.id = card_id
self.name = name
def __repr__(self):
return '<Card %s>' % self.name
def fetch(self):
"""Fetch all attributes for this card"""
json_obj = self.client.fetch_json(
'/cards/'+self.id,
query_params = {'badges': False})
self.name = json_obj['name'].encode('utf-8')
self.description = json_obj.get('desc','')
self.closed = json_obj['closed']
self.url = json_obj['url']
self.member_ids = json_obj['idMembers']
self.short_id = json_obj['idShort']
self.list_id = json_obj['idList']
self.board_id = json_obj['idBoard']
self.labels = json_obj['labels']
self.badges = json_obj['badges']
self.checked = json_obj['checkItemStates']
self.checklists = []
if self.badges['checkItems'] > 0:
json_obj = self.client.fetch_json(
'/cards/'+self.id+'/checklists',)
for cl in json_obj:
self.checklists.append(Checklist(self.client, self.checked, cl))
self.comments = []
if self.badges['comments'] > 0:
self.comments = self.client.fetch_json(
'/cards/'+self.id+'/actions',
query_params = {'filter': 'commentCard'})
def fetch_actions(self, action_filter='createCard'):
"""
Fetch actions for this card can give more argv to action_filter,
split for ',' json_obj is list
"""
json_obj = self.client.fetch_json(
'/cards/'+self.id+'/actions',
query_params = {'filter': action_filter})
self.actions = json_obj
@property
def create_date(self):
self.fetch_actions()
date_str = self.actions[0]['date'][:-5]
return datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S')
def set_description(self, description):
self._set_remote_attribute('desc', description)
self.description = description
def set_closed(self, closed):
self._set_remote_attribute('closed', closed)
self.closed = closed
def delete(self):
# Delete this card permanently
self.client.fetch_json(
'/cards/'+self.id,
http_method = 'DELETE',)
def assign(self, member_id):
self.client.fetch_json(
'/cards/'+self.id+'/members',
http_method = 'POST',
post_args = {'value' : member_id, })
def change_list(self, list_id):
self.client.fetch_json(
'/cards/'+self.id+'/idList',
http_method = 'PUT',
post_args = {'value' : list_id, })
def _set_remote_attribute(self, attribute, value):
self.client.fetch_json(
'/cards/'+self.id+'/'+attribute,
http_method = 'PUT',
post_args = {'value': value,},)
class Member(object):
"""
Class representing a Trello member.
"""
def __init__(self, client, member_id):
self.client = client
self.id = member_id
def __repr__(self):
return '<Member %s>' % self.id
def fetch(self):
"""Fetch all attributes for this card"""
json_obj = self.client.fetch_json(
'/members/'+self.id,
query_params = {'badges': False})
self.status = json_obj['status'].encode('utf-8')
self.id = json_obj.get('id','')
self.bio = json_obj.get('bio','')
self.url = json_obj.get('url','')
self.username = json_obj['username'].encode('utf-8')
self.full_name = json_obj['fullName'].encode('utf-8')
self.initials = json_obj['initials'].encode('utf-8')
return self
class Checklist(object):
"""
Class representing a Trello checklist.
"""
def __init__(self, client, checked, obj):
self.client = client
self.id = obj['id']
self.items = obj['checkItems']
for i in self.items:
i['checked'] = False
for cis in checked:
if cis['idCheckItem'] == i['id'] and cis['state'] == 'complete':
i['checked'] = True
def __repr__(self):
return '<Checklist %s>' % self.id
# vim:noexpandtab