287 lines
12 KiB
Plaintext
287 lines
12 KiB
Plaintext
|
|
{
|
||
|
|
"cells": [
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 1,
|
||
|
|
"metadata": {},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"\"\"\"\n",
|
||
|
|
"The module read_binary_format defines the function that reads \n",
|
||
|
|
"the stream of data from Secondo server which contains the result\n",
|
||
|
|
"of commands/ queries in binary format and decodes them to textual format.\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"from struct import *\n",
|
||
|
|
"\n",
|
||
|
|
"import asyncio\n",
|
||
|
|
"import nest_asyncio\n",
|
||
|
|
"nest_asyncio.apply()\n",
|
||
|
|
"\n",
|
||
|
|
"from libs_pkg.exception_handler import *\n",
|
||
|
|
"\n",
|
||
|
|
"async def binary_decode(reader):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" This function binary_decode() reads a binary byte from stream of data from Secondo server \n",
|
||
|
|
" which contains the result of commands/ queries in binary format. The read byte represents\n",
|
||
|
|
" the type of next element in the stream. The function then decodes the element to textual format according\n",
|
||
|
|
" to read type.\n",
|
||
|
|
"\n",
|
||
|
|
" :param reader: The stream reader of the Secondo object that receives the result from Secondo Server.\n",
|
||
|
|
" :return: The decoded element in the result list.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" \n",
|
||
|
|
" if reader == None:\n",
|
||
|
|
" reader.close()\n",
|
||
|
|
" raise SecondoAPI_ERROR('Connection to Secondo reset.')\n",
|
||
|
|
" else: \n",
|
||
|
|
" Typ = await reader.readexactly(1)\n",
|
||
|
|
" Typ = int.from_bytes(Typ, byteorder='big', signed=False)\n",
|
||
|
|
" \n",
|
||
|
|
" Type_dict = {\"BIN_LONGLIST\": 0,\n",
|
||
|
|
" \"BIN_INTEGER\": 1,\n",
|
||
|
|
" \"BIN_REAL\": 2,\n",
|
||
|
|
" \"BIN_BOOLEAN\": 3,\n",
|
||
|
|
" \"BIN_LONGSTRING\": 4,\n",
|
||
|
|
" \"BIN_LONGSYMBOL\": 5,\n",
|
||
|
|
" \"BIN_LONGTEXT\": 6,\n",
|
||
|
|
" \"BIN_LIST\": 10,\n",
|
||
|
|
" \"BIN_SHORTLIST\": 11,\n",
|
||
|
|
" \"BIN_SHORTINT\": 12,\n",
|
||
|
|
" \"BIN_BYTE\":13,\n",
|
||
|
|
" \"BIN_STRING\": 14,\n",
|
||
|
|
" \"BIN_SHORTSTRING\": 15,\n",
|
||
|
|
" \"BIN_SYMBOL\": 16,\n",
|
||
|
|
" \"BIN_SHORTSYMBOL\": 17,\n",
|
||
|
|
" \"BIN_TEXT\": 18,\n",
|
||
|
|
" \"BIN_SHORTTEXT\": 19,\n",
|
||
|
|
" \"BIN_DOUBLE\": 20}\n",
|
||
|
|
" type_code = None\n",
|
||
|
|
" for typ, val in Type_dict.items():\n",
|
||
|
|
" if val == Typ:\n",
|
||
|
|
" type_code = val\n",
|
||
|
|
" break\n",
|
||
|
|
" \n",
|
||
|
|
" if type_code == 0: #LongList\n",
|
||
|
|
" LE = []\n",
|
||
|
|
" barray = bytearray(4) ####readInt()\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" assert (int.from_bytes(barray, byteorder = 'big', signed = True)) >= 0, \"Invalid Integer-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('>I',barray)[0]\n",
|
||
|
|
" if r == 0:\n",
|
||
|
|
" return LE\n",
|
||
|
|
" else:\n",
|
||
|
|
" for i in range(r):\n",
|
||
|
|
" LE.append(await binary_decode(reader))\n",
|
||
|
|
" return LE\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 1:\n",
|
||
|
|
" ####readInt()\n",
|
||
|
|
" barray = bytearray(4)\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" assert (int.from_bytes(barray, byteorder = 'big', signed = True)) >= 0, \"Invalid Integer-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('>I',barray)[0]\n",
|
||
|
|
" return r \n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 2:\n",
|
||
|
|
" ####readReal()\n",
|
||
|
|
" barray = bytearray(4)\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" assert (int.from_bytes(barray, order = 'big', signed = True)) >= 0, \"Invalid Real-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('>f',barray)[0]\n",
|
||
|
|
" return r \n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 3:\n",
|
||
|
|
" ####readBool()\n",
|
||
|
|
" barray = bytearray(1)\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" assert (int.from_bytes(barray, order = 'big', signed = True)) >= 0, \"Invalid Boolean-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('?',barray)[0]\n",
|
||
|
|
" return r\n",
|
||
|
|
" \n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 4:\n",
|
||
|
|
" ####readLongString()\n",
|
||
|
|
" barray = bytearray(4) #readInt()\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, order = 'big', signed = True)) >= 0, \"Invalid Int-Byte read from Stream!\"\n",
|
||
|
|
" len = unpack('>I',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return '\"' + barray.decode() + '\"'\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 5:\n",
|
||
|
|
" ####readLongSymbol()\n",
|
||
|
|
" barray = bytearray(4) #readInt()\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, order = 'big', signed = True)) >= 0, \"Invalid Int-Byte read from Stream!\"\n",
|
||
|
|
" len = unpack('>I',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return barray.decode()\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 6:\n",
|
||
|
|
" ####readLongText()\n",
|
||
|
|
" barray = bytearray(4) #readInt()\n",
|
||
|
|
" barray = await reader.readexactly(4)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, order = 'big', signed = True)) >= 0, \"Invalid Int-Byte read from Stream!\"\n",
|
||
|
|
" len = unpack('>I',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" txt = barray.decode()\n",
|
||
|
|
" if not txt.startswith('<text>'):\n",
|
||
|
|
" txt = '\"<text>' + txt + '</text--->\"'\n",
|
||
|
|
" return txt\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 10: #List\n",
|
||
|
|
" LE = []\n",
|
||
|
|
" ####readShort()\n",
|
||
|
|
" barray = bytearray(2)\n",
|
||
|
|
" barray = await reader.readexactly(2)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, order = 'big', signed = True)) >= 0, \"Invalid ShortInt-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('>H',barray)[0]\n",
|
||
|
|
" if r == 0:\n",
|
||
|
|
" return LE\n",
|
||
|
|
" else:\n",
|
||
|
|
" for i in range(r):\n",
|
||
|
|
" LE.append(await binary_decode(reader))\n",
|
||
|
|
" return LE\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 11: #ShortList\n",
|
||
|
|
" LE = []\n",
|
||
|
|
" ####readByte()\n",
|
||
|
|
" barray = bytearray(1)\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" r = int.from_bytes(barray, byteorder = 'big', signed = False)\n",
|
||
|
|
" if r == 0:\n",
|
||
|
|
" return LE\n",
|
||
|
|
" else:\n",
|
||
|
|
" for i in range(r):\n",
|
||
|
|
" LE.append(await binary_decode(reader))\n",
|
||
|
|
" return LE\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 12:\n",
|
||
|
|
" ####readShort()\n",
|
||
|
|
" barray = bytearray(2)\n",
|
||
|
|
" barray = await reader.readexactly(2)\n",
|
||
|
|
" r = unpack('>H',barray)[0]\n",
|
||
|
|
" return r\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 13:\n",
|
||
|
|
" ####readByte()\n",
|
||
|
|
" barray = bytearray(1)\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" r = int.from_bytes(barray, byteorder = 'big', signed = True)\n",
|
||
|
|
" return r\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 14:\n",
|
||
|
|
" ####readString()\n",
|
||
|
|
" barray = bytearray(2)\n",
|
||
|
|
" barray = await reader.readexactly(2)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, byteorder = 'big', signed = True)) >= 0, \"Invalid ShortInt-Byte read from Stream!\"\n",
|
||
|
|
" len = unpack('>H',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return '\"' + barray.decode() + '\"'\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 15:\n",
|
||
|
|
" ####readShortString()\n",
|
||
|
|
" barray = bytearray(1)\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" len = int.from_bytes(barray, byteorder = 'big', signed = False)\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return '\"' + barray.decode() + '\"'\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 16:\n",
|
||
|
|
" ####readSymbol()\n",
|
||
|
|
" barray = bytearray(2) #readShort()\n",
|
||
|
|
" barray = await reader.readexactly(2)\n",
|
||
|
|
" for b in barray:\n",
|
||
|
|
" assert (int.from_bytes(b, byteorder = 'big', signed = True)) >= 0, \"Invalid ShortInt-Byte read from Stream!\"\n",
|
||
|
|
" len = unpack('>H',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return barray.decode()\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 17:\n",
|
||
|
|
" ####readShortSymbol()\n",
|
||
|
|
" barray = bytearray(1) #readByte()\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" len = int.from_bytes(barray, byteorder = 'big', signed = False)\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" return barray.decode()\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 18:\n",
|
||
|
|
" ####readText()\n",
|
||
|
|
" barray = bytearray(2) #readShort()\n",
|
||
|
|
" barray = await reader.readexactly(2)\n",
|
||
|
|
" len = unpack('>H',barray)[0]\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" ###checking Environment.ENCODING not considered yet\n",
|
||
|
|
" txt = barray.decode()\n",
|
||
|
|
" if not txt.startswith('<text>'):\n",
|
||
|
|
" txt = '\"<text>' + txt + '</text--->\"'\n",
|
||
|
|
" \n",
|
||
|
|
" return txt\n",
|
||
|
|
" \n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 19:\n",
|
||
|
|
" ####readShortText()\n",
|
||
|
|
" barray = bytearray(1) #readByte()\n",
|
||
|
|
" barray = await reader.readexactly(1)\n",
|
||
|
|
" len = int.from_bytes(barray, byteorder = 'big', signed = False)\n",
|
||
|
|
" barray = bytearray(len)\n",
|
||
|
|
" barray = await reader.readexactly(len)\n",
|
||
|
|
" txt = barray.decode()\n",
|
||
|
|
" if not txt.startswith('<text>'):\n",
|
||
|
|
" txt = '\"<text>' + txt + '</text--->\"'\n",
|
||
|
|
" \n",
|
||
|
|
" return txt\n",
|
||
|
|
" \n",
|
||
|
|
" elif type_code == 20:\n",
|
||
|
|
" ####readDouble()\n",
|
||
|
|
" barray = bytearray(8)\n",
|
||
|
|
" barray = await reader.readexactly(8)\n",
|
||
|
|
" assert (int.from_bytes(barray, byteorder = 'big', signed = True)) >= 0, \"Invalid Double-Byte read from Stream!\"\n",
|
||
|
|
" r = unpack('>d',barray)[0]\n",
|
||
|
|
" return r\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
|
||
|
|
}
|