-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello.py
executable file
·313 lines (242 loc) · 8.99 KB
/
trello.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
#!/usr/bin/env python
import json
import urllib
from http.client import HTTPSConnection, HTTPException
import os
class TrelloSession:
def __init__(self):
self._httpconn = HTTPSConnection('api.trello.com')
#self._httpconn.set_debuglevel(1)
self._boards = None
path = os.path.join(os.path.expanduser("~"), ".trello")
with open(path, 'r') as f:
self._config = json.load(f)
self._key = self._config['key']
self._token = self._config['token']
@property
def boards(self):
if self._boards is None:
self._boards = {}
for b in self.request('GET', '/1/member/me/boards'):
board = Board(self, b)
self._boards[board.name] = board
return self._boards
def request(self, action, path, post_data = {}):
data = post_data.copy()
data['key'] = self._key
data['token'] = self._token
path += '?' + urllib.parse.urlencode(data)
self._httpconn.request(action, path, None, {})
response = self._httpconn.getresponse()
if response.status != 200:
raise Exception("Error {}: {}" \
.format(response.status, response.reason))
return json.loads(response.read().decode('utf-8'))
class Card:
def __init__(self, session, data):
self._session = session
self._data = data
self.name = self._data['name']
self.id = self._data['id']
self._labels = None
self._label_ids = None
self._members = None
def __repr__(self):
return "Card: " + json.dumps(self._data, indent = 4, sort_keys = True)
def changeList( self, idList ):
self._session.request('PUT',
'/1/cards/{}/idList'.format(self.id),
{ "value": idList } )
@property
def labels(self):
if self._labels == None:
self._labels = {}
for l in self._data['labels']:
label = Label(self._session, l)
self._labels[label.name] = label
return self._labels
@property
def label_ids(self):
if self._label_ids == None:
self._label_ids = {}
for l in self._data['labels']:
label = self.labels[l['name']]
self._label_ids[label.id] = label
return self._label_ids
def addLabel(self, label):
self._session.request('POST',
'/1/cards/{}/idLabels'.format(self.id),
{'value': label})
def deleteLabel(self, label):
self._session.request('DELETE',
'/1/cards/{}/idLabels/{}'.format(self.id, label),
{'value': label})
@property
def members(self):
return self._data['idMembers']
@members.setter
def members(self, members):
self._session.request('PUT',
'/1/cards/{}/idMembers'.format(self.id),
{'value': ','.join(members)})
@property
def desc(self):
return self._data['desc']
@desc.setter
def desc(self, description):
self._session.request('PUT',
'/1/cards/{}/desc'.format(self.id),
{'value': description})
@property
def shortUrl(self):
return self._data['shortUrl']
def addComment(self, comment):
self._session.request('POST',
'/1/cards/{}/actions/comments'.format(self.id),
{'text': comment})
def delete(self):
self._session.request('DELETE', '/1/cards/{}'.format(self.id))
@property
def listId(self):
return self._data['idList']
@listId.setter
def listId(self, newid):
self._session.request('PUT',
'/1/cards/{}/idList'.format(self.id),
{'value': newid})
class List:
def __init__(self, session, data):
self._session = session
self._data = data
self.name = self._data['name']
self.id = self._data['id']
self._cards = None
def __repr__(self):
return "List: " + json.dumps(self._data, indent = 4, sort_keys = True)
@property
def cards(self):
if self._cards == None:
self._cards = {}
for c in self._session.request('GET', '/1/lists/{}/cards' \
.format(self.id)):
card = Card(self._session, c)
self._cards[card.name] = card
return self._cards
def createCard(self, name, descr, labels = "", members = ""):
data = {
'name': name,
'desc': descr,
'due': "null",
'urlSource': "null",
'idList': self.id,
'idLabels': labels,
'idMembers': members
}
return Card(self._session,
self._session.request('POST', '/1/cards', data))
class Label:
def __init__(self, session, data):
self._session = session
self._data = data
self.name = self._data['name']
self.id = self._data['id']
def __repr__(self):
return "List: " + json.dumps(self._data, indent = 4, sort_keys = True)
class Member:
def __init__(self, session, data):
self._session = session
self._data = data
self.username = self._data['username']
self.fullName = self._data['fullName']
self.id = self._data['id']
self._initials = None
def __repr__(self):
return "Member: " + json.dumps(self._data, indent = 4, sort_keys = True)
@property
def initials(self):
if self._initials == None:
self._initials = self._session.request('GET',
'/1/members/{}/initials' \
.format(self.id))['_value']
return self._initials
class Board:
def __init__(self, session, data):
self._session = session
self._data = data
self.name = self._data['name']
self.id = self._data['id']
self._lists = None
self._labels = None
self._members = None
self._cards = None
def __repr__(self):
return "Board: " + json.dumps(self._data, indent = 4, sort_keys = True)
@property
def lists(self):
if self._lists == None:
lists = self._session.request('GET', '/1/boards/{}/lists' \
.format(self.id))
self._lists = {}
self._listsById = {}
for list in lists:
l = List(self._session, list)
self._lists[list['name']] = l
self._listsById[list['id']] = l
return self._lists
@property
def listsById(self):
self.lists
return self._listsById
@property
def labels(self):
if self._labels == None:
labels = self._session.request('GET', '/1/boards/{}/labels' \
.format(self.id))
self._labels = {}
for label in labels:
self._labels[label['name']] = Label(self._session, label)
return self._labels
@property
def members(self):
if self._members == None:
members = self._session.request('GET', '/1/boards/{}/members' \
.format(self.id))
self._members = {}
self._membersById = {}
for member in members:
m = Member(self._session, member)
self._members[member['username']] = m
self._membersById[member['id']] = m
return self._members
@property
def membersById(self):
self.members
return self._membersById
def addMember(self, name, member_type = 'normal'):
self._session.request('PUT', '/1/boards/{}/members/{}' \
.format(self.id, name),
{'idMember': name,
'type': member_type})
def delMember(self, id):
self._session.request('DELETE', '/1/boards/{}/members/{}' \
.format(self.id, id),
{'idMember': id})
def copyMembers(self, to_board):
for member in self.members:
to_board.addMember(self.members[member].username)
@property
def cards(self):
if self._cards == None:
cards = self._session.request('GET', '/1/boards/{}/cards' \
.format(self.id))
self._cards = {}
self._cardsById = {}
for card in cards:
c = Card(self._session, card)
self._cards[card['name']] = c
self._cardsById[card['id']] = c
return self._cards
@property
def cardsById(self):
self.cards
return self._cardsById