<s> import json <EOL> from urlparse import urljoin <EOL> import pybcs <EOL> class BCS ( object ) : <EOL> def __init__ ( self , app = None ) : <EOL> self . app = app <EOL> if app is not None : <EOL> self . init_app ( app ) <EOL> def init_app ( self , app ) : <EOL> self . _host = app . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> access_key = app . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> secret_key = app . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _bucket_name = app . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> bcs = pybcs . BCS ( self . _host , access_key , secret_key ) <EOL> self . _bucket = bcs . bucket ( self . _bucket_name ) <EOL> def save ( self , data , filename , allow_referers = [ ] ) : <EOL> filename = unicode ( filename ) <EOL> if not filename . startswith ( '<STR_LIT:/>' ) : <EOL> filename = '<STR_LIT:/>' + filename <EOL> obj = self . _bucket . object ( filename ) <EOL> ret = obj . put ( data ) <EOL> if isinstance ( allow_referers , list ) and allow_referers : <EOL> object_policy = { <EOL> "<STR_LIT>" : <EOL> [ <EOL> { <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ self . _bucket_name + filename ] , <EOL> "<STR_LIT:user>" : [ "<STR_LIT:*>" ] , <EOL> "<STR_LIT>" : allow_referers <EOL> } <EOL> ] <EOL> } <EOL> obj . set_acl ( json . dumps ( object_policy ) ) <EOL> else : <EOL> obj . make_public ( ) <EOL> return ret <EOL> def delete ( self , filename ) : <EOL> filename = unicode ( filename ) <EOL> if not filename . startswith ( '<STR_LIT:/>' ) : <EOL> filename = '<STR_LIT:/>' + filename <EOL> obj = self . _bucket . object ( filename ) <EOL> return obj . delete ( ) <EOL> def url ( self , filename ) : <EOL> filename = unicode ( filename ) <EOL> if not filename . startswith ( '<STR_LIT:/>' ) : <EOL> filename = '<STR_LIT:/>' + filename <EOL> base_url = '<STR_LIT>' + self . _host <EOL> return urljoin ( base_url , self . _bucket_name + filename ) </s>
