Files
secondo/apis/python2/SecondoAPI/libs_pkg/.ipynb_checkpoints/secondo_results-checkpoint.ipynb
2026-01-23 17:03:45 +08:00

171 lines
6.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"The module secondo_results contains two functions for receiving the results from Secondo\n",
"and decoding the result to textual format if the default seocndo result format is binary.\n",
"\"\"\"\n",
"import asyncio\n",
"import nest_asyncio\n",
"nest_asyncio.apply()\n",
"from struct import *\n",
"import import_ipynb\n",
"\n",
"from libs_pkg.nested_list import nl_parse\n",
"from libs_pkg.read_binary_format import *\n",
"from libs_pkg.exception_handler import *\n",
"\n",
"async def receive_query_result_list(reader, final_token, bin_list):\n",
" \"\"\"\n",
" The function receive_query_result_list() reads the result list after execution of\n",
" commands/ queries from stream reader of the Secondo object until reaching the \n",
" final token then stops reading. If the the default seocndo result format is \n",
" binary it calls another function for decoding the result to textual format.\n",
"\n",
" :param reader: The stream reader of the Secondo object.\n",
" :param final_token: Stops reading by receiving this element from the stream.\n",
" :param bin_list: A binary value that defines the default seocndo result format, True when binary and False when textual nested list.\n",
" :return: The result list in textual/ binary format.\n",
" \"\"\"\n",
" result_list = []\n",
" if not bin_list:\n",
" result = ''\n",
" count = 1\n",
" \n",
" if reader == None:\n",
" reader.close()\n",
" raise SecondoAPI_ERROR('Connection to Secondo reset.')\n",
" \n",
" while True: \n",
" line = await reader.readline()\n",
" count += 1\n",
" line = line.decode('ISO-8859-1').rstrip()\n",
" if final_token in line:\n",
" result += line\n",
" break\n",
" \n",
" if line:\n",
" result += line # + ' '\n",
" else:\n",
" raise SecondoAPI_ERROR('Empty line read from Secondo.')\n",
" result_list = nl_parse(result, False)\n",
" \n",
" else: #bin_list = True --> readBinaryFrom(inputStream)\n",
" line = await reader.readexactly(3)\n",
" signature = line.decode().rstrip()\n",
" \n",
" #readShort()\n",
" barray = bytearray(2)\n",
" barray = await reader.readexactly(2)\n",
" major = unpack('>H',barray)\n",
" major = major[0]\n",
" \n",
" barray = await reader.readexactly(2)\n",
" minor = unpack('>H',barray)\n",
" minor = minor[0]\n",
" \n",
" \n",
" if signature != \"bnl\":\n",
" raise SecondoAPI_ERROR(signatur + ' is wrong Signatur.')\n",
" \n",
" \n",
" if (major != 1 or (minor != 0 and minor != 1 and minor != 2)):\n",
" raise SecondoAPI_ERROR(str(major) + '.' + str(minor) + ' is wrong Signatur.')\n",
" \n",
" result_list = await binary_decode(reader)\n",
" line = await reader.readline()\n",
" \n",
" return result_list\n",
"\n",
"\n",
"\n",
"async def receive_secondo_response(reader, bin_list):\n",
" \n",
" \"\"\"\n",
" This function receive_secondo_response() reads the response of the Secondo server after execution of\n",
" commands/ queries from stream reader of the Secondo object until reaching specific tags. \n",
" It calls the function receive_query_result_list() when receiving the <SecondoResponse> tag to get the actual result list.\n",
"\n",
" :param reader: The stream reader of the Secondo object.\n",
" :param bin_list: A binary value that defines the default seocndo result format, True when binary and False when textual nested list.\n",
" :return: The result list in textual/ binary format.\n",
" \"\"\"\n",
" \n",
" if not reader:\n",
" raise SecondoAPI_ERROR('Connection to SecondoServer reset.')\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" \n",
" if not line:\n",
" raise SecondoAPI_ERROR('Connection to SecondoServer reset.')\n",
" count = 2\n",
" while line == \"<Message>\":\n",
" Message_List = await receive_query_result_list(reader, \"</Message>\", bin_list)\n",
" \n",
" if not Message_List:\n",
" raise SecondoAPI_ERROR('Error- MessageList from Secondo is empty.')\n",
" \n",
" line = await reader.readline()\n",
" count += 1\n",
" line = line.decode().rstrip()\n",
" \n",
" if not line:\n",
" raise SecondoAPI_ERROR('Empty line read from Secondo.')\n",
" \n",
" if line == \"<SecondoError>\":\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" if line:\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" \n",
" else:\n",
" raise SecondoERROR('Empty result while trying to read Error from Secondo.')\n",
" return None\n",
" \n",
" if line != \"<SecondoResponse>\":\n",
" raise SecondoERROR('Wrong Data received from Secondo.')\n",
" \n",
" result_list = await receive_query_result_list(reader, \"</SecondoResponse>\", bin_list)\n",
" if not result_list:\n",
" return None\n",
" return result_list\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}