Redmine #1835: improved and expanded API calls

This commit is contained in:
Cyprian Laskowski 2021-03-25 15:02:50 +01:00
parent 51f5e14ee9
commit 695dd78379

View File

@ -1,8 +1,9 @@
import os import os
import shutil import shutil
import tempfile import tempfile
import lxml.etree as lxml
from flask import Flask, Response from flask import Flask, Response, request
from flask_httpauth import HTTPBasicAuth from flask_httpauth import HTTPBasicAuth
from structure_assignment.pipeline import Runner from structure_assignment.pipeline import Runner
@ -14,48 +15,105 @@ resource_directory = os.environ['API_RESOURCE_DIR']
runner = Runner(resource_directory, True) runner = Runner(resource_directory, True)
@app.route(api_prefix + '/string_to_parse/<string:string>', methods=['GET']) @app.route(api_prefix + '/string_to_parse', methods=['GET', 'POST'])
def string_to_parse(string): def string_to_parse():
tmp_directory = tempfile.mkdtemp() tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt' string_file_name = tmp_directory + '/input_string.txt'
parsed_file_name = tmp_directory + '/tmp/parsed.xml' parsed_file_name = tmp_directory + '/output_parsed.xml'
with open(string_file_name, 'w') as string_file: if (request.method == 'GET'):
string_file.write(string + '\n') string = request.args.get('string')
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
elif (request.method == 'POST'):
file_data = request.files['file']
file_data.save(string_file_name)
try: try:
runner.strings_to_parse(string_file_name, parsed_file_name) runner.strings_to_parse(string_file_name, parsed_file_name)
root = lxml.parse(parse_file_name).getroot() root = lxml.parse(parsed_file_name).getroot()
message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode() message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode()
shutil.rmtree(tmp_directory) shutil.rmtree(tmp_directory)
except Exception as e: except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode() message = '<error>' + str(e) + '</error>'
return Response(message, mimetype='text/xml') return Response(message, mimetype='text/xml')
@app.route(api_prefix + '/string_to_dictionary/<string:string>', methods=['GET']) @app.route(api_prefix + '/parse_to_dictionary', methods=['POST'])
def string_to_dictionary(string): def parse_to_dictionary():
tmp_directory = tempfile.mkdtemp() tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt' parsed_file_name = tmp_directory + '/input_parsed.xml'
dictionary_file_name = tmp_directory + '/tmp/dict.xml' dictionary_file_name = tmp_directory + '/output_dictionary.xml'
structure_file_name = tmp_directory + '/tmp/structures.xml' structure_file_name = tmp_directory + '/output_structures.xml'
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
try: try:
runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name)
file_data = request.files['file']
file_data.save(parsed_file_name)
runner.parse_to_dictionary(parsed_file_name, dictionary_file_name, structure_file_name)
root = lxml.Element('response')
dictionary_root = lxml.parse(dictionary_file_name).getroot() dictionary_root = lxml.parse(dictionary_file_name).getroot()
structure_root = lxml.parse(structure_file_name).getroot()
root = lxml.Element('root')
root.append(structure_root)
root.append(dictionary_root) root.append(dictionary_root)
structure_root = lxml.parse(structure_file_name).getroot()
new_structure_count = len(structure_root.xpath('.//syntactic_structure[@tempId]'))
root.set('new_structures', str(new_structure_count))
structure_ids = set(dictionary_root.xpath('.//lexicalUnit/@structure_id'))
structures = structure_root.xpath('syntactic_structure')
structures_element = lxml.SubElement(root, 'syntactic_structures')
for structure in structures:
if (structure.get('id') in structure_ids or structure.get('tempId') in structure_ids):
structures_element.append(structure)
message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode() message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode()
shutil.rmtree(tmp_directory) shutil.rmtree(tmp_directory)
except Exception as e: except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode() message = '<error>' + str(e) + '</error>'
return Response(message, mimetype='text/xml') return Response(message, mimetype='text/xml')
@app.route(api_prefix + '/string_to_dictionary', methods=['GET', 'POST'])
def string_to_dictionary():
tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/input_string.txt'
dictionary_file_name = tmp_directory + '/output_dictionary.xml'
structure_file_name = tmp_directory + '/output_structures.xml'
try:
if (request.method == 'GET'):
string = request.args.get('string')
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
elif (request.method == 'POST'):
file_data = request.files['file']
file_data.save(string_file_name)
runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name)
root = lxml.Element('response')
dictionary_root = lxml.parse(dictionary_file_name).getroot()
root.append(dictionary_root)
structure_root = lxml.parse(structure_file_name).getroot()
new_structure_count = len(structure_root.xpath('.//syntactic_structure[@tempId]'))
root.set('new_structures', str(new_structure_count))
structure_ids = set(dictionary_root.xpath('.//lexicalUnit/@structure_id'))
structures = structure_root.xpath('syntactic_structure')
structures_element = lxml.SubElement(root, 'syntactic_structures')
for structure in structures:
if (structure.get('id') in structure_ids or structure.get('tempId') in structure_ids):
structures_element.append(structure)
message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode()
shutil.rmtree(tmp_directory)
except Exception as e:
message = '<error>' + str(e) + '</error>'
return Response(message, mimetype='text/xml')