<commit_before>from flask import Flask, render_template, request, abort, Response
from flask_bootstrap import Bootstrap
from flask_appconfig import AppConfig
from elasticsearch import Elasticsearch
from flask.ext.babel import Babel
from presets import PresetManager
import logging

from libreantdb import DB

def initLoggers():
    streamHandler = logging.StreamHandler()
    streamHandler.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    streamHandler.setFormatter(formatter)

    loggers = [logging.getLogger('webant')]
    for logger in loggers:
        logger.addHandler(streamHandler)

def create_app(configfile=None):
    initLoggers()
    app = Flask("webant")
    app.config['BOOTSTRAP_SERVE_LOCAL'] = True
    AppConfig(app, configfile)
    Bootstrap(app)
    babel = Babel(app)

    if 'SECRET_KEY' not in app.config:
        # TODO remove me
        app.config['SECRET_KEY'] = 'asjdkasjdlkasdjlksajsdlsajdlsajd'

    if 'PRESET_PATHS' not in app.config:
        app.config['PRESET_PATHS'] = []
    else:
        app.config['PRESET_PATHS'] = app.config['PRESET_PATHS'].split(':')
    presetManager = PresetManager(app.config['PRESET_PATHS'])

    app._db = None

    def get_db():
        if app._db is None:
            db = DB(Elasticsearch())
            db.setup_db()
            # deferring assignment is meant to avoid that we _first_ cache the
            # DB object, then the setup_db() fails. This will let us with a
            # non-setupped DB
            app._db = db
        return app._db

    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/search')
    def search():
        query = request.args.get('q', None)
        if query is None:
            abort(400, "No query given")
        res = get_db().user_search(query)['hits']['hits']
        books = []
        for b in res:
            src = b['_source']
            src['_id'] = b['_id']
            books.append(src)
        format = request.args.get('format', 'html')
        if format == 'html':
            return render_template('search.html', books=books, query=query)
        if format == 'opensearch':
            return Response(render_template('opens.xml',
                                            books=books, query=query),
                            mimetype='text/xml')

        abort(400, "Wrong format")

    @app.route('/description.xml')
    def description():
        return Response(render_template('opens_desc.xml'),
                        mimetype='text/xml')

    @app.route('/view/<bookid>')
    def view_book(bookid):
        b = get_db().get_book_by_id(bookid)
        similar = get_db().mlt(bookid)['hits']['hits'][:10]
        return render_template('details.html',
                               book=b['_source'], bookid=bookid,
                               similar=similar)

    @app.route('/download/<bookid>/<fname>')
    def download_book(bookid, fname):
        raise NotImplementedError()

    @babel.localeselector
    def get_locale():
     return request.accept_languages.best_match(['en','it','sq'])   

    return app

def main():
    create_app().run(debug=True, host='0.0.0.0')

if __name__ == '__main__':
    main()<commit_msg>[ webant ] added function to retrieve client requested format<commit_after>from flask import Flask, render_template, request, abort, Response
from flask_bootstrap import Bootstrap
from flask_appconfig import AppConfig
from elasticsearch import Elasticsearch
from flask.ext.babel import Babel
from presets import PresetManager
import logging

from libreantdb import DB

def initLoggers():
    streamHandler = logging.StreamHandler()
    streamHandler.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    streamHandler.setFormatter(formatter)

    loggers = [logging.getLogger('webant')]
    for logger in loggers:
        logger.addHandler(streamHandler)

def create_app(configfile=None):
    initLoggers()
    app = Flask("webant")
    app.config['BOOTSTRAP_SERVE_LOCAL'] = True
    AppConfig(app, configfile)
    Bootstrap(app)
    babel = Babel(app)

    if 'SECRET_KEY' not in app.config:
        # TODO remove me
        app.config['SECRET_KEY'] = '<KEY>'

    if 'PRESET_PATHS' not in app.config:
        app.config['PRESET_PATHS'] = []
    else:
        app.config['PRESET_PATHS'] = app.config['PRESET_PATHS'].split(':')
    presetManager = PresetManager(app.config['PRESET_PATHS'])

    app._db = None

    def get_db():
        if app._db is None:
            db = DB(Elasticsearch())
            db.setup_db()
            # deferring assignment is meant to avoid that we _first_ cache the
            # DB object, then the setup_db() fails. This will let us with a
            # non-setupped DB
            app._db = db
        return app._db

    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/search')
    def search():
        query = request.args.get('q', None)
        if query is None:
            abort(400, "No query given")
        res = get_db().user_search(query)['hits']['hits']
        books = []
        for b in res:
            src = b['_source']
            src['_id'] = b['_id']
            books.append(src)
        format = request.args.get('format', 'html')
        if format == 'html':
            return render_template('search.html', books=books, query=query)
        if format == 'opensearch':
            return Response(render_template('opens.xml',
                                            books=books, query=query),
                            mimetype='text/xml')

        abort(400, "Wrong format")

    @app.route('/description.xml')
    def description():
        return Response(render_template('opens_desc.xml'),
                        mimetype='text/xml')

    @app.route('/view/<bookid>')
    def view_book(bookid):
        b = get_db().get_book_by_id(bookid)
        similar = get_db().mlt(bookid)['hits']['hits'][:10]
        return render_template('details.html',
                               book=b['_source'], bookid=bookid,
                               similar=similar)

    @app.route('/download/<bookid>/<fname>')
    def download_book(bookid, fname):
        raise NotImplementedError()

    @babel.localeselector
    def get_locale():
     return request.accept_languages.best_match(['en','it','sq'])   

    def reuqestedFormat(acceptedFormat):
        """Return the response format requested by client

        Client could specify requested format using:
        (options are processed in this order)
            - `format` field in http request
            - `Accept` header in http request
        Example:
            chooseFormat(['text/html','application/json'])
        Args:
            acceptedFormat: list containing all the accepted format
        Returns:
            string: the user requested mime-type (if supported)
        Raises:
            ValueError: if user request a mime-type not supported
        """
        if 'format' in request.args:
            fieldFormat = request.args.get('format')
            if fieldFormat not in acceptedFormat:
                raise ValueError("requested format not supported")
            return fieldFormat
        else:
            return request.accept_mimetypes.best_match(acceptedFormat)

    return app

def main():
    create_app().run(debug=True, host='0.0.0.0')

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.11954787234042553, 'fraction_numerical': 0.0031914893617021275, 'mean_word_length': 2.384788478847885, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5854001', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2178, 'n_words': 518}