<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# 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
import json
import os
from pprint import pprint

import server.auth
import server.db.api as api
import server.db.models

from eve import Eve
from eve_sqlalchemy import SQL
from eve_sqlalchemy.validation import ValidatorSQL
from flask import abort
from sqlalchemy.sql import text

from dci_databrowser import dci_databrowser


def site_map():
    for rule in app.url_map.iter_rules():
        pprint(rule)


def load_docs(app):
    try:
        from eve_docs import eve_docs
        from flask.ext.bootstrap import Bootstrap
        Bootstrap(app)
        app.register_blueprint(eve_docs, url_prefix='/docs')
    except ImportError:
        print("Failed to load eve_docs.")


def set_real_owner(resource, items):
    """Hack to allow the 'admin' user to change the team_id."""
    if flask.request.authorization.username != 'admin':
        return
    # NOTE(Gonéri): the fields returned by flask.request.get_json() are
    # already mangled by the Role Based Access Control.
    request_fields = json.loads(flask.request.data.decode('utf-8'))
    if "team_id" in request_fields:
        items[0]['team_id'] = request_fields['team_id']


def init_app(db_uri=None):

    if not db_uri:
        db_uri = os.environ.get(
            'OPENSHIFT_POSTGRESQL_DB_URL',
            'postgresql://boa:boa@127.0.0.1:5432/dci_control_server')
    dci_model = server.db.models.DCIModel(db_uri)
    my_settings = {
        'SQLALCHEMY_DATABASE_URI': db_uri,
        'LAST_UPDATED': 'updated_at',
        'DATE_CREATED': 'created_at',
        'ID_FIELD': 'id',
        # 94ecbcfe-a9a6-7913-13a8-1ef1d50d9817
        'ITEM_URL': 'regex("[-a-z0-9]{8}-[-a-z0-9]{4}-'
                    '[-a-z0-9]{4}-[-a-z0-9]{4}-[-a-z0-9]{12}")',
        'ITEM_LOOKUP_FIELD': 'id',
        'ETAG': 'etag',
        'DEBUG': True,
        'URL_PREFIX': 'api',
        'X_DOMAINS': '*',
        'X_HEADERS': 'Authorization',
        'DOMAIN': dci_model.generate_eve_domain_configuration(),
        # The following two lines will output the SQL statements
        # executed by SQLAlchemy. Useful while debugging and in
        # development. Turned off by default
        # --------
        'SQLALCHEMY_ECHO': False,
        'SQLALCHEMY_RECORD_QUERIES': False,
    }
    my_basicauth = server.auth.DCIBasicAuth(dci_model)
    app = Eve(settings=my_settings, validator=ValidatorSQL,
              data=SQL, auth=my_basicauth)
    db = app.data.driver
    dci_model.metadata.bind = dci_model.engine
    db.Model = dci_model.base

    def pick_jobs(documents):
        session = dci_model.get_session()
        query = text(
            """
    SELECT
        testversions.id
    FROM
        testversions, remotecis<commit_msg>accept '.' in the name of an item<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# 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
import json
import os
from pprint import pprint

import server.auth
import server.db.api as api
import server.db.models

from eve import Eve
from eve_sqlalchemy import SQL
from eve_sqlalchemy.validation import ValidatorSQL
from flask import abort
from sqlalchemy.sql import text

from dci_databrowser import dci_databrowser


def site_map():
    for rule in app.url_map.iter_rules():
        pprint(rule)


def load_docs(app):
    try:
        from eve_docs import eve_docs
        from flask.ext.bootstrap import Bootstrap
        Bootstrap(app)
        app.register_blueprint(eve_docs, url_prefix='/docs')
    except ImportError:
        print("Failed to load eve_docs.")


def set_real_owner(resource, items):
    """Hack to allow the 'admin' user to change the team_id."""
    if flask.request.authorization.username != 'admin':
        return
    # NOTE(Gonéri): the fields returned by flask.request.get_json() are
    # already mangled by the Role Based Access Control.
    request_fields = json.loads(flask.request.data.decode('utf-8'))
    if "team_id" in request_fields:
        items[0]['team_id'] = request_fields['team_id']


def init_app(db_uri=None):

    if not db_uri:
        db_uri = os.environ.get(
            'OPENSHIFT_POSTGRESQL_DB_URL',
            'postgresql://boa:boa@127.0.0.1:5432/dci_control_server')
    dci_model = server.db.models.DCIModel(db_uri)
    my_settings = {
        'SQLALCHEMY_DATABASE_URI': db_uri,
        'LAST_UPDATED': 'updated_at',
        'DATE_CREATED': 'created_at',
        'ID_FIELD': 'id',
        # 94ecbcfe-a9a6-7913-13a8-1ef1d50d9817
        'ITEM_URL': 'regex("[\.-a-z0-9]{8}-[-a-z0-9]{4}-'
                    '[-a-z0-9]{4}-[-a-z0-9]{4}-[-a-z0-9]{12}")',
        'ITEM_LOOKUP_FIELD': 'id',
        'ETAG': 'etag',
        'DEBUG': True,
        'URL_PREFIX': 'api',
        'X_DOMAINS': '*',
        'X_HEADERS': 'Authorization',
        'DOMAIN': dci_model.generate_eve_domain_configuration(),
        # The following two lines will output the SQL statements
        # executed by SQLAlchemy. Useful while debugging and in
        # development. Turned off by default
        # --------
        'SQLALCHEMY_ECHO': False,
        'SQLALCHEMY_RECORD_QUERIES': False,
    }
    my_basicauth = server.auth.DCIBasicAuth(dci_model)
    app = Eve(settings=my_settings, validator=ValidatorSQL,
              data=SQL, auth=my_basicauth)
    db = app.data.driver
    dci_model.metadata.bind = dci_model.engine
    db.Model = dci_model.base

    def pick_jobs(documents):
        session = dci_model.get_session()
        query = text(
            """
    SELECT
        testversions.id
    FROM
        testversions, remotecis
{'fraction_non_alphanumeric': 0.11059563005088296, 'fraction_numerical': 0.016761448668063453, 'mean_word_length': 2.924251321197886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655578', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2131, 'n_words': 571}