<commit_before># Copyright (C) 2013  <NAME> <<EMAIL>>
#
# This file is part of Debsources.
#
# Debsources 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/>.


from flask import render_template, redirect, url_for, request, safe_join, \
    jsonify
from flask.views import View

from app import app
from models_app import Package_app, Version_app, Location, Directory, \
    SourceFile, InvalidPackageOrVersionError, FileOrFolderNotFound
from modules.sourcecode import SourceCodeIterator
from forms import SearchForm

#import modules.tasks as tasks

@app.context_processor # variables needed by "base.html" skeleton
def skeleton_variables():
    with open(app.config['LAST_UPDATE_FILE']) as f:
        last_update = f.readline()
    
    return dict(packages_prefixes = Package_app.get_packages_prefixes(),
                searchform = SearchForm(),
                last_update=last_update)

### GENERAL VIEW HANDLING ###

class GeneralView(View):
    def __init__(self, render_func=jsonify, err_func=lambda *x: x):
        self.render_func = render_func
        self.err_func = err_func
    
    def dispatch_request(self, **kwargs):
        try:
            context = self.get_objects(**kwargs)
            return self.render_func(**context)
        except Http500Error as e:
            return self.err_func(e, http=500)
        except Http404Error as e:
            return self.err_func(e, http=404)


### EXCEPTIONS ###

class Http500Error(Exception): pass
class Http404Error(Exception): pass


### ERRORS ###

def deal_error(error, http=404, mode='html'):
    if http == 404:
        return deal_404_error(error, mode)
    elif http == 500:
        return deal_500_error(error, mode)
    else:
        raise Exception("Unimplemented HTTP error: %s" % str(http))

def deal_404_error(error, mode='html'):
    if mode == 'json':
        return jsonify(dict(error=404))
    else:
        return render_template('404.html'), 404

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

def deal_500_error(error, mode='html'):
    """ logs a 500 error and returns the correct template """
    app.logger.error(error)
    
    if mode == 'json':
        return jsonify(dict(error=500))
    else:
        return render_template('500.html'), 500

@app.errorhandler(500)
def server_error(e):
    return render_template('500.html'), 500

### PING ###

@app.route('/ping/')
def ping():
    try:
        a = Package_app.query.first().id
    except:
        return jsonify(dict(result="db error", http_status_code=500))
    return jsonify(dict(result="pong", http_status_code=200))

### INDEX, DOCUMENTATION ###

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

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

@app.route('/doc/url/')
def doc_url():
    return render_template('doc_url.html')
<commit_msg>update: /ping/ -> /api/ping/<commit_after># Copyright (C) 2013  <NAME> <<EMAIL>>
#
# This file is part of Debsources.
#
# Debsources 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/>.


from flask import render_template, redirect, url_for, request, safe_join, \
    jsonify
from flask.views import View

from app import app
from models_app import Package_app, Version_app, Location, Directory, \
    SourceFile, InvalidPackageOrVersionError, FileOrFolderNotFound
from modules.sourcecode import SourceCodeIterator
from forms import SearchForm

#import modules.tasks as tasks

@app.context_processor # variables needed by "base.html" skeleton
def skeleton_variables():
    with open(app.config['LAST_UPDATE_FILE']) as f:
        last_update = f.readline()
    
    return dict(packages_prefixes = Package_app.get_packages_prefixes(),
                searchform = SearchForm(),
                last_update=last_update)

### GENERAL VIEW HANDLING ###

class GeneralView(View):
    def __init__(self, render_func=jsonify, err_func=lambda *x: x):
        self.render_func = render_func
        self.err_func = err_func
    
    def dispatch_request(self, **kwargs):
        try:
            context = self.get_objects(**kwargs)
            return self.render_func(**context)
        except Http500Error as e:
            return self.err_func(e, http=500)
        except Http404Error as e:
            return self.err_func(e, http=404)


### EXCEPTIONS ###

class Http500Error(Exception): pass
class Http404Error(Exception): pass


### ERRORS ###

def deal_error(error, http=404, mode='html'):
    if http == 404:
        return deal_404_error(error, mode)
    elif http == 500:
        return deal_500_error(error, mode)
    else:
        raise Exception("Unimplemented HTTP error: %s" % str(http))

def deal_404_error(error, mode='html'):
    if mode == 'json':
        return jsonify(dict(error=404))
    else:
        return render_template('404.html'), 404

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

def deal_500_error(error, mode='html'):
    """ logs a 500 error and returns the correct template """
    app.logger.error(error)
    
    if mode == 'json':
        return jsonify(dict(error=500))
    else:
        return render_template('500.html'), 500

@app.errorhandler(500)
def server_error(e):
    return render_template('500.html'), 500

### PING ###

@app.route('/api/ping/')
def ping():
    try:
        a = Package_app.query.first().id
    except:
        return jsonify(dict(status="db error", http_status_code=500))
    return jsonify(dict(status="ok", http_status_code=200))

### INDEX, DOCUMENTATION ###

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

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

@app.route('/doc/url/')
def doc_url():
    return render_template('doc_url.html')

{'fraction_non_alphanumeric': 0.1119112775457295, 'fraction_numerical': 0.025637332565173556, 'mean_word_length': 3.6078301260783014, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3589095', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2049, 'n_words': 638}