<s> from tweepy1 . models import ModelFactory <EOL> from tweepy1 . utils import import_simplejson <EOL> from tweepy1 . error import TweepError <EOL> class Parser ( object ) : <EOL> def parse ( self , method , payload ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def parse_error ( self , payload ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class RawParser ( Parser ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def parse ( self , method , payload ) : <EOL> return payload <EOL> def parse_error ( self , payload ) : <EOL> return payload <EOL> class JSONParser ( Parser ) : <EOL> payload_format = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . json_lib = import_simplejson ( ) <EOL> def parse ( self , method , payload ) : <EOL> try : <EOL> json = self . json_lib . loads ( payload ) <EOL> except Exception , e : <EOL> raise TweepError ( '<STR_LIT>' % e ) <EOL> needsCursors = method . parameters . has_key ( '<STR_LIT>' ) <EOL> if needsCursors and isinstance ( json , dict ) and '<STR_LIT>' in json and '<STR_LIT>' in json : <EOL> cursors = json [ '<STR_LIT>' ] , json [ '<STR_LIT>' ] <EOL> return json , cursors <EOL> else : <EOL> return json <EOL> def parse_error ( self , payload ) : <EOL> error = self . json_lib . loads ( payload ) <EOL> if error . has_key ( '<STR_LIT:error>' ) : <EOL> return error [ '<STR_LIT:error>' ] <EOL> else : <EOL> return error [ '<STR_LIT>' ] <EOL> class ModelParser ( JSONParser ) : <EOL> def __init__ ( self , model_factory = None ) : <EOL> JSONParser . __init__ ( self ) <EOL> self . model_factory = model_factory or ModelFactory <EOL> def parse ( self , method , payload ) : <EOL> try : <EOL> if method . payload_type is None : return <EOL> model = getattr ( self . model_factory , method . payload_type ) <EOL> except AttributeError : <EOL> raise TweepError ( '<STR_LIT>' % method . payload_type ) <EOL> json = JSONParser . parse ( self , method , payload ) <EOL> if isinstance ( json , tuple ) : <EOL> json , cursors = json <EOL> else : <EOL> cursors = None <EOL> if method . payload_list : <EOL> result = model . parse_list ( method . api , json ) <EOL> else : <EOL> result = model . parse ( method . api , json ) <EOL> result . json = json <EOL> if cursors : <EOL> return result , cursors <EOL> else : <EOL> return result <EOL> class RawJsonParser ( JSONParser ) : <EOL> def parse ( self , method , payload ) : <EOL> json = JSONParser . parse ( self , method , payload ) <EOL> return json </s>
