#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 os
import shlex
import unittest
from argparse import Namespace
from unittest import mock

import pytest
from parameterized import parameterized

from airflow.security import kerberos
from airflow.security.kerberos import renew_from_kt
from tests.test_utils.config import conf_vars

KRB5_KTNAME = os.environ.get('KRB5_KTNAME')

@unittest.skipIf(KRB5_KTNAME is None, 'Skipping Kerberos API tests due to missing KRB5_KTNAME')
class TestKerberos(unittest.TestCase):
    def setUp(self):
        self.args = Namespace(
            keytab=KRB5_KTNAME, principal=None, pid=None, daemon=None, stdout=None, stderr=None, log_file=None
        )

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME})
    def test_renew_from_kt(self):
        """
        We expect no result, but a successful run. No more TypeError
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'include_ip'): ''})
    def test_renew_from_kt_include_ip_empty(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'include_ip'): 'False'})
    def test_renew_from_kt_include_ip_false(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'include_ip'): 'True'})
    def test_renew_from_kt_include_ip_true(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    # Validate forwardable kerberos option
    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'forwardable'): ''})
    def test_renew_from_kt_forwardable_empty(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'forwardable'): 'False'})
    def test_renew_from_kt_forwardable_false(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): KRB5_KTNAME, ('kerberos', 'forwardable'): 'True'})
    def test_renew_from_kt_forwardable_true(self):
        """
        We expect no result, but a successful run.
        """
        assert renew_from_kt(principal=self.args.principal, keytab=self.args.keytab) is None

    @conf_vars({('kerberos', 'keytab'): ''})
    def test_args_from_cli(self):
        """
        We expect no result, but a run with sys.exit(1) because keytab not exist.
        """
        with pytest.raises(SystemExit) as ctx:
            renew_from_kt(principal=self.args.principal, keytab=self.args.keytab)

            with self.assertLogs(kerberos.log) as log:
                assert (
                    f'kinit: krb5_init_creds_set_keytab: Failed to find airflow@LUPUS.GRIDDYNAMICS.NET in '
                    f'keytab FILE:{self.args.keytab} (unknown enctype)' in log.output
                )

            assert ctx.value.code == 1

class TestKerberosUnit(unittest.TestCase):
    @parameterized.expand(
        [
            (
                {('kerberos', 'reinit_frequency'): '42'},
                [
                    'kinit',
                    '-f',
                    '-a',
                    '-r',
                    '42m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
            ),
            (
                {('kerberos', 'forwardable'): 'True', ('kerberos', 'include_ip'): 'True'},
                [
                    'kinit',
                    '-f',
                    '-a',
                    '-r',
                    '3600m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
            ),
            (
                {('kerberos', 'forwardable'): 'False', ('kerberos', 'include_ip'): 'False'},
                [
                    'kinit',
                    '-F',
                    '-A',
                    '-r',
                    '3600m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
            ),
        ]
    )
    def test_renew_from_kt(self, kerberos_config, expected_cmd):
        with self.assertLogs(kerberos.log) as log_ctx, conf_vars(kerberos_config), mock.patch(
            'airflow.security.kerberos.subprocess'
        ) as mock_subprocess, mock.patch(
            'airflow.security.kerberos.NEED_KRB181_WORKAROUND', None
        ), mock.patch(
            'airflow.security.kerberos.open', mock.mock_open(read_data=b'X-CACHECONF:')
        ), mock.patch(
            'time.sleep', return_value=None
        ):
            mock_subprocess.Popen.return_value.__enter__.return_value.returncode = 0
            mock_subprocess.call.return_value = 0
            renew_from_kt(principal="test-principal", keytab="keytab")

        assert mock_subprocess.Popen.call_args[0][0] == expected_cmd

        expected_cmd_text = " ".join(shlex.quote(f) for f in expected_cmd)
        assert log_ctx.output == [
            f'INFO:airflow.security.kerberos:Re-initialising kerberos from keytab: {expected_cmd_text}',
            'INFO:airflow.security.kerberos:Renewing kerberos ticket to work around kerberos 1.8.1: '
            'kinit -c /tmp/airflow_krb5_ccache -R',
        ]

        assert mock_subprocess.mock_calls == [
            mock.call.Popen(
                expected_cmd,
                bufsize=-1,
                close_fds=True,
                stderr=mock_subprocess.PIPE,
                stdout=mock_subprocess.PIPE,
                universal_newlines=True,
            ),
            mock.call.Popen().__enter__(),
            mock.call.Popen().__enter__().wait(),
            mock.call.Popen().__exit__(None, None, None),
            mock.call.call(['kinit', '-c', '/tmp/airflow_krb5_ccache', '-R'], close_fds=True),
        ]

    @mock.patch('airflow.security.kerberos.subprocess')
    @mock.patch('airflow.security.kerberos.NEED_KRB181_WORKAROUND', None)
    @mock.patch('airflow.security.kerberos.open', mock.mock_open(read_data=b''))
    def test_renew_from_kt_without_workaround(self, mock_subprocess):
        mock_subprocess.Popen.return_value.__enter__.return_value.returncode = 0
        mock_subprocess.call.return_value = 0

        with self.assertLogs(kerberos.log) as log_ctx:
            renew_from_kt(principal="test-principal", keytab="keytab")

        assert log_ctx.output == [
            'INFO:airflow.security.kerberos:Re-initialising kerberos from keytab: '
            'kinit -f -a -r 3600m -k -t keytab -c /tmp/airflow_krb5_ccache test-principal'
        ]

        assert mock_subprocess.mock_calls == [
            mock.call.Popen(
                [
                    'kinit',
                    '-f',
                    '-a',
                    '-r',
                    '3600m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
                bufsize=-1,
                close_fds=True,
                stderr=mock_subprocess.PIPE,
                stdout=mock_subprocess.PIPE,
                universal_newlines=True,
            ),
            mock.call.Popen().__enter__(),
            mock.call.Popen().__enter__().wait(),
            mock.call.Popen().__exit__(None, None, None),
        ]

    @mock.patch('airflow.security.kerberos.subprocess')
    @mock.patch('airflow.security.kerberos.NEED_KRB181_WORKAROUND', None)
    def test_renew_from_kt_failed(self, mock_subprocess):
        mock_subp = mock_subprocess.Popen.return_value.__enter__.return_value
        mock_subp.returncode = 1
        mock_subp.stdout = mock.MagicMock(name="stdout", **{'readlines.return_value': ["STDOUT"]})
        mock_subp.stderr = mock.MagicMock(name="stderr", **{'readlines.return_value': ["STDERR"]})

        with self.assertLogs(kerberos.log) as log_ctx, self.assertRaises(SystemExit):
            renew_from_kt(principal="test-principal", keytab="keytab")

        assert log_ctx.output == [
            'INFO:airflow.security.kerberos:Re-initialising kerberos from keytab: '
            'kinit -f -a -r 3600m -k -t keytab -c /tmp/airflow_krb5_ccache test-principal',
            "ERROR:airflow.security.kerberos:Couldn't reinit from keytab! `kinit' exited with 1.\n"
            "STDOUT\n"
            "STDERR",
        ]

        assert mock_subprocess.mock_calls == [
            mock.call.Popen(
                [
                    'kinit',
                    '-f',
                    '-a',
                    '-r',
                    '3600m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
                bufsize=-1,
                close_fds=True,
                stderr=mock_subprocess.PIPE,
                stdout=mock_subprocess.PIPE,
                universal_newlines=True,
            ),
            mock.call.Popen().__enter__(),
            mock.call.Popen().__enter__().wait(),
            mock.call.Popen().__exit__(mock.ANY, mock.ANY, mock.ANY),
        ]

    @mock.patch('airflow.security.kerberos.subprocess')
    @mock.patch('airflow.security.kerberos.NEED_KRB181_WORKAROUND', None)
    @mock.patch('airflow.security.kerberos.open', mock.mock_open(read_data=b'X-CACHECONF:'))
    @mock.patch('airflow.security.kerberos.socket.getfqdn', return_value="HOST")
    @mock.patch('time.sleep', return_value=None)
    def test_renew_from_kt_failed_workaround(self, mock_sleep, mock_getfqdn, mock_subprocess):
        mock_subprocess.Popen.return_value.__enter__.return_value.returncode = 0
        mock_subprocess.call.return_value = 1

        with self.assertLogs(kerberos.log) as log_ctx, self.assertRaises(SystemExit):
            renew_from_kt(principal="test-principal", keytab="keytab")

        assert log_ctx.output == [
            'INFO:airflow.security.kerberos:Re-initialising kerberos from keytab: '
            'kinit -f -a -r 3600m -k -t keytab -c /tmp/airflow_krb5_ccache test-principal',
            'INFO:airflow.security.kerberos:Renewing kerberos ticket to work around kerberos 1.8.1: '
            'kinit -c /tmp/airflow_krb5_ccache -R',
            "ERROR:airflow.security.kerberos:Couldn't renew kerberos ticket in order to work around "
            "Kerberos 1.8.1 issue. Please check that the ticket for 'test-principal/HOST' is still "
            "renewable:\n"
            "  $ kinit -f -c /tmp/airflow_krb5_ccache\n"
            "If the 'renew until' date is the same as the 'valid starting' date, the ticket cannot be "
            "renewed. Please check your KDC configuration, and the ticket renewal policy (maxrenewlife) for "
            "the 'test-principal/HOST' and `krbtgt' principals.",
        ]

        assert mock_subprocess.mock_calls == [
            mock.call.Popen(
                [
                    'kinit',
                    '-f',
                    '-a',
                    '-r',
                    '3600m',
                    '-k',
                    '-t',
                    'keytab',
                    '-c',
                    '/tmp/airflow_krb5_ccache',
                    'test-principal',
                ],
                bufsize=-1,
                close_fds=True,
                stderr=mock_subprocess.PIPE,
                stdout=mock_subprocess.PIPE,
                universal_newlines=True,
            ),
            mock.call.Popen().__enter__(),
            mock.call.Popen().__enter__().wait(),
            mock.call.Popen().__exit__(None, None, None),
            mock.call.call(['kinit', '-c', '/tmp/airflow_krb5_ccache', '-R'], close_fds=True),
        ]

    def test_run_without_keytab(self):
        with self.assertLogs(kerberos.log) as log_ctx, self.assertRaises(SystemExit):
            kerberos.run(principal="test-principal", keytab=None)
        assert log_ctx.output == [
            'WARNING:airflow.security.kerberos:Keytab renewer not starting, no keytab configured'
        ]

    @mock.patch('airflow.security.kerberos.renew_from_kt')
    @mock.patch('time.sleep', return_value=None)
    def test_run(self, mock_sleep, mock_renew_from_kt):
        mock_renew_from_kt.side_effect = [1, 1, SystemExit(42)]
        with self.assertRaises(SystemExit):
            kerberos.run(principal="test-principal", keytab="/tmp/keytab")
        assert mock_renew_from_kt.mock_calls == [
            mock.call('test-principal', '/tmp/keytab'),
            mock.call('test-principal', '/tmp/keytab'),
            mock.call('test-principal', '/tmp/keytab'),
        ]


# -*- coding: utf-8 -*-
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.testing import z2

import rer.sitesearch

class RERSitesearchLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        # Load any other ZCML that is required for your tests.
        # The z3c.autoinclude feature is disabled in the Plone fixture base
        # layer.
        self.loadZCML(package=rer.sitesearch)

    def setUpPloneSite(self, portal):
        applyProfile(portal, 'rer.sitesearch:default')
        setRoles(portal, TEST_USER_ID, ['Member', 'Manager'])
        # create some example contents
        for i in range(0, 20):
            """
            create some documents
            """
            portal.invokeFactory(
                'Document',
                'my-page-{0}'.format(str(i)),
                title='My page plone {0}'.format(str(i)),
            )
        for i in range(0, 5):
            """
            create some news with a Subject
            """
            portal.invokeFactory(
                'News Item',
                'my-news-{0}'.format(str(i)),
                title='My news plone {0}'.format(str(i)),
                subject=i % 2 == 0 and ['apple'] or ['mango'],
            )
        for i in range(0, 5):
            """
            create some news with a Subject
            """
            portal.invokeFactory(
                'Event',
                'my-event-{0}'.format(str(i)),
                title='My event plone {0}'.format(str(i)),
                subject=['apple', 'mango'],
            )
        for i in range(0, 5):
            """
            create some documents with a Subject
            """
            portal.invokeFactory(
                'Document',
                'categorized-page-{0}'.format(str(i)),
                title='Categorized page plone {0}'.format(str(i)),
                text='spam chocolate ham eggs',
                subject=i % 2 == 0 and ['apple'] or ['kiwi'],
            )

RER_SITESEARCH_FIXTURE = RERSitesearchLayer()

RER_SITESEARCH_INTEGRATION_TESTING = IntegrationTesting(
    bases=(RER_SITESEARCH_FIXTURE,),
    name='RERSitesearchLayer:IntegrationTesting',
)

RER_SITESEARCH_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(RER_SITESEARCH_FIXTURE,), name='RERSitesearchLayer:FunctionalTesting'
)

RER_SITESEARCH_ACCEPTANCE_TESTING = FunctionalTesting(
    bases=(
        RER_SITESEARCH_FIXTURE,
        REMOTE_LIBRARY_BUNDLE_FIXTURE,
        z2.ZSERVER_FIXTURE,
    ),
    name='RERSitesearchLayer:AcceptanceTesting',
)


import cherrypy

LOG_LOCATION = r'D:\work\licenta\anomaly-detect\implementare\logs\detection.log'

def readLogs(log_location):
    res = []
    with open(log_location, "r") as log:
        for line in log:
            line = line.strip()
            res.append("<h4>" + line + "</h4>")

    return res

class HttpServer(object):
    @cherrypy.expose
    def logs(self):
        return readLogs(LOG_LOCATION)

if __name__ == '__main__':
    cherrypy.quickstart(HttpServer())


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json, pytest
from pathlib import Path

from gnpy.core.parameters import SimParams
from gnpy.core.science_utils import Simulation
from gnpy.core.elements import Fiber

TEST_DIR = Path(__file__).parent
DATA_DIR = TEST_DIR / 'data'

def test_sim_parameters():
    f = open(DATA_DIR / 'test_sim_params.json')
    j = json.load(f)
    sim_params = SimParams(**j)
    Simulation.set_params(sim_params)
    s1 = Simulation.get_simulation()
    assert s1.sim_params.raman_params.flag_raman
    s2 = Simulation.get_simulation()
    assert s2.sim_params.raman_params.flag_raman
    j['raman_parameters']['flag_raman'] = False
    sim_params = SimParams(**j)
    Simulation.set_params(sim_params)
    assert not s2.sim_params.raman_params.flag_raman
    assert not s1.sim_params.raman_params.flag_raman

if __name__ == '__main__':
    from logging import getLogger, basicConfig, INFO
    logger = getLogger(__name__)
    basicConfig(level=INFO)

    test_sim_parameters()

    print('\n')


from flask import Flask, render_template, request, redirect, session, flash #get_flashed_messages
from flask_bcrypt import Bcrypt
from mysqlconnection import connectToMySQL
from datetime import datetime
import re
import my_utils

app = Flask(__name__)
bcrypt = Bcrypt(app)
app.secret_key = 'keep private private wall'

dbname = 'user_pw'

EMAIL_REGEX = re.compile(r"^[a-zA-Z0-9._+-]+@[a-zA-Z0-9]+\.[a-zA-Z]+$")
NUM_REGEX = re.compile(r"^.*[0-9]+.*")
CAP_REGEX = re.compile(r"^.*[A-Z]+.*")

@app.route("/")
def mainpage():
    print("in '/', main page method")
    return render_template("index.html")

@app.route('/create_user', methods=['POST'])
def create():
    print("in create method: ", request.form)
    # include some logic to validate user input before adding them to the database!
    is_valid=True
    if not EMAIL_REGEX.match(request.form["email"]):
        flash("INVALID email address. Try again.", "email")
        is_valid=False
    if len(request.form['firstname'])<2:
        is_valid=False
        flash("Name needs to be at least 2 characters long.", "firstname")
    if len(request.form['lastname'])<2:
        is_valid=False
        flash("Name needs to be at least 2 characters long.", "lastname")
    if not my_utils.is_valid_password(request.form):
        is_valid=False
    if not is_valid:
        return redirect('/')

    # is email in db?
    mysql = connectToMySQL(dbname)
    email_unique = mysql.query_db("SELECT * FROM users WHERE email=%(email)s", request.form)
    print("is email in database?: ", email_unique)
    if len(email_unique) > 0:
        flash("This email is already taken.", "email")
        return redirect('/')
    else:
        pw_hash = bcrypt.generate_password_hash(request.form['password'])
        mysql = connectToMySQL(dbname)
        query = "INSERT INTO users (first_name, last_name, email, password, created_at, updated_at) VALUES (%(firstname)s, %(lastname)s,%(email)s,%(pw_hash)s, NOW(), NOW());"
        # put the pw_hash in our data dictionary, NOT the password the user provided
        data = {
            "firstname" : request.form['firstname'],
            "lastname" : request.form['lastname'],
            "email" : request.form['email'],
            "pw_hash" : pw_hash
        }
        user_id = mysql.query_db(query, data)
        print("id is:", user_id)

        # check query has an id value and is not () and False
        if user_id:
            session['id']= user_id
            session['firstname'] = request.form['firstname']
            print(" registration session: ", session)
            flash("Your registration was successful "+session['firstname']+ ". Welcome!", "registration")
            return redirect('/wall')
        else:
            flash("There was a problem in the registration process on our side.Try later.","registerproblem")
            return redirect("/")

@app.route('/login', methods=['POST'])
def login():
    print("IN login:", request.form)
    # check if email entered is in the database
    mysql = connectToMySQL(dbname)
    result = mysql.query_db("SELECT * FROM users WHERE  email= %(user_email)s;", request.form)
    print("result of email check, in login method: ", result)
    # if list is not empty - email exists
    if len(result) > 0:
        # does password in db matches password entered in form?
        if bcrypt.check_password_hash(result[0]['password'], request.form['user_password']):
            # if we get True, put user id in session
            session['id'] = result[0]['id']
            session['firstname'] = result[0]['first_name']
            print("login session", session)
            flash("Your login was successful "+session['firstname'], "login")

            return redirect('/wall')

    # if email is not in db or if the passwords don't match,
    flash("You could not be logged in", "notloggedin")
    return redirect("/")

@app.route('/wall')
def display_wall():
    #check if user is logged in
    if 'id' in session:
        # get data of all other users
        mysql = connectToMySQL(dbname)
        user_query = "SELECT id, first_name, last_name FROM users WHERE id != %(current_id)s;"
        data = { "current_id" : session['id']}
        all_users = mysql.query_db(user_query, data)

        # display messages to current user from others, MUST include messages.id in order to delete it.
        mysql = connectToMySQL(dbname)
        message_query = "SELECT messages.id as message_id, message, users.id as id, first_name FROM users JOIN messages ON users.id=messages.sender_id WHERE recipient_id = %(current_id)s ORDER BY messages.created_at DESC ;"
        all_user_messages = mysql.query_db(message_query, data)
        return render_template("wall.html", all_users=all_users, all_user_messages=all_user_messages)

    # if not logged in, direct user back to login page.
    else:
        flash("You need to be logged in.","mustlogin")
        return redirect('/')

@app.route('/sendmsg', methods=['POST'])
def send_message():
    #check if user is logged in
    if 'id' in session:
        print("IN sendmsg method: session is: ", session)
        print("req.for", request.form)
        mysql = connectToMySQL(dbname)
        insert_query = "INSERT INTO messages (message, sender_id, recipient_id, created_at)VALUES(%(message)s, %(sender_id)s, %(recipient_id)s, NOW());"
        msg_data = { "message": request.form['message'],
        "sender_id" : session['id'],
        "recipient_id" : request.form['recipient_id']}
        msg = mysql.query_db(insert_query, msg_data)
        return redirect('/wall')
    # if not logged in, direct user back to login page.
    else:
        flash("You need to be logged in.","mustlogin")
        return redirect('/')

@app.route('/deleteMsg/<id>')
def delete_msg(id):
    if 'id' not in session:
        flash("You need to be logged in.","mustlogin")
        redirect('/')

    mysql = connectToMySQL(dbname)
    delete_query = "DELETE FROM messages WHERE id = %(message_id)s;" #AND user.id= session['id]
    data = { "message_id" : id}
    mysql.query_db(delete_query, data)
    return redirect('/wall')

@app.route('/clear')
def clear_session():
    session.clear()
    print("session should be empty: ", session)
    flash("You are logged out.", "logout")
    return redirect('/')

if __name__ == "__main__":
    app.run(debug=True)


#from database_managment import db_connection
import pandas as pd
import numpy as np
import xlrd

def convert_adverse_data(list_value):
    new_list_1 = []
    final_list = []
    for blank in map(lambda x: np.nan if x == '练习'  else x, list_value):
        new_list_1.append(blank)
    for each in new_list_1:
        value=str(each).split('-')[0]
        value=str(value).split('+')[0]
        value = float(value.replace('阶测','').replace('左右',''))
        #value = str(value )
        final_list.append(float(value))
    return final_list

def Output_Judge( a, b, c, d ):
    sum = a+b+c+d
    if sum>=16:
        if ( sum/4 ) % 1 == 0.25 or ( sum/4 ) % 1 == 0.75 :
            return sum / 4 + 0.25
        elif ( sum/4 ) % 1 == 0.625 or ( sum/4 ) % 1 == 0.375:
            return sum // 4 + 0.5
        elif ( sum/4 ) % 1 == 0.125:
            return sum // 4
        elif ( sum/4 ) % 1 == 0.875:
            return sum // 4 + 1
        else:
            return sum / 4

def academic_result( grade ):
    if grade>38:
        return 9.0
    elif grade>36:
        return 8.5
    elif grade>34:
        return 8.0
    elif grade>32:
        return 7.5
    elif grade>29:
        return 7.0
    elif grade>26:
        return 6.5
    elif grade>22:
        return 6.0
    elif grade>19:
        return 5.5
    elif grade>15:
        return 5.0
    elif grade>12:
        return 4.5
    elif grade>9:
        return 4.0
    elif grade>5:
        return 3.5
    elif grade>3:
        return 3.0
    elif grade==3:
        return 2.5
    elif grade==2:
        return 2.0
    else:
        return 1.0

writing_path = r'/Users/xiaoking/Downloads/tp/分数统计 写作10.6.xlsx'

def convert_writing( path = writing_path ):
    name_list = xlrd.open_workbook( path ).sheet_names()
    name_list.remove( '模板' )

    content=pd.DataFrame(  )

    for each in name_list:
        content_temp = pd.read_excel( writing_path,sheet_name=each )
        content_temp['日期']=content_temp['日期'].fillna( method='pad' )
        content_temp.dropna( inplace=True )
        content_temp['姓名']=each
        content = content_temp.append( content )

    content['TA'] = convert_adverse_data(content['TA'].values)
    content['CC'] = convert_adverse_data(content['CC'].values)
    content['LR'] = convert_adverse_data(content['LR'].values)
    content['GRA'] = convert_adverse_data(content['GRA'].values)
    content['总分'] = convert_adverse_data(content['总分'].values)
    content.columns = [ 'test_date','task','type','ta','cc','lr','gra','total_input','student_name' ]
    total = []

    for i in range(len(content)):
            #print(writing_final.iat[i,0])
            score = Output_Judge(float(content.iat[i, 3]), float(content.iat[i, 4]), float(content.iat[i, 5]),
                                           float(content.iat[i, 6]))
            total.append(score)

    content['total_cal'] = total
    content.reset_index(drop=True,inplace=True)
    return content

def sql_writing(  ):
    content = convert_writing()
    from database_managment import db_connection
    con = db_connection.connect()
    #content.to_sql( 'writing_band_test',con,if_exists='replace',index=False )

    #content['task']=content['task'].astype(str)
    #content['type']=content['type'].astype(str)

    #content['student_name']=content['student_name'].astype(str)

    #print( content.dtypes )
    value = content.values
    sql = '''
    insert into writing_band ( test_date,task,type,ta,cc,lr,gra,total_input,student_name,total_cal ) values ( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s );
    '''
    cur = con.cursor()
    for each in value:
        cur.execute( sql,( str(each[0]),str(each[1]),str(each[2]),str(each[3]),str(each[4]),str(each[5]),str(each[6]),str(each[7]),str(each[8]),str(each[9]) ) )

    cur.close()
    con.commit()
    con.close()

speaking_path = r'/Users/xiaoking/Downloads/tp/分数统计 口语10.5.xlsx'
def convert_speaking(path = speaking_path):
    name_list = xlrd.open_workbook(path).sheet_names()
    #name_list.remove('模板')
    #print( name_list )
    temp = pd.DataFrame()
    for each in name_list:
        content = pd.read_excel( path,sheet_name=each )
        #print( content )
        #print( content.index )
        #print(content.columns )
        #print( content.loc['日期'].values.tolist() )
        try:
            content.columns = content.loc['日期'].values.tolist()
        except:
            print( each )
        content.drop( index = '日期',inplace=True )
        content['总分']=content['总分'].apply(str)
        #print( content.dtypes )
        #print(content['总分'].str.contains('练习'))
        content['总分'].where(~content['总分'].str.contains('练习'),'4.0',inplace=True)
        content['姓名'] = each
        temp = content.append( temp )
    temp.dropna( axis=1,how='all',inplace=True )
    temp['part1话题'].fillna('UNKONWN',inplace=True)
    temp['part2话题'].fillna('UNKONWN',inplace=True)
    temp.fillna('0.0',inplace=True)
    total = convert_adverse_data( temp['总分'] )
    temp['总分']=total
    temp['日期'] = temp.index
    return temp

def sql_spraking(  ):
    content = convert_speaking()
    from database_managment import db_connection
    con = db_connection.connect()

    value = content.values
    sql = '''
            insert into speaking_band ( test_date,part1,part2,fc,gra,lr,pro,total_input,student_name ) values ( %s,%s,%s,%s,%s,%s,%s,%s,%s );
            '''

    cur = con.cursor()
    for each in value:
        cur.execute(sql, (
            str(each[8]), str(each[4]), str(each[5]), str(each[0]), str(each[1]), str(each[2]), str(each[3]),
            str(each[6]),
            str(each[7])))

    cur.close()
    con.commit()
    con.close()

def convertion_R_L(  ):
    listening_path = r'/Users/xiaoking/Downloads/tp/错题整理 听力10.7.xlsx'

    name_list = xlrd.open_workbook(listening_path).sheet_names()
    temp = pd.DataFrame()
    for each in name_list:
        content = pd.read_excel( listening_path,sheet_name=each )
        content['姓名']=each
        content.dropna(how='any',inplace=True)
        temp = content.append(temp)
    readinging_path = r'/Users/xiaoking/Downloads/tp/错题整理 阅读10.7.xlsx'
    name_list = xlrd.open_workbook(readinging_path).sheet_names()
    for each in name_list:
        content = pd.read_excel( readinging_path,sheet_name=each )
        content['姓名']=each
        content.dropna(how='any',inplace=True)
        temp = content.append(temp)
    return temp

def sql_rl(  ):
    content = convertion_R_L()
    content.columns = ['A_01', 'A_10', 'A_11', 'A_12', 'A_13', 'A_14', 'A_15', 'A_16', 'A_17',
       'A_18', 'A_19', 'A_02', 'A_20', 'A_21', 'A_22', 'A_23', 'A_24', 'A_25',
       'A_26', 'A_27', 'A_28', 'A_29', 'A_03', 'A_30', 'A_31', 'A_32', 'A_33',
       'A_34', 'A_35', 'A_36', 'A_37', 'A_38', 'A_39', 'A_04', 'A_40', 'A_05',
       'A_06', 'A_07', 'A_08', 'A_09', '刷题材料', '姓名', '日期', '科目']
    sorted = content.columns.tolist()
    sorted.sort( )
    #print(sorted)
    content = content[sorted]
    content.reset_index(drop=True,inplace=True)
    result=[]
    for each in range(len(content)):
        temp = academic_result(content.iloc[each,:].drop( index=['刷题材料','姓名','日期','科目'] ).value_counts()['对'])
        result.append( temp )

    content['总分']=result
    content.replace( '对',True,inplace=True )
    content.replace('错',False,inplace=True )
    print( 'convertion succeeded\n' )

    from database_managment import db_connection

    con = db_connection.connect()

    value = content.values
    sql = '''
                    insert into correction_rl ( test_date,material,student_name,type,band,
                    a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,
                    a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,
                    a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,
                    a31,a32,a33,a34,a35,a36,a37,a38,a39,a40
                        ) values ( %s,%s,%s,%s,%s,
                        %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
                         %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
                          %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
                           %s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                    '''

    cur = con.cursor()
    for each in value:
        cur.execute(sql, ( str(each[-3]),str(each[-5]),str(each[-4]),str(each[-2]),str(each[-1]),str(each[0]),
str(each[1]),
str(each[2]),
str(each[3]),
str(each[4]),
str(each[5]),
str(each[6]),
str(each[7]),
str(each[8]),
str(each[9]),
str(each[10]),
str(each[11]),
str(each[12]),
str(each[13]),
str(each[14]),
str(each[15]),
str(each[16]),
str(each[17]),
str(each[18]),
str(each[19]),
str(each[20]),
str(each[21]),
str(each[22]),
str(each[23]),
str(each[24]),
str(each[25]),
str(each[26]),
str(each[27]),
str(each[28]),
str(each[29]),
str(each[30]),
str(each[31]),
str(each[32]),
str(each[33]),
str(each[34]),
str(each[35]),
str(each[36]),
str(each[37]),
str(each[38]),
str(each[39]) ))

    cur.close()
    con.commit()
    con.close()