# Copyright 2015 The Meson development team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import flask

from mesonwrap import wrapdb
from wrapweb.app import APP
from wrapweb import jsonstatus


def get_query_db():
    db = getattr(flask.g, '_query_database', None)
    if db is None:
        dbdir = APP.config['DB_DIRECTORY']
        db = flask.g._query_database = wrapdb.WrapDatabase(dbdir)
    return db


def get_projectlist():
    querydb = get_query_db()
    return jsonstatus.ok(projects=querydb.name_search(''))


@APP.route('/v1/query/byname/<project>', methods=['GET'])
def name_query(project):
    querydb = get_query_db()
    return jsonstatus.ok(projects=querydb.name_search(project))


@APP.route('/v1/query/get_latest/<project>', methods=['GET'])
def get_latest(project):
    querydb = get_query_db()
    matches = querydb.get_versions(project, latest=True)
    if len(matches) == 0:
        return jsonstatus.error(500, 'No such project')
    latest = matches[0]
    return jsonstatus.ok(branch=latest[0], revision=latest[1])


@APP.route('/v1/projects', defaults={'project': None})
@APP.route('/v1/projects/<project>')
def get_project_info(project):
    if project is None:
        return get_projectlist()
    querydb = get_query_db()
    matches = querydb.get_versions(project)
    if len(matches) == 0:
        return jsonstatus.error(500, 'No such project')
    versions = [{'branch': i[0], 'revision': i[1]} for i in matches]
    return jsonstatus.ok(versions=versions)


@APP.route('/v1/projects/<project>/<branch>/<int:revision>/get_wrap')
@APP.route('/v1/projects/<project>/<branch>/<int:revision>/get_zip')
def get_wrap(project, branch, revision):
    querydb = get_query_db()
    revision = revision
    if flask.request.path.endswith('/get_wrap'):
        result = querydb.get_wrap(project, branch, revision)
        mtype = 'text/plain'
        fname = ''
    else:
        result = querydb.get_zip(project, branch, revision)
        mtype = 'application/zip'
        fname = '%s-%s-%d-wrap.zip' % (project, branch, revision)
    if result is None:
        return jsonstatus.error(500, 'No such entry')
    else:
        resp = flask.make_response(result)
        resp.mimetype = mtype
        if fname:
            resp.headers['Content-Disposition'] = (
                'attachment; filename=%s' % fname)
        return resp

@@ -2058,16 +2058,310 @@
 _wrap')%0A
+def get_wrap(project, branch, revision):%0A    querydb = get_query_db()%0A    result = querydb.get_wrap(project, branch, revision)%0A    if result is None:%0A        return jsonstatus.error(500, 'No such entry')%0A    resp = flask.make_response(result)%0A    resp.mimetype = 'text/plain'%0A    return resp%0A%0A%0A
 @APP.rou
@@ -2421,35 +2421,34 @@
 t_zip')%0Adef get_
-wra
+zi
 p(project, branc
@@ -2522,304 +2522,32 @@
     
-if flask.request.path.endswith('/get_wrap'):%0A        result = querydb.get_wrap(project, branch, revision)%0A        mtype = 'text/plain'%0A        fname = ''%0A    else:%0A        result = querydb.get_zip(project, branch, revision)%0A        mtype = 'application/zip'%0A        fname = '%25s-%25s-%25d-wrap.zip' %25 
+result = querydb.get_zip
 (pro
@@ -2651,22 +2651,8 @@
 y')%0A
-    else:%0A    
     
@@ -2686,20 +2686,16 @@
 result)%0A
-    
     resp
@@ -2710,40 +2710,26 @@
 e = 
-mtype%0A        if fname:%0A        
+'application/zip'%0A
     
@@ -2768,24 +2768,16 @@
 n'%5D = (%0A
-        
         
@@ -2804,23 +2804,64 @@
 e=%25s
-' %25 fname)%0A    
+-%25s-%25d-wrap.zip' %25%0A        (project, branch, revision))%0A
     

Use separate functions for get_wrap and get_zip endpoints