<commit_before>#!/usr/bin/env python3

import argparse
import markdown
import sys
import os
import configparser
import json
import math
import logging
from datetime import datetime

from models import api
from models import errors

# from flipflop import WSGIServer
from flask import Flask
from flask import render_template
from flask import Markup
import pymysql

log = logging.getLogger('test')

app = Flask(__name__)
app._static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
app.register_blueprint(api.page)

config = configparser.ConfigParser()

parser = argparse.ArgumentParser(description='start the web app')
parser.add_argument('--config', default='config.ini')
parser.add_argument('--host', default='0.0.0.0')
parser.add_argument('--port', type=int, default=2325)
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--no-debug', dest='debug', action='store_false')
parser.set_defaults(debug=False)

args = parser.parse_args()

res = config.read(args.config)

if len(res) == 0:
    print('Errors reading config file at "{0}"'.format(args.config))
    sys.exit(0)

try:
    sqlconn = pymysql.connect(unix_socket=config['sql']['unix_socket'], user=config['sql']['user'], passwd=config['sql']['passwd'], db=config['sql']['db'], charset='utf8')
except pymysql.err.OperationalError as e:
    error_code, error_message = e.args
    if error_code == 1045:
        print('Access denied to database with user \'{0}\'. Review your config file.'.format(config['sql']['user']))
    elif error_code == 1044:
        print('Access denied to database \'{0}\' with user \'{1}\'. Make sure the database \'{0}\' exists and user \'{1}\' has full access to it.'.format(config['sql']['db'], config['sql']['user']))
    else:
        print(e)
    sys.exit(1)

modules = config['site'].get('modules', '').split()
cursor = sqlconn.cursor()
cursor.execute('SELECT COUNT(*) as `num_decks` FROM `tb_deck`')
ret = cursor.fetchone()
has_decks = ret[0] > 0

api.init(sqlconn)
errors.init(app)

def get_cursor():
    sqlconn.ping()
    return sqlconn.cursor(pymysql.cursors.DictCursor)


def get_normal_cursor():
    sqlconn.ping()
    return sqlconn.cursor()


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


@app.route('/commands')
def commands():
    cursor = get_cursor()
    query = 'SELECT * FROM `tb_commands` WHERE `enabled`=1'
    cursor.execute(query)
    custom_commands = []
    point_commands = []
    moderator_commands = []
    for command in cursor:
        action = json.loads(command['action'])
        command['aliases'] = ['!' + s for s in command['command'].split('|')]
        for alias in command['aliases']:
            alias = '!' + alias
        if action['type'] in ['say', 'me', 'whisper']:
            if command['description'] is None or 'Added by' in command['description']:
                command['description'] = action['message']

        command['arguments'] = []

        try:
            if command['description'] is not None:
                description = json.loads(command['description'])
                if 'description' in description:
                    command['description'] = description['description']
                if 'usage' in description:
                    if description['usage'] == 'whisper':
                        for x in range(0, len(command['aliases'])):
                            alias = command['aliases'][x]
                            command['aliases'][x] = '/w {0} {1}'.format(config['bot']['full_name'], alias)
                        for alias in command['aliases']:
                            command['aliases']
                    command['description'] = description['description']
                if 'arguments' in description:
                    command['arguments'] = description['arguments']
                if 'hidden' in description:
                    if description['hidden'] is True:
                        continue
        except ValueError:
            # Command description was not valid json
            pass
        except:
            pass

        if command['description']:
            try:
                command['description'] = Markup(markdown.markdown(command['description']))
            except:
                log.exception('Unhandled exception in markdown shit')
            if command['level'] > 100:
                moderator_commands.append(command)
            elif command['cost'] > 0:
                point_commands.append(command)
            elif not command['description'].startswith('Added by'):
                custom_commands.append(command)
    cursor.close()
    sqlconn.commit()
    try:
        return render_template('commands.html',
                custom_commands=custom_commands,
                point_commands=point_commands,
                moderator_commands=sorted(moderator_commands, key=lambda x: x['level']))
    except Exception:
        log.exception('Unhandled exception in commands() render_template')


@app.route('/decks')
def decks():
    cursor = get_cursor()
    cursor.execute('SELECT * FROM `tb_deck` ORDER BY `last_used` DESC, `first_used` DESC')
    decks = []
    cursor.close()
    sqlconn.commit()
    for deck in cursor:
        decks.append(deck)
    return render_template('decks.html',
            decks=decks)


@app.route('/user/<username>')
def user_profile(username):
    cursor = get_cursor()
    print(username)
    cursor.execute('SELECT *, `points` as `user_points`, (SELECT COUNT(*)+1 FROM `tb_user` WHERE `points` > `user_points` ORDER BY `username` ASC) as `rank` FROM `tb_user` WHERE `username`=%s', [username])
    user = cursor.fetchone()
    if user:
        return render_template('user.html',
                user=user)
    else:
        return render_template('no_user.html'), 404


@app.route('/points')
def points():
    cursor = get_cursor()
    cursor.execute('SELECT * FROM `tb_user` ORDER BY `points` DESC, `username` ASC LIMIT 30')
    top_30_users = cursor.fetchall()
    cursor.close()
    sqlconn.commit()
    return render_template('points.html',
            top_30_users=top_30_users)


@app.route('/debug')
def debug():<commit_msg>We now commit shit so the user page is actually updated<commit_after>#!/usr/bin/env python3

import argparse
import markdown
import sys
import os
import configparser
import json
import math
import logging
from datetime import datetime

