bema/func/libxml_backend #20
@@ -8,15 +8,34 @@ import Parser
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
app = Flask(__name__)
 | 
					app = Flask(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def process_xml(request, type):
 | 
					
 | 
				
			||||||
 | 
					def process_xml(request: request, type: str) -> str:
 | 
				
			||||||
 | 
					    """Function to process 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param request: Received request
 | 
				
			||||||
 | 
					    :type request: request
 | 
				
			||||||
 | 
					    :param type: Type of needed processing: xsd, xslt or xpath
 | 
				
			||||||
 | 
					    :type type: str
 | 
				
			||||||
 | 
					    :raises ValueError: is raised when type is different than those provided above
 | 
				
			||||||
 | 
					    :return: response JSON converted to string
 | 
				
			||||||
 | 
					    :rtype: str
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
    start = time.time_ns()
 | 
					    start = time.time_ns()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    response = dict()
 | 
					    response = dict()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        request_data = json.loads(request.get_data(as_text=True))
 | 
					        request_data = json.loads(request.get_data(as_text=True))
 | 
				
			||||||
        xml = request_data['data']
 | 
					        data = request_data['data']
 | 
				
			||||||
        xsd = request_data['process']
 | 
					        process = request_data['process']
 | 
				
			||||||
        response['result'] = Parser.xsd(xml, xsd)
 | 
					        if (type == "xsd"):
 | 
				
			||||||
 | 
					            response['result'] = Parser.xsd(data, process)
 | 
				
			||||||
 | 
					        elif (type == "xslt"):
 | 
				
			||||||
 | 
					            response['result'] = Parser.xslt(data, process)
 | 
				
			||||||
 | 
					        elif (type == "xpath"):
 | 
				
			||||||
 | 
					            response['result'] = Parser.xpath(data, process)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            raise ValueError("Valid operation types are: xsd, xslt, xpath")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        response['status'] = "OK"
 | 
					        response['status'] = "OK"
 | 
				
			||||||
    except KeyError as e:
 | 
					    except KeyError as e:
 | 
				
			||||||
        response['result'] = "Missing key: " + str(e)
 | 
					        response['result'] = "Missing key: " + str(e)
 | 
				
			||||||
@@ -33,26 +52,7 @@ def process_xml(request, type):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@app.route("/xpath", methods=["POST"])
 | 
					@app.route("/xpath", methods=["POST"])
 | 
				
			||||||
def xpath():
 | 
					def xpath():
 | 
				
			||||||
    start = time.time_ns()
 | 
					    return process_xml(request, "xpath")
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    response = dict()
 | 
					 | 
				
			||||||
    try:
 | 
					 | 
				
			||||||
        request_data = json.loads(request.get_data(as_text=True))
 | 
					 | 
				
			||||||
        xml = request_data['data']
 | 
					 | 
				
			||||||
        xpath = request_data['process']
 | 
					 | 
				
			||||||
        response['result'] = Parser.xpath(xml, xpath)
 | 
					 | 
				
			||||||
        response['status'] = "OK"
 | 
					 | 
				
			||||||
    except KeyError as e:
 | 
					 | 
				
			||||||
        response['result'] = "Missing key: " + str(e)
 | 
					 | 
				
			||||||
        response['status'] = "ERR"
 | 
					 | 
				
			||||||
    except Exception as e:
 | 
					 | 
				
			||||||
        response['result'] = str(e)
 | 
					 | 
				
			||||||
        response['status'] = "ERR"
 | 
					 | 
				
			||||||
    finally:
 | 
					 | 
				
			||||||
        exec_time = (time.time_ns() - start) / 10**6
 | 
					 | 
				
			||||||
        response['time'] = f"{exec_time:.03f}"
 | 
					 | 
				
			||||||
        response['processor'] = "libxml2 over lxml"
 | 
					 | 
				
			||||||
        return json.dumps(response)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.route("/xsd", methods=["POST"])
 | 
					@app.route("/xsd", methods=["POST"])
 | 
				
			||||||
def xsd():
 | 
					def xsd():
 | 
				
			||||||
@@ -60,23 +60,7 @@ def xsd():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@app.route("/xslt", methods=["POST"])
 | 
					@app.route("/xslt", methods=["POST"])
 | 
				
			||||||
def xslt():
 | 
					def xslt():
 | 
				
			||||||
    start = time.time_ns()
 | 
					    return process_xml(request, "xslt")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    response = dict()
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    try:
 | 
					    app.run()
 | 
				
			||||||
        request_data = json.loads(request.get_data(as_text=True))
 | 
					 | 
				
			||||||
        xml = request_data['data']
 | 
					 | 
				
			||||||
        xslt = request_data['process']
 | 
					 | 
				
			||||||
        response['result'] = Parser.xslt(xml, xslt)
 | 
					 | 
				
			||||||
        response['status'] = "OK"
 | 
					 | 
				
			||||||
    except KeyError as e:
 | 
					 | 
				
			||||||
        response['result'] = "Missing key: " + str(e)
 | 
					 | 
				
			||||||
        response['status'] = "ERR"
 | 
					 | 
				
			||||||
    except Exception as e:
 | 
					 | 
				
			||||||
        response['result'] = str(e)
 | 
					 | 
				
			||||||
        response['status'] = "ERR"
 | 
					 | 
				
			||||||
    finally:
 | 
					 | 
				
			||||||
        exec_time = (time.time_ns() - start) / 10**6
 | 
					 | 
				
			||||||
        response['time'] = f"{exec_time:.03f}"
 | 
					 | 
				
			||||||
        response['processor'] = "libxml2 over lxml"
 | 
					 | 
				
			||||||
        return json.dumps(response)
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user