<s> import json , re , sys <EOL> from os . path import basename <EOL> import urllib <EOL> import cloudinary <EOL> import socket <EOL> from cloudinary import utils <EOL> from cloudinary . api import Error <EOL> from cloudinary . poster . encode import multipart_encode <EOL> from cloudinary . poster . streaminghttp import register_openers <EOL> from cloudinary . compat import urllib2 , BytesIO , string_types , urlencode , to_bytes , to_string , PY3 , HTTPError <EOL> _initialized = False <EOL> def upload ( file , ** options ) : <EOL> params = utils . build_upload_params ( ** options ) <EOL> return call_api ( "<STR_LIT>" , params , file = file , ** options ) <EOL> def unsigned_upload ( file , upload_preset , ** options ) : <EOL> return upload ( file , upload_preset = upload_preset , unsigned = True , ** options ) <EOL> def upload_image ( file , ** options ) : <EOL> result = upload ( file , ** options ) <EOL> return cloudinary . CloudinaryImage ( result [ "<STR_LIT>" ] , version = str ( result [ "<STR_LIT:version>" ] ) , <EOL> format = result . get ( "<STR_LIT>" ) , metadata = result ) <EOL> def upload_large ( file , ** options ) : <EOL> """<STR_LIT>""" <EOL> with open ( file , '<STR_LIT:rb>' ) as file_io : <EOL> upload = upload_id = None <EOL> index = <NUM_LIT:1> <EOL> public_id = options . get ( "<STR_LIT>" ) <EOL> chunk = file_io . read ( <NUM_LIT> ) <EOL> while ( chunk ) : <EOL> chunk_io = BytesIO ( chunk ) <EOL> chunk_io . name = basename ( file ) <EOL> chunk = file_io . read ( <NUM_LIT> ) <EOL> upload = upload_large_part ( chunk_io , public_id = public_id , <EOL> upload_id = upload_id , part_number = index , final = chunk == "<STR_LIT>" , ** options ) <EOL> upload_id = upload . get ( "<STR_LIT>" ) <EOL> public_id = upload . get ( "<STR_LIT>" ) <EOL> index += <NUM_LIT:1> <EOL> return upload <EOL> def upload_large_part ( file , ** options ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT:type>" : options . get ( "<STR_LIT:type>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) and "<STR_LIT:U+002C>" . join ( utils . build_array ( options [ "<STR_LIT>" ] ) ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , resource_type = "<STR_LIT>" , file = file , ** options ) <EOL> def destroy ( public_id , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT:type>" : options . get ( "<STR_LIT:type>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : public_id <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def rename ( from_public_id , to_public_id , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT:type>" : options . get ( "<STR_LIT:type>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : from_public_id , <EOL> "<STR_LIT>" : to_public_id <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def explicit ( public_id , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT:type>" : options . get ( "<STR_LIT:type>" ) , <EOL> "<STR_LIT>" : public_id , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : utils . build_custom_headers ( options . get ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : utils . build_eager ( options . get ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) and "<STR_LIT:U+002C>" . join ( utils . build_array ( options [ "<STR_LIT>" ] ) ) , <EOL> "<STR_LIT>" : utils . encode_double_array ( options . get ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : utils . encode_double_array ( options . get ( "<STR_LIT>" ) ) } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def generate_sprite ( tag , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT>" : tag , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : utils . generate_transformation_string ( fetch_format = options . get ( "<STR_LIT>" ) , ** options ) [ <NUM_LIT:0> ] <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def multi ( tag , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT>" : tag , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : utils . generate_transformation_string ( ** options ) [ <NUM_LIT:0> ] <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def explode ( public_id , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT>" : public_id , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : utils . generate_transformation_string ( ** options ) [ <NUM_LIT:0> ] <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> def add_tag ( tag , public_ids = [ ] , ** options ) : <EOL> exclusive = options . pop ( "<STR_LIT>" , None ) <EOL> command = "<STR_LIT>" if exclusive else "<STR_LIT>" <EOL> return call_tags_api ( tag , command , public_ids , ** options ) <EOL> def remove_tag ( tag , public_ids = [ ] , ** options ) : <EOL> return call_tags_api ( tag , "<STR_LIT>" , public_ids , ** options ) <EOL> def replace_tag ( tag , public_ids = [ ] , ** options ) : <EOL> return call_tags_api ( tag , "<STR_LIT:replace>" , public_ids , ** options ) <EOL> def call_tags_api ( tag , command , public_ids = [ ] , ** options ) : <EOL> params = { <EOL> "<STR_LIT>" : utils . now ( ) , <EOL> "<STR_LIT>" : tag , <EOL> "<STR_LIT>" : utils . build_array ( public_ids ) , <EOL> "<STR_LIT>" : command , <EOL> "<STR_LIT:type>" : options . get ( "<STR_LIT:type>" ) <EOL> } <EOL> return call_api ( "<STR_LIT>" , params , ** options ) <EOL> TEXT_PARAMS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def text ( text , ** options ) : <EOL> params = { "<STR_LIT>" : utils . now ( ) , "<STR_LIT:text>" : text } <EOL> for key in TEXT_PARAMS : <EOL> params [ key ] = options . get ( key ) <EOL> return call_api ( "<STR_LIT:text>" , params , ** options ) <EOL> def call_api ( action , params , ** options ) : <EOL> try : <EOL> file_io = None <EOL> return_error = options . get ( "<STR_LIT>" ) <EOL> if options . get ( "<STR_LIT>" ) : <EOL> params = utils . cleanup_params ( params ) <EOL> else : <EOL> params = utils . sign_request ( params , options ) <EOL> param_list = [ ] <EOL> for k , v in params . items ( ) : <EOL> if isinstance ( v , list ) : <EOL> for vv in v : <EOL> param_list . append ( ( k + "<STR_LIT>" , vv ) ) <EOL> elif v : <EOL> param_list . append ( ( k , v ) ) <EOL> api_url = utils . cloudinary_api_url ( action , ** options ) <EOL> global _initialized <EOL> if not _initialized : <EOL> _initialized = True <EOL> register_openers ( ) <EOL> datagen = [ ] <EOL> headers = { } <EOL> if "<STR_LIT:file>" in options : <EOL> file = options [ "<STR_LIT:file>" ] <EOL> if not isinstance ( file , string_types ) : <EOL> datagen , headers = multipart_encode ( { '<STR_LIT:file>' : file } ) <EOL> elif not re . match ( r'<STR_LIT>' , file ) : <EOL> file_io = open ( file , "<STR_LIT:rb>" ) <EOL> datagen , headers = multipart_encode ( { '<STR_LIT:file>' : file_io } ) <EOL> else : <EOL> param_list . append ( ( "<STR_LIT:file>" , file ) ) <EOL> if _is_gae ( ) : <EOL> datagen = "<STR_LIT>" . join ( datagen ) <EOL> request = urllib2 . Request ( api_url + "<STR_LIT:?>" + urlencode ( param_list ) , datagen , headers ) <EOL> request . add_header ( "<STR_LIT>" , cloudinary . USER_AGENT ) <EOL> kw = { } <EOL> if '<STR_LIT>' in options : <EOL> kw [ '<STR_LIT>' ] = options [ '<STR_LIT>' ] <EOL> code = <NUM_LIT:200> <EOL> try : <EOL> response = urllib2 . urlopen ( request , ** kw ) . read ( ) <EOL> except HTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if not e . code in [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ] : <EOL> raise Error ( "<STR_LIT>" % ( e . code , e . read ( ) ) ) <EOL> code = e . code <EOL> response = e . read ( ) <EOL> except socket . error : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise Error ( "<STR_LIT>" % str ( e ) ) <EOL> try : <EOL> result = json . loads ( to_string ( response ) ) <EOL> except Exception : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise Error ( "<STR_LIT>" , code , response , e ) <EOL> if "<STR_LIT:error>" in result : <EOL> if return_error : <EOL> result [ "<STR_LIT:error>" ] [ "<STR_LIT>" ] = code <EOL> else : <EOL> raise Error ( result [ "<STR_LIT:error>" ] [ "<STR_LIT:message>" ] ) <EOL> return result <EOL> finally : <EOL> if file_io : file_io . close ( ) <EOL> def _is_gae ( ) : <EOL> if PY3 : <EOL> return False <EOL> else : <EOL> import httplib <EOL> return '<STR_LIT>' in str ( httplib . HTTP ) </s>
