def solve_hdd(): pm = ProcessManager.get_instance() if not sessions.check_session(): return {"text": "No ambiguities data available execute a full analysis first"}, 400 if not session['ambiguities']: queue = pm.get_queue(session['id']) if not queue: return {"text": "No ambiguities data available execute a full analysis first"}, 400 else: session['ambiguities'] = queue.get() payload = {} file_path = session['model'] if os.path.isfile(file_path): dis = Disambiguator.from_file(file_path) dis.remove_transitions(session['ambiguities']['dead']) dis.solve_hidden_deadlocks(session['ambiguities']['hidden']) pm.delete_queue(session['id']) graph = graphviz.Graph(dis.get_graph()) payload['mts'] = graph.get_mts() payload['text'] = "Removed hidden deadlocks and dead transitions" payload['graph'] = graph.get_graph() payload['edges'], payload['nodes'] = graph.get_graph_number() graph.draw_graph(session['graph']) return payload, 200 return {"text": 'File not found'}, 400
def get_graph(): message = """No graph data available, the graph may be too big render. You can render it locally by downloading the graph source code and use the following command: dot -Tsvg model.dot -o output.svg""" if 'src' in request.form and request.form['src']: graphviz.Graph(request.form['src']).draw_graph(session['graph']) if sessions.check_session(): if os.path.isfile(session['graph']): return {"source":os.path.join('static', 'tmp', os.path.basename(session['graph']))}, 200 return {"text":message}, 400
def upload_file(): """This function allows file uploading while checking if the provided file is valid and creating necessary folders if not present""" payload = {} dot = "" sessions.close_session() sessions.new_session() if not os.path.exists(config.UPLOAD_FOLDER): os.makedirs(config.UPLOAD_FOLDER) if not os.path.exists(os.path.dirname(session['output'])): os.makedirs(os.path.dirname(session['output'])) if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return {"text": 'No file part'}, 400 file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return {"text": 'No selected file'}, 400 if file: file_path = session['model'] file.save(file_path) if not puremagic.from_file(file_path) in ALLOWED_EXTENSIONS: os.remove(file_path) return {"text": "Incompatible file format"}, 400 if not is_fts(file_path): os.remove(file_path) return { "text": "The given file is not a FTS or contains errors" }, 400 with open(file_path, 'r') as source: dot = source.read() graph = graphviz.Graph(dot) payload['mts'] = graph.get_mts() graph.draw_graph(session['graph']) payload['graph'] = dot payload['edges'], payload['nodes'] = graph.get_graph_number() payload['text'] = "Model loaded" return payload, 200 else: return {"text": "Incompatible file format"}, 400 return {"text": "Invalid request"}, 400
def get_output(): pm = ProcessManager.get_instance() if not sessions.check_session(): sessions.delete_output_file() return {"text": '\nSession timed-out'}, 404 elif not 'id' in session or not pm.process_exists(session['id']): sessions.delete_output_file() return {"text": ''}, 404 else: if pm.is_alive(session['id']): with open(session['output']) as out: out.seek(session['position']) result = out.read(4096) session['position'] = out.tell() return {"text": result}, 206 else: with open(session['output']) as out: out.seek(session['position']) result = out.read() os.remove(session['output']) queue = pm.get_queue(session['id']) payload = {} payload['text'] = result graph = graphviz.Graph.from_file(session['model']) payload['edges'], payload['nodes'] = graph.get_graph_number() payload['mts'] = graph.get_mts() if (queue): tmp = queue.get()['ambiguities'] session['ambiguities'] = tmp payload['ambiguities'] = tmp ProcessManager.get_instance().delete_queue(session['id']) try: dis = Disambiguator.from_file(session['model']) dis.highlight_ambiguities(tmp['dead'], tmp['false'], tmp['hidden']) payload['graph'] = dis.get_graph() graphviz.Graph(dis.get_graph()).draw_graph( session['graph']) return payload, 200 except: return payload, 200 return payload, 200