<commit_before>from flask import Flask, request, Response, render_template
import sdv
import StringIO
import urllib2
import json

app = Flask(__name__, static_folder='public', static_url_path='')

@app.route('/validate', methods=['POST', 'GET'])
def validate_ws():
    post_data = request.get_data()
    url = request.args.get('url')

    if url:
        try:
            return wrap_json_results(urllib2.urlopen(url).read())
        except ValueError:
            return Response('{"result": "error", "message": "could not find xml"}\n', mimetype='application/json')
    elif len(post_data) > 0:
        return wrap_json_results(post_data)
    else:
        return Response('{"result": "error", "message": "could not find xml"}\n', mimetype='application/json')

@app.route('/', methods=['POST', 'GET'])
def root():
    if request.method == 'POST':
        url = request.form['url']
        xml = request.form['xml']

        if url and len(url) > 0:
            try:
                results = get_results(urllib2.urlopen(url).read())
            except ValueError:
                results = {"result": "error", "message": "could not open url"}
        elif xml and len(xml) > 0:
            try:
                results = get_results(xml)
            except sdv.errors.ValidationError:
                results = {"result": "error", "message": "not well-formed"}
        else:
            results = {"result": "error", "message": "could not find xml"}

        return render_template('index.html', results=results, url=url, xml=xml, json=json.dumps(results))

    else:
        return render_template('index.html', results=None)


def get_results(data):
    return {
        "xml": sdv.validate_xml(StringIO.StringIO(data)).as_dict(),
        "best_practices": sdv.validate_best_practices(StringIO.StringIO(data)).as_dict(),
        "result": "validated"
    }


def wrap_json_results(data):
    return Response(json.dumps(results.as_json()) + "\n", mimetype='application/json')

if __name__ == "__main__":
    app.run('localhost', 5000, True)<commit_msg>refactored for better error handling<commit_after>from flask import Flask, request, Response, render_template
import sdv
import StringIO
import urllib2
import json

app = Flask(__name__, static_folder='public', static_url_path='')

@app.route('/validate', methods=['POST', 'GET'])
def validate_ws():
    post_data = request.get_data()
    url = request.args.get('url')

    if url:
        results = validate_url(url)
    elif len(post_data) > 0:
        results = validate_xml(post_data)
    else:
        results = {"result": "error", "message": "could not find xml"}

    return Response(json.dumps(results),mimetype="application/json")

@app.route('/', methods=['POST', 'GET'])
def root():
    if request.method == 'POST':
        url = request.form['url']
        xml = request.form['xml']

        if url and len(url) > 0:
            results = validate_url(url)
        elif xml and len(xml) > 0:
            results = validate_xml(xml)
        else:
            results = {"result": "error", "message": "could not find xml"}

        return render_template('index.html', results=results, url=url, xml=xml, json=json.dumps(results))
    else:
        return render_template('index.html', results=None)


def validate_xml(data):
    try:
        return {
            "xml": sdv.validate_xml(StringIO.StringIO(data)).as_dict(),
            "best_practices": sdv.validate_best_practices(StringIO.StringIO(data)).as_dict(),
            "result": "validated"
        }
    except sdv.errors.ValidationError as e:
        return {"result": "error", "message": str(e)}

def validate_url(url):
    try:
        return validate_xml(urllib2.urlopen(url).read())
    except ValueError:
        return {"result": "error", "message": "could not open url"}

if __name__ == "__main__":
    app.run('localhost', 5000, True)
{'fraction_non_alphanumeric': 0.1483587490307573, 'fraction_numerical': 0.004910829671749806, 'mean_word_length': 3.077976817702845, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, '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': '5574935', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1172, 'n_words': 281}