<commit_before># coding: utf-8

# Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io

from __future__ import absolute_import, print_function, division
from flask import current_app, url_for, request
import flask_restful
from flask_restful import marshal_with, marshal, reqparse, inputs, abort

import sqlalchemy
from validate_email import validate_email
from datetime import datetime
from tyr.tyr_user_event import TyrUserEvent
from tyr.tyr_end_point_event import EndPointEventMessage, TyrEventsRabbitMq
from tyr.helper import load_instance_config
import logging
import os
import shutil
import json
from jsonschema import validate, ValidationError
from tyr.formats import poi_type_conf_format, parse_error, equipments_provider_format
from navitiacommon.default_traveler_profile_params import (
    default_traveler_profile_params,
    acceptable_traveler_types,
)
from navitiacommon import models, utils
from navitiacommon.models import db
from navitiacommon.parser_args_type import CoordFormat, PositiveFloat, BooleanType, OptionValue, geojson_argument
from functools import wraps
from tyr.validations import datetime_format
from tyr.tasks import (
    create_autocomplete_depot,
    remove_autocomplete_depot,
    import_autocomplete,
    cities,
    cosmogony2cities,
    COSMOGONY_REGEXP,
)
from tyr.helper import get_instance_logger, save_in_tmp
from tyr.fields import *
from werkzeug.exceptions import BadRequest

import werkzeug

__ALL__ = ['Api', 'Instance', 'User', 'Key']


class Api(flask_restful.Resource):
    def __init__(self):
        pass

    def get(self):
        return marshal(models.Api.query.all(), api_fields)


class Index(flask_restful.Resource):
    def get(self):
        return {'jobs': {'href': url_for('jobs', _external=True)}}


class Job(flask_restful.Resource):
    @marshal_with(jobs_fields)
    def get(self, instance_name=None):
        query = models.Job.query
        if instance_name:
            query = query.join(models.Instance)
            query = query.filter(models.Instance.name == instance_name)
        return {'jobs': query.order_by(models.Job.created_at.desc()).limit(30)}

    def post(self, instance_name):
        instance = models.Instance.query_existing().filter_by(name=instance_name).first_or_404()

        if not request.files:
            return {'message': 'the Data file is missing'}, 400
        content = request.files['file']
        logger = get_instance_logger(instance)
        logger.info('content received: %s', content)

        instance = load_instance_config(instance_name)
        if not os.path.exists(instance.source_directory):
            return {'error': 'input folder unavailable'}, 500

        full_file_name = os.path.join(os.path.realpath(instance.source_directory), content.filename)
        content.save(full_file_name + ".tmp")
        shutil.move(full_file_name + ".tmp", full_file_name)

        return {'message': 'OK'}, 200
<commit_msg>upgrade Tyr endpoints '/' & '/api'<commit_after># coding: utf-8

# Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io

from __future__ import absolute_import, print_function, division
from flask import current_app, url_for, request
import flask_restful
from flask_restful import marshal_with, marshal, reqparse, inputs, abort

import sqlalchemy
from validate_email import validate_email
from datetime import datetime
from tyr.tyr_user_event import TyrUserEvent
from tyr.tyr_end_point_event import EndPointEventMessage, TyrEventsRabbitMq
from tyr.helper import load_instance_config
import logging
import os
import shutil
import json
from jsonschema import validate, ValidationError
from tyr.formats import poi_type_conf_format, parse_error, equipments_provider_format
from navitiacommon.default_traveler_profile_params import (
    default_traveler_profile_params,
    acceptable_traveler_types,
)
from navitiacommon import models, utils
from navitiacommon.models import db
from navitiacommon.parser_args_type import CoordFormat, PositiveFloat, BooleanType, OptionValue, geojson_argument
from functools import wraps
from tyr.validations import datetime_format
from tyr.tasks import (
    create_autocomplete_depot,
    remove_autocomplete_depot,
    import_autocomplete,
    cities,
    cosmogony2cities,
    COSMOGONY_REGEXP,
)
from tyr import api
from tyr.helper import get_instance_logger, save_in_tmp
from tyr.fields import *
from werkzeug.exceptions import BadRequest
import werkzeug


class Api(flask_restful.Resource):
    def __init__(self):
        pass

    def get(self):
        def check_db():
            tyr_db = sqlalchemy.create_engine(current_app.config['SQLALCHEMY_DATABASE_URI'])
            try:
                tyr_db.connect()
                result = tyr_db.execute("SELECT version_num FROM alembic_version")
                for row in result:
                    return row['version_num']
            except Exception as e:
                logging.exception("Tyr db not reachable : {}".format(e.message))
                return None

        return {'db version': check_db(), 'api': marshal(models.Api.query.all(), api_fields)}


class Index(flask_restful.Resource):
    def get(self):
        response = {}
        for endpoint in api.endpoints:
            try:
                response[endpoint] = {'href': url_for(endpoint, _external=True)}
            except werkzeug.routing.BuildError:
                logging.warning('Could not build url for endpoint \'{}\' '.format(endpoint))
        return response


class Job(flask_restful.Resource):
    @marshal_with(jobs_fields)
    def get(self, instance_name=None):
        query = models.Job.query
        if instance_name:
            query = query.join(models.Instance)
            query = query.filter(models.Instance.name == instance_name)
        return {'jobs': query.order_by(models.Job.created_at.desc()).limit(30)}

    def post(self, instance_name):
        instance = models.Instance.query_existing().filter_by(name=instance_name).first_or_404()

        if not request.files:
            return {'message': 'the Data file is missing'}, 400
        content = request.files['file']
        logger = get_instance_logger(instance)
        logger.info('content received: %s', content)

        instance = load_instance_config(instance_name)
        if not os.path.exists(instance.source_directory):
            return {'error': 'input folder unavailable'}, 500

        full_file_name = os.path.join(os.path.realpath(instance.source_directory), content.filename)
        content.save(full_file_name + ".tmp")
        shutil.move(full_file_name + ".tmp", full_file_name)

        return {'message': 'OK'}, 200

{'fraction_non_alphanumeric': 0.0803190294315884, 'fraction_numerical': 0.0056167153448663226, 'mean_word_length': 3.838586956521739, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, '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': '5257727', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2494, 'n_words': 812}