<commit_before>import logging
import os
import sys
import threading
import json
import time

import flask
from flask import Flask, render_template, request
from flask_socketio import SocketIO
from flask_cors import CORS

from opentrons.instruments import Pipette
from opentrons import robot
from opentrons.containers import placeable
from opentrons.util import trace
from opentrons.util.vector import VectorEncoder

sys.path.insert(0, os.path.abspath('..'))  # NOQA
from server import helpers
from server.process_manager import run_once


TEMPLATES_FOLDER = os.path.join(helpers.get_frozen_root() or '', 'templates')
STATIC_FOLDER = os.path.join(helpers.get_frozen_root() or '', 'static')
BACKGROUND_TASKS = {}

app = Flask(__name__,
            static_folder=STATIC_FOLDER,
            template_folder=TEMPLATES_FOLDER
            )


CORS(app)
app.jinja_env.autoescape = False
app.config['ALLOWED_EXTENSIONS'] = set(['json', 'py'])
socketio = SocketIO(app, async_mode='gevent')

filename = "x"
last_modified = "y"


def notify(info):
    s = json.dumps(info, cls=VectorEncoder)
    socketio.emit('event', json.loads(s))


trace.EventBroker.get_instance().add(notify)


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


def get_protocol_locals():
    from opentrons import robot, containers, instruments  # NOQA
    return locals()


def load_python(stream, filename):
    global robot
    code = helpers.convert_byte_stream_to_str(stream)
    api_response = {'errors': [], 'warnings': []}

    robot.reset()

    patched_robot, restore_patched_robot = (
        helpers.get_upload_proof_robot(robot)
    )
    try:
        exec(code, globals(), get_protocol_locals())
        robot = restore_patched_robot()
        robot.simulate()
        if len(robot._commands) == 0:
            error = (
                "This protocol does not contain any commands for the robot."
            )
            api_response['errors'] = error
    except Exception as e:
        app.logger.error(e)
        api_response['errors'] = ['Failed to load protocol: ' + str(e)]
    finally:
        robot = restore_patched_robot()

    api_response['warnings'] = robot.get_warnings() or []

    return api_response


@app.route("/upload", methods=["POST"])
def upload():
    global filename
    global last_modified

    file = request.files.get('file')
    filename = file.filename
    last_modified = request.form.get('lastModified')<commit_msg>specifying line number if syntax error<commit_after>import logging
import os
import sys
import threading
import json
import time

import flask
import traceback
from flask import Flask, render_template, request
from flask_socketio import SocketIO
from flask_cors import CORS

from opentrons.instruments import Pipette
from opentrons import robot
from opentrons.containers import placeable
from opentrons.util import trace
from opentrons.util.vector import VectorEncoder

sys.path.insert(0, os.path.abspath('..'))  # NOQA
from server import helpers
from server.process_manager import run_once


TEMPLATES_FOLDER = os.path.join(helpers.get_frozen_root() or '', 'templates')
STATIC_FOLDER = os.path.join(helpers.get_frozen_root() or '', 'static')
BACKGROUND_TASKS = {}

app = Flask(__name__,
            static_folder=STATIC_FOLDER,
            template_folder=TEMPLATES_FOLDER
            )


CORS(app)
app.jinja_env.autoescape = False
app.config['ALLOWED_EXTENSIONS'] = set(['json', 'py'])
socketio = SocketIO(app, async_mode='gevent')

filename = "x"
last_modified = "y"


def notify(info):
    s = json.dumps(info, cls=VectorEncoder)
    socketio.emit('event', json.loads(s))


trace.EventBroker.get_instance().add(notify)


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


def get_protocol_locals():
    from opentrons import robot, containers, instruments  # NOQA
    return locals()


def load_python(stream, filename):
    global robot
    code = helpers.convert_byte_stream_to_str(stream)
    api_response = {'errors': [], 'warnings': []}

    robot.reset()

    patched_robot, restore_patched_robot = (
        helpers.get_upload_proof_robot(robot)
    )
    try:
        try:
            exec(code, globals(), get_protocol_locals())
        except Exception as e:
            tb = e.__traceback__
            stack_list = traceback.extract_tb(tb)
            _, line, name, text = stack_list[1]
            raise Exception(
                'Error in protocol file line {} : {}\n{}'.format(
                    line,
                    str(e),
                    text
                )
            )

        robot = restore_patched_robot()
        robot.simulate()
        if len(robot._commands) == 0:
            error = (
                "This protocol does not contain any commands for the robot."
            )
            api_response['errors'] = error
    except Exception as e:
        app.logger.error(e)
        api_response['errors'] = [str(e)]
    finally:
        robot = restore_patched_robot()

    api_response['warnings'] = robot.get_warnings() or []

    return api_response


@app.route("/upload", methods=["POST"])
def upload():
    global filename
    global last_modified

    file = request.files.get('file')
    filename = file.filename
    last_modified = request.form.get('lastModified')
{'fraction_non_alphanumeric': 0.10743957703927492, 'fraction_numerical': 0.0009441087613293052, 'mean_word_length': 3.1939825811559777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1147423', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1626, 'n_words': 384}