Minor cleaning up of the new read code
[doneit] / src / rtm_api.py
1
2 """
3 Python library for Remember The Milk API
4
5 @note For help, see http://www.rememberthemilk.com/services/api/methods/
6 """
7
8 import weakref
9 import warnings
10 import urllib
11 import urllib2
12 import hashlib
13 import time
14
15 _use_simplejson = False
16 try:
17         import simplejson
18         _use_simplejson = True
19 except ImportError:
20         pass
21
22
23 __author__ = 'Sridhar Ratnakumar <http://nearfar.org/>'
24
25 SERVICE_URL = 'http://api.rememberthemilk.com/services/rest/'
26 AUTH_SERVICE_URL = 'http://www.rememberthemilk.com/services/auth/'
27
28
29 class RTMError(StandardError):
30         pass
31
32
33 class RTMAPIError(RTMError):
34         pass
35
36
37 class RTMParseError(RTMError):
38         pass
39
40
41 class AuthStateMachine(object):
42         """If the state is in those setup for the machine, then return
43         the datum sent.  Along the way, it is an automatic call if the
44         datum is a method.
45         """
46
47         class NoData(RTMError):
48                 pass
49
50         def __init__(self, states):
51                 self.states = states
52                 self.data = {}
53
54         def dataReceived(self, state, datum):
55                 if state not in self.states:
56                         raise RTMError, "Invalid state <%s>" % state
57                 self.data[state] = datum
58
59         def get(self, state):
60                 if state in self.data:
61                         return self.data[state]
62                 else:
63                         raise AuthStateMachine.NoData('No data for <%s>' % state)
64
65
66 class RTMapi(object):
67
68         def __init__(self, userID, apiKey, secret, token=None):
69                 self._userID = userID
70                 self._apiKey = apiKey
71                 self._secret = secret
72                 self._authInfo = AuthStateMachine(['frob', 'token'])
73
74                 # this enables one to do 'rtm.tasks.getList()', for example
75                 for prefix, methods in API.items():
76                         setattr(self, prefix,
77                                         RTMAPICategory(self, prefix, methods))
78
79                 if token:
80                         self._authInfo.dataReceived('token', token)
81
82         def _sign(self, params):
83                 "Sign the parameters with MD5 hash"
84                 pairs = ''.join(['%s%s' % (k, v) for (k, v) in sortedItems(params)])
85                 return hashlib.md5(self._secret+pairs).hexdigest()
86
87         @staticmethod
88         def open_url(url, queryArgs=None):
89                 if queryArgs:
90                         url += '?' + urllib.urlencode(queryArgs)
91                 warnings.warn("Performing download of %s" % url, stacklevel=5)
92                 return urllib2.urlopen(url)
93
94         @staticmethod
95         def read_by_length(connection, timeout):
96                 # It appears that urllib uses the non-blocking variant of file objects
97                 # which means reads might not always be complete, so grabbing as much
98                 # of the data as possible with a sleep in between to give it more time
99                 # to grab data.
100                 contentLengthField = "Content-Length"
101                 assert contentLengthField in connection.info(), "Connection didn't provide content length info"
102                 specifiedLength = int(connection.info()["Content-Length"])
103
104                 actuallyRead = 0
105                 chunks = []
106
107                 chunk = connection.read()
108                 actuallyRead += len(chunk)
109                 chunks.append(chunk)
110                 while 0 < timeout and actuallyRead < specifiedLength:
111                         time.sleep(1)
112                         timeout -= 1
113                         chunk = connection.read()
114                         actuallyRead += len(chunk)
115                         chunks.append(chunk)
116
117                 json = "".join(chunks)
118
119                 if "Content-Length" in connection.info():
120                         assert len(json) == int(connection.info()["Content-Length"]), "The packet header promised %s of data but only was able to read %s of data" % (
121                                 connection.info()["Content-Length"],
122                                 len(json),
123                         )
124
125                 return json
126
127         @staticmethod
128         def read_by_guess(connection, timeout):
129                 # It appears that urllib uses the non-blocking variant of file objects
130                 # which means reads might not always be complete, so grabbing as much
131                 # of the data as possible with a sleep in between to give it more time
132                 # to grab data.
133
134                 chunks = []
135
136                 chunk = connection.read()
137                 chunks.append(chunk)
138                 while chunk and 0 < timeout:
139                         time.sleep(1)
140                         timeout -= 1
141                         chunk = connection.read()
142                         chunks.append(chunk)
143
144                 json = "".join(chunks)
145
146                 if "Content-Length" in connection.info():
147                         assert len(json) == int(connection.info()["Content-Length"]), "The packet header promised %s of data but only was able to read %s of data" % (
148                                 connection.info()["Content-Length"],
149                                 len(json),
150                         )
151
152                 return json
153
154         def get(self, **params):
155                 "Get the XML response for the passed `params`."
156                 params['api_key'] = self._apiKey
157                 params['format'] = 'json'
158                 params['api_sig'] = self._sign(params)
159
160                 connection = self.open_url(SERVICE_URL, params)
161                 json = self.read_by_guess(connection, 5)
162                 # json = self.read_by_length(connection, 5)
163
164                 data = DottedDict('ROOT', parse_json(json))
165                 rsp = data.rsp
166
167                 if rsp.stat == 'fail':
168                         raise RTMAPIError, 'API call failed - %s (%s)' % (
169                                 rsp.err.msg, rsp.err.code)
170                 else:
171                         return rsp
172
173         def getNewFrob(self):
174                 rsp = self.get(method='rtm.auth.getFrob')
175                 self._authInfo.dataReceived('frob', rsp.frob)
176                 return rsp.frob
177
178         def getAuthURL(self):
179                 try:
180                         frob = self._authInfo.get('frob')
181                 except AuthStateMachine.NoData:
182                         frob = self.getNewFrob()
183
184                 params = {
185                         'api_key': self._apiKey,
186                         'perms': 'delete',
187                         'frob': frob
188                 }
189                 params['api_sig'] = self._sign(params)
190                 return AUTH_SERVICE_URL + '?' + urllib.urlencode(params)
191
192         def getToken(self):
193                 frob = self._authInfo.get('frob')
194                 rsp = self.get(method='rtm.auth.getToken', frob=frob)
195                 self._authInfo.dataReceived('token', rsp.auth.token)
196                 return rsp.auth.token
197
198
199 class RTMAPICategory(object):
200         "See the `API` structure and `RTM.__init__`"
201
202         def __init__(self, rtm, prefix, methods):
203                 self._rtm = weakref.ref(rtm)
204                 self._prefix = prefix
205                 self._methods = methods
206
207         def __getattr__(self, attr):
208                 if attr not in self._methods:
209                         raise AttributeError, 'No such attribute: %s' % attr
210
211                 rargs, oargs = self._methods[attr]
212                 if self._prefix == 'tasksNotes':
213                         aname = 'rtm.tasks.notes.%s' % attr
214                 else:
215                         aname = 'rtm.%s.%s' % (self._prefix, attr)
216                 return lambda **params: self.callMethod(
217                         aname, rargs, oargs, **params
218                 )
219
220         def callMethod(self, aname, rargs, oargs, **params):
221                 # Sanity checks
222                 for requiredArg in rargs:
223                         if requiredArg not in params:
224                                 raise TypeError, 'Required parameter (%s) missing' % requiredArg
225
226                 for param in params:
227                         if param not in rargs + oargs:
228                                 warnings.warn('Invalid parameter (%s)' % param)
229
230                 return self._rtm().get(method=aname,
231                                                         auth_token=self._rtm()._authInfo.get('token'),
232                                                         **params)
233
234
235 def sortedItems(dictionary):
236         "Return a list of (key, value) sorted based on keys"
237         keys = dictionary.keys()
238         keys.sort()
239         for key in keys:
240                 yield key, dictionary[key]
241
242
243 class DottedDict(object):
244         "Make dictionary items accessible via the object-dot notation."
245
246         def __init__(self, name, dictionary):
247                 self._name = name
248
249                 if isinstance(dictionary, dict):
250                         for key, value in dictionary.items():
251                                 if isinstance(value, dict):
252                                         value = DottedDict(key, value)
253                                 elif isinstance(value, (list, tuple)):
254                                         value = [DottedDict('%s_%d' % (key, i), item)
255                                                          for i, item in enumerate(value)]
256                                 setattr(self, key, value)
257
258         def __repr__(self):
259                 children = [c for c in dir(self) if not c.startswith('_')]
260                 return '<dotted %s: %s>' % (
261                         self._name,
262                         ', '.join(children))
263
264         def __str__(self):
265                 children = [(c, getattr(self, c)) for c in dir(self) if not c.startswith('_')]
266                 return '{dotted %s: %s}' % (
267                         self._name,
268                         ', '.join(
269                                 ('%s: "%s"' % (k, str(v)))
270                                 for (k, v) in children)
271                 )
272
273
274 def safer_eval(string):
275         try:
276                 return eval(string, {}, {})
277         except SyntaxError, e:
278                 print "="*60
279                 print string
280                 print "="*60
281                 newE = RTMParseError("Error parseing json")
282                 newE.error = e
283                 raise newE
284
285
286 if _use_simplejson:
287         parse_json = simplejson.loads
288 else:
289         parse_json = safer_eval
290
291
292 API = {
293         'auth': {
294                 'checkToken':
295                         [('auth_token'), ()],
296                 'getFrob':
297                         [(), ()],
298                 'getToken':
299                         [('frob'), ()]
300         },
301         'contacts': {
302                 'add':
303                         [('timeline', 'contact'), ()],
304                 'delete':
305                         [('timeline', 'contact_id'), ()],
306                 'getList':
307                         [(), ()],
308         },
309         'groups': {
310                 'add':
311                         [('timeline', 'group'), ()],
312                 'addContact':
313                         [('timeline', 'group_id', 'contact_id'), ()],
314                 'delete':
315                         [('timeline', 'group_id'), ()],
316                 'getList':
317                         [(), ()],
318                 'removeContact':
319                         [('timeline', 'group_id', 'contact_id'), ()],
320         },
321         'lists': {
322                 'add':
323                         [('timeline', 'name'), ('filter'), ()],
324                 'archive':
325                         [('timeline', 'list_id'), ()],
326                 'delete':
327                         [('timeline', 'list_id'), ()],
328                 'getList':
329                         [(), ()],
330                 'setDefaultList':
331                         [('timeline'), ('list_id'), ()],
332                 'setName':
333                         [('timeline', 'list_id', 'name'), ()],
334                 'unarchive':
335                         [('timeline'), ('list_id'), ()],
336         },
337         'locations': {
338                 'getList':
339                         [(), ()],
340         },
341         'reflection': {
342                 'getMethodInfo':
343                         [('methodName',), ()],
344                 'getMethods':
345                         [(), ()],
346         },
347         'settings': {
348                 'getList':
349                         [(), ()],
350         },
351         'tasks': {
352                 'add':
353                         [('timeline', 'name',), ('list_id', 'parse',)],
354                 'addTags':
355                         [('timeline', 'list_id', 'taskseries_id', 'task_id', 'tags'),
356                          ()],
357                 'complete':
358                         [('timeline', 'list_id', 'taskseries_id', 'task_id',), ()],
359                 'delete':
360                         [('timeline', 'list_id', 'taskseries_id', 'task_id'), ()],
361                 'getList':
362                         [(),
363                          ('list_id', 'filter', 'last_sync')],
364                 'movePriority':
365                         [('timeline', 'list_id', 'taskseries_id', 'task_id', 'direction'),
366                          ()],
367                 'moveTo':
368                         [('timeline', 'from_list_id', 'to_list_id', 'taskseries_id', 'task_id'),
369                          ()],
370                 'postpone':
371                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
372                          ()],
373                 'removeTags':
374                         [('timeline', 'list_id', 'taskseries_id', 'task_id', 'tags'),
375                          ()],
376                 'setDueDate':
377                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
378                          ('due', 'has_due_time', 'parse')],
379                 'setEstimate':
380                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
381                          ('estimate',)],
382                 'setLocation':
383                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
384                          ('location_id',)],
385                 'setName':
386                         [('timeline', 'list_id', 'taskseries_id', 'task_id', 'name'),
387                          ()],
388                 'setPriority':
389                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
390                          ('priority',)],
391                 'setRecurrence':
392                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
393                          ('repeat',)],
394                 'setTags':
395                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
396                          ('tags',)],
397                 'setURL':
398                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
399                          ('url',)],
400                 'uncomplete':
401                         [('timeline', 'list_id', 'taskseries_id', 'task_id'),
402                          ()],
403         },
404         'tasksNotes': {
405                 'add':
406                         [('timeline', 'list_id', 'taskseries_id', 'task_id', 'note_title', 'note_text'), ()],
407                 'delete':
408                         [('timeline', 'note_id'), ()],
409                 'edit':
410                         [('timeline', 'note_id', 'note_title', 'note_text'), ()],
411         },
412         'test': {
413                 'echo':
414                         [(), ()],
415                 'login':
416                         [(), ()],
417         },
418         'time': {
419                 'convert':
420                         [('to_timezone',), ('from_timezone', 'to_timezone', 'time')],
421                 'parse':
422                         [('text',), ('timezone', 'dateformat')],
423         },
424         'timelines': {
425                 'create':
426                         [(), ()],
427         },
428         'timezones': {
429                 'getList':
430                         [(), ()],
431         },
432         'transactions': {
433                 'undo':
434                         [('timeline', 'transaction_id'), ()],
435         },
436 }