You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
62 lines
2.1 KiB
import os
|
|
import shutil
|
|
import tempfile
|
|
|
|
from flask import Flask, Response
|
|
from flask_httpauth import HTTPBasicAuth
|
|
|
|
from structure_assignment.pipeline import Runner
|
|
|
|
app = Flask(__name__)
|
|
api_prefix = os.environ['API_PREFIX']
|
|
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):
|
|
|
|
tmp_directory = tempfile.mkdtemp()
|
|
string_file_name = tmp_directory + '/tmp/string.txt'
|
|
parsed_file_name = tmp_directory + '/tmp/parsed.xml'
|
|
|
|
with open(string_file_name, 'w') as string_file:
|
|
string_file.write(string + '\n')
|
|
|
|
try:
|
|
runner.strings_to_parse(string_file_name, parsed_file_name)
|
|
root = lxml.parse(parse_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()
|
|
|
|
return Response(message, mimetype='text/xml')
|
|
|
|
|
|
@app.route(api_prefix + '/string_to_dictionary/<string:string>', methods=['GET'])
|
|
def string_to_dictionary(string):
|
|
|
|
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'
|
|
|
|
with open(string_file_name, 'w') as string_file:
|
|
string_file.write(string + '\n')
|
|
|
|
try:
|
|
runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name)
|
|
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)
|
|
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()
|
|
|
|
return Response(message, mimetype='text/xml')
|