from models import api
from models import errors

# from flipflop import WSGIServer
from flask import Flask
from flask import render_template
from flask import Markup
import pymysql

log = logging.getLogger('test')

app = Flask(__name__)
app._static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
app.register_blueprint(api.page)

config = configparser.ConfigParser()

parser = argparse.ArgumentParser(description='start the web app')
parser.add_argument('--config', default='config.ini')
parser.add_argument('--host', default='0.0.0.0')
parser.add_argument('--port', type=int, default=2325)
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--no-debug', dest='debug', action='store_false')
parser.set_defaults(debug=False)

args = parser.parse_args()

res = config.read(args.config)

if len(res) == 0:
    print('Errors reading config file at "{0}"'.format(args.config))
    sys.exit(0)

try:
    sqlconn = pymysql.connect(unix_socket=config['sql']['unix_socket'], user=config['sql']['user'], passwd=config['sql']['passwd'], db=config['sql']['db'], charset='utf8')
except pymysql.err.OperationalError as e:
    error_code, error_message = e.args
    if error_code == 1045:
        print('Access denied to database with user \'{0}\'. Review your config file.'.format(config['sql']['user']))
    elif error_code == 1044:
        print('Access denied to database \'{0}\' with user \'{1}\'. Make sure the database \'{0}\' exists and user \'{1}\' has full access to it.'.format(config['sql']['db'], config['sql']['user']))
    else:
        print(e)
    sys.exit(1)

modules = config['site'].get('modules', '').split()
cursor = sqlconn.cursor()
cursor.execute('SELECT COUNT(*) as `num_decks` FROM `tb_deck`')
ret = cursor.fetchone()
has_decks = ret[0] > 0

api.init(sqlconn)
errors.init(app)

def get_cursor():
    sqlconn.ping()
    return sqlconn.cursor(pymysql.cursors.DictCursor)


def get_normal_cursor():
    sqlconn.ping()
    return sqlconn.cursor()


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


@app.route('/commands')
def commands():
    cursor = get_cursor()
    query = 'SELECT * FROM `tb_commands` WHERE `enabled`=1'
    cursor.execute(query)
    custom_commands = []
    point_commands = []
    moderator_commands = []
    for command in cursor:
        action = json.loads(command['action'])
        command['aliases'] = ['!' + s for s in command['command'].split('|')]
        for alias in command['aliases']:
            alias = '!' + alias
        if action['type'] in ['say', 'me', 'whisper']:
            if command['description'] is None or 'Added by' in command['description']:
                command['description'] = action['message']

        command['arguments'] = []

        try:
            if command['description'] is not None:
                description = json.loads(command['description'])
                if 'description' in description:
                    command['description'] = description['description']
                if 'usage' in description:
                    if description['usage'] == 'whisper':
                        for x in range(0, len(command['aliases'])):
                            alias = command['aliases'][x]
                            command['aliases'][x] = '/w {0} {1}'.format(config['bot']['full_name'], alias)
                        for alias in command['aliases']:
                            command['aliases']
                    command['description'] = description['description']
                if 'arguments' in description:
                    command['arguments'] = description['arguments']
                if 'hidden' in description:
                    if description['hidden'] is True:
                        continue
        except ValueError:
            # Command description was not valid json
            pass
        except:
            pass

        if command['description']:
            try:
                command['description'] = Markup(markdown.markdown(command['description']))
            except:
                log.exception('Unhandled exception in markdown shit')
            if command['level'] > 100:
                moderator_commands.append(command)
            elif command['cost'] > 0:
                point_commands.append(command)
            elif not command['description'].startswith('Added by'):
                custom_commands.append(command)
    cursor.close()
    sqlconn.commit()
    try:
        return render_template('commands.html',
                custom_commands=custom_commands,
                point_commands=point_commands,
                moderator_commands=sorted(moderator_commands, key=lambda x: x['level']))
    except Exception:
        log.exception('Unhandled exception in commands() render_template')


@app.route('/decks')
def decks():
    cursor = get_cursor()
    cursor.execute('SELECT * FROM `tb_deck` ORDER BY `last_used` DESC, `first_used` DESC')
    decks = []
    cursor.close()
    sqlconn.commit()
    for deck in cursor:
        decks.append(deck)
    return render_template('decks.html',
            decks=decks)


@app.route('/user/<username>')
def user_profile(username):
    cursor = get_cursor()
    print(username)
    cursor.execute('SELECT *, `points` as `user_points`, (SELECT COUNT(*)+1 FROM `tb_user` WHERE `points` > `user_points` ORDER BY `username` ASC) as `rank` FROM `tb_user` WHERE `username`=%s', [username])
    user = cursor.fetchone()
    cursor.close()
    sqlconn.commit()
    if user:
        return render_template('user.html',
                user=user)
    else:
        return render_template('no_user.html'), 404


@app.route('/points')
def points():
    cursor = get_cursor()
    cursor.execute('SELECT * FROM `tb_user` ORDER BY `points` DESC, `username` ASC LIMIT 30')
    top_30_users = cursor.fetchall()
    cursor.close()
    sqlconn.commit()
    return render_template('points.html',
            top_30_users=top_30_users)


@app.route('/debug')
def debug():
{'fraction_non_alphanumeric': 0.14106683804627249, 'fraction_numerical': 0.007872750642673522, 'mean_word_length': 2.8649487736727726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685589', 'n_tokens_mistral': 3648, 'n_tokens_neox': 3509, 'n_words': 871}