<s> import base64 <EOL> import copy <EOL> import cStringIO <EOL> from pkg_resources import resource_filename <EOL> import flask <EOL> import flask . ext . restless <EOL> import flask . ext . sqlalchemy <EOL> from flask_bootstrap import Bootstrap <EOL> from kombu import Connection <EOL> from kombu . pools import producers <EOL> from oslo_config import cfg <EOL> from oslo_log import log <EOL> from PIL import Image <EOL> from sqlalchemy . dialects import mysql <EOL> from faafo import queues <EOL> from faafo import version <EOL> LOG = log . getLogger ( '<STR_LIT>' ) <EOL> CONF = cfg . CONF <EOL> api_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> ] <EOL> CONF . register_opts ( api_opts ) <EOL> log . register_options ( CONF ) <EOL> log . set_defaults ( ) <EOL> CONF ( project = '<STR_LIT>' , prog = '<STR_LIT>' , <EOL> default_config_files = [ '<STR_LIT>' ] , <EOL> version = version . version_info . version_string ( ) ) <EOL> log . setup ( CONF , '<STR_LIT>' , <EOL> version = version . version_info . version_string ( ) ) <EOL> template_path = resource_filename ( __name__ , "<STR_LIT>" ) <EOL> app = flask . Flask ( '<STR_LIT>' , template_folder = template_path ) <EOL> app . config [ '<STR_LIT>' ] = CONF . debug <EOL> app . config [ '<STR_LIT>' ] = CONF . database_url <EOL> db = flask . ext . sqlalchemy . SQLAlchemy ( app ) <EOL> Bootstrap ( app ) <EOL> def list_opts ( ) : <EOL> """<STR_LIT>""" <EOL> return [ ( None , copy . deepcopy ( api_opts ) ) ] <EOL> class Fractal ( db . Model ) : <EOL> uuid = db . Column ( db . String ( <NUM_LIT> ) , primary_key = True ) <EOL> checksum = db . Column ( db . String ( <NUM_LIT> ) , unique = True ) <EOL> url = db . Column ( db . String ( <NUM_LIT> ) , nullable = True ) <EOL> duration = db . Column ( db . Float ) <EOL> size = db . Column ( db . Integer , nullable = True ) <EOL> width = db . Column ( db . Integer , nullable = False ) <EOL> height = db . Column ( db . Integer , nullable = False ) <EOL> iterations = db . Column ( db . Integer , nullable = False ) <EOL> xa = db . Column ( db . Float , nullable = False ) <EOL> xb = db . Column ( db . Float , nullable = False ) <EOL> ya = db . Column ( db . Float , nullable = False ) <EOL> yb = db . Column ( db . Float , nullable = False ) <EOL> if CONF . database_url . startswith ( '<STR_LIT>' ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> image = db . Column ( mysql . MEDIUMBLOB , nullable = True ) <EOL> else : <EOL> image = db . Column ( db . LargeBinary , nullable = True ) <EOL> generated_by = db . Column ( db . String ( <NUM_LIT> ) , nullable = True ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . uuid <EOL> db . create_all ( ) <EOL> manager = flask . ext . restless . APIManager ( app , flask_sqlalchemy_db = db ) <EOL> connection = Connection ( CONF . transport_url ) <EOL> @ app . route ( '<STR_LIT:/>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def index ( page = <NUM_LIT:1> ) : <EOL> fractals = Fractal . query . filter ( <EOL> ( Fractal . checksum != None ) & ( Fractal . size != None ) ) . paginate ( <EOL> page , <NUM_LIT:5> , error_out = False ) <EOL> return flask . render_template ( '<STR_LIT>' , fractals = fractals ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def get_fractal ( fractalid ) : <EOL> fractal = Fractal . query . filter_by ( uuid = fractalid ) . first ( ) <EOL> if not fractal : <EOL> response = flask . jsonify ( { '<STR_LIT:code>' : <NUM_LIT> , <EOL> '<STR_LIT:message>' : '<STR_LIT>' } ) <EOL> response . status_code = <NUM_LIT> <EOL> else : <EOL> image_data = base64 . b64decode ( fractal . image ) <EOL> image = Image . open ( cStringIO . StringIO ( image_data ) ) <EOL> output = cStringIO . StringIO ( ) <EOL> image . save ( output , "<STR_LIT>" ) <EOL> image . seek ( <NUM_LIT:0> ) <EOL> response = flask . make_response ( output . getvalue ( ) ) <EOL> response . content_type = "<STR_LIT>" <EOL> return response <EOL> def generate_fractal ( ** kwargs ) : <EOL> with producers [ connection ] . acquire ( block = True ) as producer : <EOL> producer . publish ( kwargs [ '<STR_LIT:result>' ] , <EOL> serializer = '<STR_LIT>' , <EOL> exchange = queues . task_exchange , <EOL> declare = [ queues . task_exchange ] , <EOL> routing_key = '<STR_LIT>' ) <EOL> def main ( ) : <EOL> manager . create_api ( Fractal , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> postprocessors = { '<STR_LIT:POST>' : [ generate_fractal ] } , <EOL> exclude_columns = [ '<STR_LIT:image>' ] , <EOL> url_prefix = '<STR_LIT>' ) <EOL> app . run ( host = CONF . listen_address , port = CONF . bind_port ) </s>
