<commit_before>#!/usr/bin/python
#------------------------------------------------------------
# Run me with (may need su privilege for logging):
#        gunicorn -w 4 -b 127.0.0.1:3031 pyxserver_wsgi:application
#------------------------------------------------------------

import json
from lxml import etree
from time import localtime, strftime 

import pyxserver
import logging


def do_GET(data):
    return "Hey, the time is %s" % strftime("%a, %d %b %Y %H:%M:%S", localtime())


def do_POST(data):
    # This server expects jobs to be pushed to it from the queue
    xpackage = json.loads(data)
    body  = xpackage['xqueue_body']
    files = xpackage['xqueue_files'] 

    # Delivery from the lms
    body = json.loads(body) 
    grader_payload = body['grader_payload']
    student_response = body['student_response']

    # Extract pyxserver-specific content from the grader_payload. Note that 
    #   external graders are free to define their payload format.
    grader_payload = etree.fromstring(grader_payload)
    tests = grader_payload.find('tests').text.strip()
    processor = grader_payload.find('processor').text

    award, message = pyxserver.run_code_sandbox(processor, student_response, tests)

    # "External grader" reply format
    correct = award == 'EXACT_ANS'
    points = 1 if correct else 0
    reply = { 'correct': correct,
              'score': points, 
              'msg': message }

    return json.dumps(reply)

# Entry point
def application(env, start_response):
    logging.basicConfig()

    # Handle request
    method = env['REQUEST_METHOD']
    data = env['wsgi.input'].read()

    print '-' * 60
    print method

    handlers = {'GET': do_GET,
                 'POST': do_POST,
                 }
    if method in handlers.keys():
        reply = handlers[method](data)
        print ' [*] reply:\n%s\n' % reply
        start_response('200 OK', [('Content-Type', 'text/html')])
        return reply
    else:
        start_response('404 Not Found', [('Content-Type', 'text/plain')])
        return ''<commit_msg>Wrap message in root element in order to form valid XML<commit_after>#!/usr/bin/python
#------------------------------------------------------------
# Run me with (may need su privilege for logging):
#        gunicorn -w 4 -b 127.0.0.1:3031 pyxserver_wsgi:application
#------------------------------------------------------------

import json
from lxml import etree
from time import localtime, strftime 

import pyxserver
import logging


def do_GET(data):
    return "Hey, the time is %s" % strftime("%a, %d %b %Y %H:%M:%S", localtime())


def do_POST(data):
    # This server expects jobs to be pushed to it from the queue
    xpackage = json.loads(data)
    body  = xpackage['xqueue_body']
    files = xpackage['xqueue_files'] 

    # Delivery from the lms
    body = json.loads(body) 
    grader_payload = body['grader_payload']
    student_response = body['student_response']

    # Extract pyxserver-specific content from the grader_payload. Note that 
    #   external graders are free to define their payload format.
    grader_payload = etree.fromstring(grader_payload)
    tests = grader_payload.find('tests').text.strip()
    processor = grader_payload.find('processor').text

    award, message = pyxserver.run_code_sandbox(processor, student_response, tests)

    # "External grader" reply format
    message = '<span>' + message + '</span>'
    correct = award == 'EXACT_ANS'
    points = 1 if correct else 0
    reply = { 'correct': correct,
              'score': points, 
              'msg': message }

    return json.dumps(reply)

# Entry point
def application(env, start_response):
    logging.basicConfig()

    # Handle request
    method = env['REQUEST_METHOD']
    data = env['wsgi.input'].read()

    print '-' * 60
    print method

    handlers = {'GET': do_GET,
                 'POST': do_POST,
                 }
    if method in handlers.keys():
        reply = handlers[method](data)
        print ' [*] reply:\n%s\n' % reply
        start_response('200 OK', [('Content-Type', 'text/html')])
        return reply
    else:
        start_response('404 Not Found', [('Content-Type', 'text/plain')])
        return ''
{'fraction_non_alphanumeric': 0.17019253624910863, 'fraction_numerical': 0.009983361064891847, 'mean_word_length': 2.363709032773781, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, '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': '4356215', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1201, 'n_words': 350}