<commit_before>import tornado.ioloop
import tornado.web
import base64
import logging
import os

import helper


class abgabe(object):
    def __init__(self,mailAddress,status,downloadLink):
        self.mailAddress = mailAddress
        self.status = status
        self.downloadLink = downloadLink

class requestHandlerWithAuth(tornado.web.RequestHandler):
    def _execute(self, transforms, *args, **kwargs):
        # executed before everything else.
        if httpBasicAuth(self,kwargs):
            return tornado.web.RequestHandler._execute(self, transforms, *args, **kwargs)
        return False

class tableHandler(requestHandlerWithAuth):
    def get(self):
        if helper.getConfigValue("settings")["savemode"] == "file":
            abgaben = []
            if os.path.exists("attachments"):
                for entry in os.listdir("attachments/"):
                   abgaben.append(abgabe(entry,"Bearbeitet","*"))

            self.render("table.html",abgaben=abgaben)

def main():
    helper.setupLogging()

    application = tornado.web.Application([
        (r"/",tableHandler),
    ])

    logging.debug("Server started on port %i.",helper.getConfigValue("login")["korrektur_server_port"])

    application.listen(helper.getConfigValue("login")["korrektur_server_port"])
    tornado.ioloop.IOLoop.instance().start()

def httpBasicAuth(self,*kwargs):
    # http://de.wikipedia.org/wiki/HTTP-Statuscode
    receivedAuth = self.request.headers.get("Authorization")

    if receivedAuth is not None:
        decodedAuth = base64.decodestring(receivedAuth[6:])
        username, password = decodedAuth.split(":", 2)
        password = helper.md5sum(password)
        korrektoren = helper.getConfigValue("korrektoren")
        if username not in korrektoren:
            logging.debug("Received nonexistent user '%s'."%username)
            return False
        if korrektoren[username] == password:
            return True
        logging.error("Failed login from '%s' with password '%s'."%(username,password))

    self.set_status(401)
    self.set_header("WWW-Authenticate", "Basic realm='netsec-Uebungssystem Korrektoranmeldung'")
    self._transforms = []
    self.write("401: Authentifizierung erforderlich.")
    self.finish()
    return False

if __name__ == "__main__":
    main()<commit_msg>Status IO, zip Downloads<commit_after>import tornado.ioloop
import tornado.web
import base64
import logging
import os

import helper

class requestHandlerWithAuth(tornado.web.RequestHandler):
    def _execute(self, transforms, *args, **kwargs):
        # executed before everything else.
        if httpBasicAuth(self,kwargs):
            return tornado.web.RequestHandler._execute(self, transforms, *args, **kwargs)
        return False

class tableHandler(requestHandlerWithAuth):
    def get(self, *args, **kwargs):
        if helper.getConfigValue("settings")["savemode"] == "file":
            abgaben = []
            if os.path.exists("attachments"):
                for entry in os.listdir("attachments/"):
                   abgaben.append(entry.lower())

            self.render("table.html",abgaben=abgaben)

class zipHandler(requestHandlerWithAuth):
    def get(self, *args, **kwargs):
        requestedFile = self.request.uri.replace("/zips/","/zips").replace("/zips","")

        if len(requestedFile) is 0:
            self.set_status(404)
            self.write("Zur&uuml;ck zur <a href=\"/\">&Uuml;bersicht</a>")
            self.finish()
            return

        self.write(requestedFile)

class statusHandler(requestHandlerWithAuth):
    def get(self, *args, **kwargs):
        uri = self.request.uri.replace("/status/","")
        if uri.count("/") == 1:
            student,status = uri.split("/")
        else:
            student = uri
            status = ""
        if not status:
            self.write(readStatus(student))
        else:
            writeStatus(student,status)

def main():
    helper.setupLogging()

    application = tornado.web.Application([
        (r"/",tableHandler),
        (r"/zips/.*",zipHandler),
        (r"/status/.*",statusHandler),
    ])

    logging.debug("Server started on port %i.",helper.getConfigValue("login")["korrektur_server_port"])

    application.listen(helper.getConfigValue("login")["korrektur_server_port"])
    tornado.ioloop.IOLoop.instance().start()

def httpBasicAuth(self,*kwargs):
    # http://de.wikipedia.org/wiki/HTTP-Statuscode
    receivedAuth = self.request.headers.get("Authorization")

    if receivedAuth is not None:
        decodedAuth = base64.decodestring(receivedAuth[6:])
        username, password = decodedAuth.split(":", 2)
        password = <PASSWORD>)
        korrektoren = helper.getConfigValue("korrektoren")
        if username not in korrektoren:
            logging.debug("Received nonexistent user '%s'."%username)
            return False
        if korrektoren[username] == password:
            return True
        logging.error("Failed login from '%s' with password '%s'."%(username,password))

    self.set_status(401)
    self.set_header("WWW-Authenticate", "Basic realm='netsec-Uebungssystem Korrektoranmeldung'")
    self._transforms = []
    self.write("401: Authentifizierung erforderlich.")
    self.finish()
    return False

def readStatus(student):
    student = student.lower()

    retdir = os.getcwd()

    if not os.path.exists("attachments"):
        return
    os.chdir("attachments")

    if not os.path.exists(student):
        return "Student ohne Abgabe"
    os.chdir(student)

    if not os.path.exists("korrekturstatus.txt"):
        return "Unbearbeitet"
    statusfile = open("korrekturstatus.txt","r")
    status = statusfile.read()
    statusfile.close()
    os.chdir(retdir)
    return status

def writeStatus(student,status):
    student = student.lower()
    status = status.lower()
    retdir = os.getcwd()

    if not os.path.exists("attachments"):
        return
    os.chdir("attachments")

    if not os.path.exists(student):
        return
    os.chdir(student)
    statusfile = open("korrekturstatus.txt","w")
    statusfile.write(status)
    statusfile.close()
    os.chdir(retdir)

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.13075804105382252, 'fraction_numerical': 0.004848876676903184, 'mean_word_length': 3.005177993527508, 'pattern_counts': {'":': 6, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '66851', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1841, 'n_words': 354}