<s> """<STR_LIT>""" <EOL> import json <EOL> from ckanapi . errors import ( CKANAPIError , NotAuthorized , NotFound , <EOL> ValidationError , SearchQueryError , SearchError , SearchIndexError , <EOL> ServerIncompatibleError ) <EOL> class ActionShortcut ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ckan ) : <EOL> self . _ckan = ckan <EOL> def __getattr__ ( self , name ) : <EOL> def action ( ** kwargs ) : <EOL> files = { } <EOL> for k , v in kwargs . items ( ) : <EOL> if is_file_like ( v ) : <EOL> files [ k ] = v <EOL> if files : <EOL> nonfiles = dict ( ( k , v ) for k , v in kwargs . items ( ) <EOL> if k not in files ) <EOL> return self . _ckan . call_action ( name , <EOL> data_dict = nonfiles , <EOL> files = files ) <EOL> return self . _ckan . call_action ( name , data_dict = kwargs ) <EOL> return action <EOL> def is_file_like ( v ) : <EOL> """<STR_LIT>""" <EOL> return hasattr ( v , '<STR_LIT>' ) or ( <EOL> isinstance ( v , tuple ) and len ( v ) >= <NUM_LIT:2> and hasattr ( v [ <NUM_LIT:1> ] , '<STR_LIT>' ) ) <EOL> def prepare_action ( action , data_dict = None , apikey = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> if not data_dict : <EOL> data_dict = { } <EOL> headers = { } <EOL> if files : <EOL> items = data_dict . items ( ) <EOL> data_dict = { } <EOL> for ( k , v ) in items : <EOL> if v is None : <EOL> continue <EOL> if isinstance ( v , ( int , float ) ) : <EOL> v = unicode ( v ) <EOL> data_dict [ k . encode ( '<STR_LIT:utf-8>' ) ] = v . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> data_dict = json . dumps ( data_dict ) . encode ( '<STR_LIT:ascii>' ) <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> if apikey : <EOL> apikey = str ( apikey ) <EOL> headers [ '<STR_LIT>' ] = apikey <EOL> headers [ '<STR_LIT>' ] = apikey <EOL> url = '<STR_LIT>' + action <EOL> return url , data_dict , headers <EOL> def reverse_apicontroller_action ( url , status , response ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> parsed = json . loads ( response ) <EOL> if parsed . get ( '<STR_LIT:success>' ) : <EOL> return parsed [ '<STR_LIT:result>' ] <EOL> if hasattr ( parsed , '<STR_LIT>' ) : <EOL> err = parsed . get ( '<STR_LIT:error>' , { } ) <EOL> else : <EOL> err = { } <EOL> except ( AttributeError , ValueError ) : <EOL> err = { } <EOL> if not isinstance ( err , dict ) : <EOL> raise ServerIncompatibleError ( repr ( [ url , status , response ] ) ) <EOL> etype = err . get ( '<STR_LIT>' ) <EOL> emessage = err . get ( '<STR_LIT:message>' , '<STR_LIT>' ) . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if etype == '<STR_LIT>' : <EOL> raise SearchQueryError ( emessage ) <EOL> elif etype == '<STR_LIT>' : <EOL> raise SearchError ( emessage ) <EOL> elif etype == '<STR_LIT>' : <EOL> raise SearchIndexError ( emessage ) <EOL> elif etype == '<STR_LIT>' : <EOL> raise ValidationError ( err ) <EOL> elif etype == '<STR_LIT>' : <EOL> raise NotFound ( emessage ) <EOL> elif etype == '<STR_LIT>' : <EOL> raise NotAuthorized ( err ) <EOL> raise CKANAPIError ( repr ( [ url , status , response ] ) ) </s>
