Redmine #1835: improved and expanded API calls

master
Cyprian Laskowski 3 years ago
parent 51f5e14ee9
commit 695dd78379

@ -1,8 +1,9 @@
import os
import shutil
import tempfile
import lxml.etree as lxml
from flask import Flask, Response
from flask import Flask, Response, request
from flask_httpauth import HTTPBasicAuth
from structure_assignment.pipeline import Runner
@ -14,48 +15,105 @@ resource_directory = os.environ['API_RESOURCE_DIR']
runner = Runner(resource_directory, True)
@app.route(api_prefix + '/string_to_parse/<string:string>', methods=['GET'])
def string_to_parse(string):
@app.route(api_prefix + '/string_to_parse', methods=['GET', 'POST'])
def string_to_parse():
tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt'
parsed_file_name = tmp_directory + '/tmp/parsed.xml'
string_file_name = tmp_directory + '/input_string.txt'
parsed_file_name = tmp_directory + '/output_parsed.xml'
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
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)
try:
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()
shutil.rmtree(tmp_directory)
except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode()
message = '<error>' + str(e) + '</error>'
return Response(message, mimetype='text/xml')
@app.route(api_prefix + '/string_to_dictionary/<string:string>', methods=['GET'])
def string_to_dictionary(string):
@app.route(api_prefix + '/parse_to_dictionary', methods=['POST'])
def parse_to_dictionary():
tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt'
dictionary_file_name = tmp_directory + '/tmp/dict.xml'
structure_file_name = tmp_directory + '/tmp/structures.xml'
parsed_file_name = tmp_directory + '/input_parsed.xml'
dictionary_file_name = tmp_directory + '/output_dictionary.xml'
structure_file_name = tmp_directory + '/output_structures.xml'
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
try:
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()
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')
@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()
structure_root = lxml.parse(structure_file_name).getroot()
root = lxml.Element('root')
root.append(structure_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()
shutil.rmtree(tmp_directory)
except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode()
message = '<error>' + str(e) + '</error>'
return Response(message, mimetype='text/xml')

Loading…
Cancel
Save