Changed everything to ASCII. I'm worried we'll lose the forest for the \u1f332
[twitter-api-cdsw] / tweepy / parsers.py
1 # Tweepy
2 # Copyright 2009-2010 Joshua Roesslein
3 # See LICENSE for details.
4
5 from tweepy.models import ModelFactory
6 from tweepy.utils import import_simplejson
7 from tweepy.error import TweepError
8
9
10 class Parser(object):
11
12     def parse(self, method, payload):
13         """
14         Parse the response payload and return the result.
15         Returns a tuple that contains the result data and the cursors
16         (or None if not present).
17         """
18         raise NotImplementedError
19
20     def parse_error(self, payload):
21         """
22         Parse the error message from payload.
23         If unable to parse the message, throw an exception
24         and default error message will be used.
25         """
26         raise NotImplementedError
27
28
29 class RawParser(Parser):
30
31     def __init__(self):
32         pass
33
34     def parse(self, method, payload):
35         return payload
36
37     def parse_error(self, payload):
38         return payload
39
40
41 class JSONParser(Parser):
42
43     payload_format = 'json'
44
45     def __init__(self):
46         self.json_lib = import_simplejson()
47
48     def parse(self, method, payload):
49         try:
50             json = self.json_lib.loads(payload)
51         except Exception as e:
52             raise TweepError('Failed to parse JSON payload: %s' % e)
53
54         needsCursors = method.parameters.has_key('cursor')
55         if needsCursors and isinstance(json, dict) and 'previous_cursor' in json and 'next_cursor' in json:
56             cursors = json['previous_cursor'], json['next_cursor']
57             return json, cursors
58         else:
59             return json
60
61     def parse_error(self, payload):
62         error = self.json_lib.loads(payload)
63         if error.has_key('error'):
64             return error['error']
65         else:
66             return error['errors']
67
68
69 class ModelParser(JSONParser):
70
71     def __init__(self, model_factory=None):
72         JSONParser.__init__(self)
73         self.model_factory = model_factory or ModelFactory
74
75     def parse(self, method, payload):
76         try:
77             if method.payload_type is None: return
78             model = getattr(self.model_factory, method.payload_type)
79         except AttributeError:
80             raise TweepError('No model for this payload type: %s' % method.payload_type)
81
82         json = JSONParser.parse(self, method, payload)
83         if isinstance(json, tuple):
84             json, cursors = json
85         else:
86             cursors = None
87
88         if method.payload_list:
89             result = model.parse_list(method.api, json)
90         else:
91             result = model.parse(method.api, json)
92
93         if cursors:
94             return result, cursors
95         else:
96             return result
97

Benjamin Mako Hill || Want to submit a patch?