Files
secondo/apis/python2/SecondoAPI/.ipynb_checkpoints/Base-checkpoint.ipynb

1321 lines
46 KiB
Plaintext
Raw Permalink Normal View History

2026-01-23 17:03:45 +08:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"import os\n",
"import configparser as cfg\n",
"import pandas as pd\n",
"import socket\n",
"import sys\n",
"import time\n",
"from io import BytesIO\n",
"import asyncio\n",
"\n",
"# SecondoMonitor -s in Secondo/bin\n",
"#StartOptServer in Secondo/Optimizer\n",
"#sgui in Secondo/Javagui"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b030ee9083874fdaa71fd8366ed67f04",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(button_style='primary', description='connect', icon='server', style=ButtonStyle(), toolt…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dcb4e0f2224d4fe4b0f952880877356f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Label(value=''),))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Name\n",
"0 int\n",
"1 real\n",
"2 point\n",
"3 region\n"
]
}
],
"source": [
"#kleines GUI\n",
"\n",
"btn_server=widgets.Button(\n",
" max_height= '10px',\n",
" max_width='10px',\n",
" description='connect',\n",
" disabled=False,\n",
" button_style='primary', # 'success', 'info', 'warning', 'danger' or ''\n",
" tooltip='Connect to Secondo Server',\n",
" icon='server'\n",
")\n",
"btn_algebralist=widgets.Button(\n",
" max_height= '50px',\n",
" max_width='50px',\n",
" description='Algebras',\n",
" disabled=True,\n",
" button_style='primary', # 'success', 'infojöm, 'warning', 'danger' or ''\n",
" tooltip='List of all available Algebras',\n",
" icon='list'\n",
") \n",
"hbox =widgets.HBox([btn_server,btn_algebralist])\n",
"display(hbox)\n",
"lbl=widgets.Label(background_color='blue')\n",
"vbox=widgets.VBox([lbl])\n",
"display(vbox)\n",
"list_algebra=['int','real','point','region']\n",
"algebra=pd.DataFrame(list_algebra,columns=['Name'])\n",
"print(algebra)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"127.0.0.1 1234 true mahsa 1234 localhost 1235 true\n"
]
}
],
"source": [
"# config_Datei lesen\n",
"\n",
"Config = cfg.ConfigParser()\n",
"\n",
"def ConfigSectionMap(section):\n",
" dict1 = {}\n",
" options = Config.options(section)\n",
" for option in options:\n",
" try:\n",
" dict1[option] = Config.get(section, option)\n",
" if dict1[option] == -1:\n",
" DebugPrint(\"skip: %s\" % option)\n",
" except:\n",
" print(\"exception on %s!\" % option)\n",
" dict1[option] = None\n",
" return dict1\n",
"\n",
"servername=\"\"\n",
"user=\"\"\n",
"passw=\"\"\n",
"serverport=0\n",
"startconnection=False\n",
"optservername = \"\"\n",
"optserverport = 0\n",
"optenable = \"\"\n",
"\n",
"if os.path.isfile(os.getcwd()+'/'+\"gui.cfg\"):\n",
" \n",
" Config.read(os.getcwd()+'/'+\"gui.cfg\")\n",
" #servername=Config.get(\"General\", \"SERVERNAME\")\n",
" #serverport=Config.getint(\"General\", \"SERVERPORT\")\n",
" #startconnection=Config.getboolean(\"General\", \"START_CONNECTION\")\n",
" #user=Config.get(\"General\", \"USER\")\n",
" #passw=Config.get(\"General\", \"PASSWD\")\n",
" \n",
" servername = ConfigSectionMap(\"General\")['servername']\n",
" serverport = ConfigSectionMap(\"General\")['serverport']\n",
" startconnection = ConfigSectionMap(\"General\")['start_connection']\n",
" user = ConfigSectionMap(\"General\")['user']\n",
" passw = ConfigSectionMap(\"General\")['passwd']\n",
" optservername = ConfigSectionMap(\"General\")['optimizer_host']\n",
" optserverport = ConfigSectionMap(\"General\")['optimizer_port']\n",
" optenable = ConfigSectionMap(\"General\")['enable_optimizer']\n",
" \n",
"else:\n",
" print(\"The ConfigFile not found!\") \n",
"\n",
"if optservername == \"\":\n",
" optservername = \"localhost\"\n",
"if int(optserverport) <= 0:\n",
" optserverport = 1235\n",
" \n",
"print(servername,serverport,startconnection,user,passw, optservername, optserverport, optenable) \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Received: <SecondoOk/>\n",
"\n",
"<Connect>\n",
"mahsa\n",
"1234\n",
"</Connect>\n",
"\n",
"Received: '<SecondoIntro>\\n'\n",
"Received: 'You are connected with a Secondo server.\\n'\n",
"Received: '</SecondoIntro>\\n'\n"
]
}
],
"source": [
"#Connecting to SecondoServer with acyncio method\n",
"\n",
"initialized = False\n",
"\n",
"async def tcp_echo_client(message1, server, port):\n",
" \n",
" reader, writer = await asyncio.open_connection(\n",
" server, int(port))\n",
" \n",
" while True:\n",
" line = await reader.readline()\n",
" if not line:\n",
" print(\"No response from Server!\")\n",
" initialized = False\n",
" break\n",
" line = line.decode()\n",
" #.rstrip()\n",
" if line != '<SecondoOk/>\\n':\n",
" print(f'Received: {line!r}')\n",
" initialized = False\n",
" break\n",
" \n",
" if line == '<SecondoOk/>\\n':\n",
" print(f'Received: {line}')\n",
" print(message1)\n",
" \n",
" writer.write(message1.encode())\n",
" await writer.drain()\n",
" \n",
" while True:\n",
" line = await reader.readline()\n",
" \n",
" if not line:\n",
" print(\"Connection to Server failed!\")\n",
" initialized = False\n",
" break\n",
"\n",
" line = line.decode()\n",
" if line == '<SecondoIntro>\\n':\n",
" print(f'Received: {line!r}')\n",
" while True:\n",
" line = await reader.readline()\n",
" \n",
" if not line:\n",
" print(\"Connection to Server failed!\")\n",
" initialized = False\n",
" break\n",
" line = line.decode()\n",
" \n",
" if line == '</SecondoIntro>\\n':\n",
" print(f'Received: {line!r}')\n",
" initialized = True\n",
" return reader, writer\n",
" break\n",
" \n",
" elif line == '<SecondoError>\\n':\n",
" print(f'Received: {line!r}')\n",
" initialized = False\n",
" break\n",
" else:\n",
" print(f'Received: {line!r}')\n",
" initialized = False\n",
" break\n",
" \n",
" \n",
" break \n",
" \n",
" # Ignore the body, close the socket\n",
" #writer.close()\n",
" #await writer.wait_closed()\n",
" print(initialized)\n",
"message = \"<Connect>\\n\" + user + \"\\n\" + passw + \"\\n\" + \"</Connect>\\n\"\n",
"reader, writer = await tcp_echo_client(message, servername, serverport)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import ast\n",
"\n",
"#updates a specific element by value in a nested list with unknown depth and structure\n",
"def change(seq, what, make):\n",
" for i, item in enumerate(seq):\n",
" if item == what:\n",
" seq[i] = make\n",
" elif type(item) == list:\n",
" change(item, what, make)\n",
" return seq\n",
"\n",
"\n",
"#modifying the text types of form '****' to <text>****</text--->\n",
"def change_textform(seq):\n",
" for i, item in enumerate(seq):\n",
" if seq[i] and seq[i] != '<text>' and item[0] == \"'\" and item[1] != \"(\":\n",
" seq[i] = \"<text>\" + item[1:]\n",
" j = i+1 \n",
" while True: \n",
" \n",
" seq[i] += seq[j]\n",
" del seq[j] \n",
" \n",
" if seq[j] == \"'\":\n",
" seq[i] += \"</text--->\"\n",
" del seq[j]\n",
" break\n",
" \n",
" elif seq[i] and seq[i] != '<text>' and item[0] == \"'\" and item[1] == \"(\":\n",
" seq[i] = \"<text>\" + item[1:-1] + \"</text--->\"\n",
" \n",
" elif seq[i] and seq[i] == '<text>':\n",
" item = [\"<text>\",seq[i+1],\"</text--->\"] \n",
" seq[i] = item\n",
" del seq[i+1], seq[i+1]\n",
" \n",
" elif seq[i] and type(item) == list:\n",
" change_textform(item)\n",
" \n",
" return seq\n",
"\n",
"\n",
"#flattens a nested list with unknown depth and structure to a flat list\n",
"def flatten_list(lisst):\n",
" lst = []\n",
" for i in lisst:\n",
" if isinstance (i, list):\n",
" lst.extend (flatten_list (i))\n",
" else:\n",
" lst.append (i)\n",
" return lst \n",
"\n",
"\n",
"#returns index of first appearance of item in a nested list with unknown depth and structure\n",
"def nested_index(lst, target):\n",
" for index, item in enumerate(lst):\n",
" if item == target:\n",
" return [index]\n",
" if isinstance(item, (list, tuple)):\n",
" path = nested_index(item, target)\n",
" if path:\n",
" return [index] + path\n",
" return []\n",
"\n",
"\n",
"#indices=[]\n",
"#for item in flattend_list:\n",
"# indices.append(nested_index(nestedlist,item))\n",
"#print(indices)\n",
"\n",
"#distinguishing the data types of nested list items\n",
"def NL_itemsTypeCheck(NL):\n",
" flattend_list = flatten_list(NL)\n",
" #print (flattend_list)\n",
" for item in flattend_list:\n",
" \n",
" #if not item.startswith('\"') and not item.startswith('<text>') and not item.startswith('</text--->') and item != ' ' and item.lower() != 'from' and item[-1] != ':' and item[-1] != '.' and item.lower() != \"is\":\n",
" if item.isdigit() or item in ('True', 'False'):\n",
" try:\n",
" if type(eval(item)) in(int, float, bool):\n",
" change(NL, item, ast.literal_eval(item))\n",
" except (ValueError, NameError):\n",
" pass\n",
" #print (NL)\n",
" return(NL)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['OBJECT', 'SEC_DERIVED_OBJ', [], ['rel', ['tuple', [['Name', 'string'], ['Value', 'text'], ['UsedObjs', 'text']]]], [['\"plz_Ort\"', ['<text>', ['createbtree', 'plz', 'Ort'], '</text--->'], ['<text>', ['plz'], '</text--->']], ['\"plz_Ort_btree\"', ['<text>', ['createbtree', 'plz', 'Ort'], '</text--->'], ['<text>', ['plz'], '</text--->']], ['\"plz_PLZ\"', ['<text>', ['createbtree', 'plz', 'PLZ'], '</text--->'], ['<text>', ['plz'], '</text--->']], ['\"plz_PLZ_btree\"', ['<text>', ['createbtree', 'plz', 'PLZ'], '</text--->'], ['<text>', ['plz'], '</text--->']], ['\"plz2\"', ['<text>', ['consume', ['remove', ['sortby', ['extend', ['feed', 'plz'], [['Sid', ['fun', ['tuple1', 'TUPLE'], ['randint', ['count', 'plz']]]]]], [['Sid', 'asc']]], ['Sid']]], '</text--->'], ['<text>', ['plz'], '</text--->']], ['\"plz3\"', ['<text>', ['consume', ['remove', ['sortby', ['extend', ['feed', 'plz'], [['Sid', ['fun', ['tuple1', 'TUPLE'], ['randint', ['count', 'plz']]]]]], [['Sid', 'asc']]], ['Sid']]], '</text--->'], ['<text>', ['plz'], '</text--->']]]], ['OBJECT', 'Staedte', ['StaedteRel'], ['rel', ['tuple', [['SName', 'string'], ['Bev', 'int'], ['PLZ', 'int'], ['Vorwahl', 'string'], ['Kennzeichen', 'string']]]], [['\"Aachen\"', 239000, 5100, '\"0241\"', '\"AC\"', True], '<text>(hjhhhjlmmnmkkjn)</text--->', '<text>/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAZAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAgICAgICAgICAwMDAwMDAwMDAwEBAQEBAQECAQECAgIBAgIDAwMDAwMDAwMDAwMDAwMDAwMDAwMD</text--->', ['\"Berlin\"', 1859000, 1000, '\"030\"', '\"B\"'], ['\"Bielefeld\"', 300000, False, 4800, '\"0521\"', '\"BI\"'], ['\"Bochum\"', 382000, 4630, '\"0234\"', '\"BO\"'], ['\"Bonn\"', 291000, 5300, '\"0228\"', '\"BN\"'], ['\"Braunschweig\"', 248000, 3300, '\"0531\"', '\"BS\"'], ['\"Bremen\"', 526000, 2800, '\"0421\"', '\"HB\"'], ['\"Bremerhaven\"', 134000, 2850, '\"0471\"', '\"HB\"']]]]\n"
]
}
],
"source": [
"#Parsing to a Python Nestedlist\n",
"\n",
"\n",
"from pyparsing import *\n",
"import pprint\n",
"import re\n",
"def NLparse(content, ftype): #ftype = True: file as argument, ftype = False: string as argument\n",
" #ident = Word(alphas, alphanums + \"-_.\")\n",
" #number = Word('-'+'+'+nums+'.')\n",
" #nestedParens = nestedExpr('(', ')', content=enclosed) \n",
" enclosed = Forward()\n",
" nestedParens = nestedExpr('(', ')')\n",
" enclosed << (OneOrMore(nestedParens))\n",
" if ftype:\n",
" try:\n",
" with open(content, encoding=\"utf8\", errors='ignore') as content_file:\n",
" \n",
" content = content_file.read()\n",
" NLresult = enclosed.parseString(content).asList()\n",
" #print(NLresult)\n",
" #return NLresult\n",
" \n",
" except (ParseException, ParseFatalException) as err:\n",
" NLresult = []\n",
" print(err)\n",
" print(\"Invalid syntax at line {}, column {}: '{}': {}.\".format(err.lineno,err.column,err.markInputline(),err.msg))\n",
"\n",
" if not ftype:\n",
" \n",
" try:\n",
" NLresult = enclosed.parseString(content).asList()\n",
" #print(NLresult)\n",
" #return NLresult\n",
" \n",
" except (ParseException, ParseFatalException) as err:\n",
" NLresult = []\n",
" print(err)\n",
" print(\"Invalid syntax at line {}, column {}: '{}': {}.\".format(err.lineno,err.column,err.markInputline(),err.msg))\n",
"\n",
" \n",
" NLresult = change_textform(NLresult)\n",
" NLresult = NL_itemsTypeCheck(NLresult)\n",
" return NLresult\n",
"\n",
" \n",
" \n",
" \n",
"#converts the nested list to a string with nested parenthesis\n",
"def NLtoString(NL):\n",
" strresult = pprint.pformat(NL)\n",
" replacements = {\"[\": \"(\", \"]\": \")\", \"'\": \"\"}\n",
" strresult = \"\".join([replacements.get(c, c) for c in strresult])\n",
" #print(strresult)\n",
" return strresult\n",
"NL=NLparse(\"opt.txt\", True)\n",
"#NL=NLparse(\"(restore database berlintest from berlintest)\", False)\n",
"print(NL)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['restore', 'database', 'berlintest', 'from', 'berlintest1']\n",
"name:\n",
"berlintest\n",
"line after </DbRestore>:\n",
"first readline line in receiveResponse():\n",
"<SecondoResponse>\n",
"line before checking <SecondoResponse>:\n",
"<SecondoResponse>\n",
"line before decode('iso8859-1'):\n",
"b'bnl\\x00\\x01\\x00\\x02\\x0b\\x04\\r\\x00\\r\\x00\\x13\\x00\\x0b\\x00</SecondoResponse>\\n'\n",
"receiveList(): read line:\n",
"\u0000\u0013\u0000\u000b",
"\u0000</SecondoResponse>\n",
"receiveList(): result string:\n",
"\u0000\u0013\u0000\u000b",
"\u0000</SecondoResponse>)\n",
"['bnl\\x00\\x01\\x00\\x02\\x0b\\x04', '\\x00', '\\x00\\x13\\x00\\x0b\\x00</SecondoResponse>']\n"
]
}
],
"source": [
"#NLresult = []\n",
"binLists = False\n",
"ftype = False\n",
"\n",
"\n",
"def AtomType(item):\n",
" \n",
" if isinstance(item, str) and item[0] == '\"':\n",
" return \"string\"\n",
" if isinstance(item, str) and item[0] != '\"' and item[0] != '<':\n",
" return \"symbol\"\n",
" if isinstance(item, int):\n",
" return \"integer\"\n",
" if isinstance(item, float):\n",
" return \"real\"\n",
" if isinstance(item, bool):\n",
" return \"boolean\"\n",
" if (isinstance(item, list) and item[0] == \"<text>\") or (isinstance(item, str) and item.startswith('<text>')):\n",
" return \"text\"\n",
" if isinstance(item, list):\n",
" return \"list\"\n",
" \n",
"\n",
"'''\n",
"async def receiveList(finalToken):\n",
" line = \"\"\n",
" result = \"\"\n",
" NL = []\n",
" if not binLists:\n",
" while True:\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" print(\"receiveList(): read line:\")\n",
" print(line)\n",
" if not line:\n",
" print (\"receiveList(): line empty\")\n",
" return \n",
" if line == finalToken:\n",
" break\n",
" elif line != finalToken:\n",
" result = ''.join(line + \"\\n\")\n",
" print(\"receiveList(): result string:\")\n",
" print(result) \n",
" NL = NLparse(result, ftype)\n",
" return NL\n",
" elif binLists:\n",
" pass\n",
"\n",
" ''' \n",
" \n",
"'''\n",
"async def receiveResponse():\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" print(\"first readline line in receiveResponse():\")\n",
" print(line)\n",
" if not line:\n",
" print (\"receiveResponse(): line empty\")\n",
" return\n",
" while line == \"<Message>\":\n",
" NLmessage = await receiveList(\"</Message>\")\n",
" print(\"messagelist:\")\n",
" print(NLmessage)\n",
" if not NLmessage:\n",
" print (\"receiveResponse(): NLmessage empty\")\n",
" return\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" if not line:\n",
" (\"receiveResponse(): line empty\")\n",
" return\n",
" if line == \"<SecondoError>\": #error handling\n",
" print(\"receiveResponse()-inside if <SecondoError>: Start of SecondoError:\")\n",
" error = \"80\"\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" print(line)\n",
" if line:\n",
" error += line\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" else:\n",
" print(\"receiveResponse()-SecondoInterface: Error in reading SecondoError!\")\n",
" return\n",
" print(\"receiveResponse()-SecondoError: End of SecondoError!\")\n",
" return\n",
" print(\"line before checking <SecondoResponse>:\")\n",
" print(line)\n",
" if line != '<SecondoResponse>':\n",
" print(\"line:\")\n",
" print(line)\n",
" print(\"receiveResponse()- Not receiving <SecondoResponse>: Error in reading SecondoResponse!\")\n",
" return\n",
" NLanswer = await receiveList(\"</SecondoResponse>\")\n",
" if not NLanswer:\n",
" print(\"receiveResponse(): NLanswer empty\")\n",
" return\n",
" NLresult = NLanswer[3]\n",
" return NLresult\n",
" \n",
"\n",
"'''\n",
" \n",
"\n",
"\n",
"\n",
"#Command format: restore [database] name from <filename> \n",
"async def restore_command(command):\n",
" if reader and writer:\n",
" if not command.startswith('(') :\n",
" command = '(' + command.strip() + ')'\n",
" \n",
" CmdNL = NLparse(command, ftype)[0]\n",
" print(CmdNL)\n",
" length = len(CmdNL)\n",
" if length != 4 and length != 5:\n",
" print(\"restore_command(): command lenght wrong\") \n",
" return\n",
" if length == 5: #restore database\n",
" if CmdNL[0] != 'restore' or CmdNL[1] != 'database' or CmdNL[3] != 'from':\n",
" print(\"restore_command-fun: command wrong\") \n",
" return\n",
" name = CmdNL[2]\n",
" filename = CmdNL[4]\n",
" database = True\n",
" if length == 4:\n",
" if CmdNL[0] != 'restore' or CmdNL[2] != 'from':\n",
" print(\"restore_command-fun: command lenght wrong\") \n",
" return\n",
" name = CmdNL[1]\n",
" filename = CmdNL[3]\n",
" database = False\n",
" \n",
" typ = AtomType(filename)\n",
" if not typ in (\"string\", \"symbol\", \"text\"):\n",
" print(\"restore_command-fun: filename wrong\") \n",
" return\n",
" \n",
" file = open(filename, encoding=\"utf8\", errors='ignore')\n",
" f = file.read()\n",
" flen = len(f) #number of lines\n",
" tag = \"DbRestore\" if database else \"ObjectRestore\"\n",
" message = \"<\" + tag + \">\\n\"\n",
" message = message.encode()\n",
" writer.write(message)\n",
" #await writer.drain()\n",
" \n",
" message = name + \"\\n\"\n",
" print(\"name:\")\n",
" print(name)\n",
" message = message.encode()\n",
" writer.write(message)\n",
" #await writer.drain()\n",
" \n",
" #### secondoresponse\n",
" \n",
" message = \"<FileData>\\n\"\n",
" message = message.encode()\n",
" writer.write(message)\n",
" #await writer.drain()\n",
" \n",
" message = str(flen) + \"\\n\"\n",
" message = message.encode()\n",
" writer.write(message)\n",
" await writer.drain()\n",
" \n",
" lcount = 0\n",
" writer.write(f.encode())\n",
" #await writer.drain()\n",
" \n",
" #for line1 in f:\n",
" # line1 = line1.encode()\n",
" # writer.write(line1)\n",
" #await writer.drain()\n",
" # lcount +=1\n",
" #if lcount != flen:\n",
" # print(\"some file data missing during transmission to server!\")\n",
" message = \"</FileData>\\n\"\n",
" message = message.encode()\n",
" writer.write(message)\n",
" #await writer.drain()\n",
" \n",
" message = \"</\" + tag + \">\\n\"\n",
" message = message.encode()\n",
" writer.write(message)\n",
" await writer.drain()\n",
" \n",
" print(\"line after </\" + tag + \">:\")\n",
" file.close()\n",
" \n",
" #response = await receiveResponse() # to get result\n",
" ##### ReceiveResponse begin\n",
" \n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" print(\"first readline line in receiveResponse():\")\n",
" print(line)\n",
" if not line:\n",
" print (\"receiveResponse(): line empty\")\n",
" return\n",
" while line == \"<Message>\":\n",
" NLmessage = await receiveList(\"</Message>\")\n",
" print(\"messagelist:\")\n",
" print(NLmessage)\n",
" if not NLmessage:\n",
" print (\"receiveResponse(): NLmessage empty\")\n",
" return\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" if not line:\n",
" (\"receiveResponse(): line empty\")\n",
" return\n",
" if line == \"<SecondoError>\": #error handling\n",
" print(\"receiveResponse()-inside if <SecondoError>: Start of SecondoError:\")\n",
" error = \"80\"\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" print(line)\n",
" if line:\n",
" error += line\n",
" line = await reader.readline()\n",
" line = line.decode().rstrip()\n",
" else:\n",
" print(\"receiveResponse()-SecondoInterface: Error in reading SecondoError!\")\n",
" return\n",
" print(\"receiveResponse()-SecondoError: End of SecondoError!\")\n",
" return\n",
" print(\"line before checking <SecondoResponse>:\")\n",
" print(line)\n",
" if line != '<SecondoResponse>':\n",
" print(\"line:\")\n",
" print(line)\n",
" print(\"receiveResponse()- Not receiving <SecondoResponse>: Error in reading SecondoResponse!\")\n",
" return\n",
" #NLanswer = await receiveList(\"</SecondoResponse>\")\n",
" ### Start of receiveList(\"</SecondoResponse>\")\n",
" \n",
" line = \"\"\n",
" result = \"\"\n",
" NL = []\n",
" if not binLists:\n",
" while True:\n",
" line = await reader.readline()\n",
" print(\"line before decode('iso8859-1'):\")\n",
" print(line)\n",
" #.encode('utf-8')\n",
" line = line.decode('iso8859-1').rstrip()\n",
" print(\"receiveList(): read line:\")\n",
" print(line)\n",
" if not line:\n",
" #result = ''.join(line)\n",
" print (\"receiveList(): line empty\")\n",
" #break \n",
" if \"</SecondoResponse>\" in line :\n",
" #result = ''.join(line + \"\\n\")\n",
" result = ''.join(line)\n",
" break\n",
" elif \"</SecondoResponse>\" not in line :\n",
" result = ''.join(line)\n",
" #print(result)\n",
" print(\"receiveList(): result string:\")\n",
" \n",
" result = \"(\" + result + \")\"\n",
" print(result) \n",
" NL = NLparse(result, ftype)\n",
" #return NL\n",
" elif binLists:\n",
" pass\n",
" \n",
" \n",
" ### End of receiveList(\"</SecondoResponse>\")\n",
" \n",
" if not NL:\n",
" print(\"receiveResponse(): NLanswer empty\")\n",
" return \n",
" \n",
" NLres = NL[0]\n",
" \n",
" \n",
" ####### ReceiveResponse End\n",
" \n",
" return NLres\n",
" \n",
" else:\n",
" print(\"reader and writer Sockets null\")\n",
" return\n",
"\n",
" \n",
"\n",
"async def perform_command(command):\n",
" if command.lstrip().startswith('(restore') or command.lstrip().startswith('( restore') or command.lstrip().startswith('restore'):\n",
" result = await restore_command(command)\n",
" return result\n",
"\n",
" \n",
"\n",
"command = '(restore database berlintest from berlintest1)' \n",
"rs = await perform_command(command)\n",
"print(rs)\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" #finding the index of an item in a list\n",
"#index(...)\n",
"#L.index(value, [start, [stop]]) -> integer -- return first index of value\n",
"\n",
"#\n",
"#i = iter(lisst)\n",
"#first = next(i)\n",
"#rest = list(i)\n",
"\n",
"\n",
"#recursive approach to itterate a nested list of unknown depth and structure\n",
"#def traverse(o, tree_types=(list, tuple)):\n",
"# if isinstance(o, tree_types):\n",
"# for value in o:\n",
"# for subvalue in traverse(value, tree_types):\n",
"# yield subvalue\n",
"# else:\n",
"# yield o\n",
"\n",
"#a = number of inner lists\n",
"#lst = [[] for _ in range(a)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Nonblocking TCP Connection to OptomizerServer\n",
"\n",
"\n",
"optinitialized=False\n",
"\n",
"sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
"server_address = (optservername, int(optserverport))\n",
"sock.connect(server_address)\n",
"sock.setblocking(False)\n",
"\n",
"\n",
"with BytesIO() as buffer:\n",
" message = \"<who>\\n\"\n",
" sock.send(bytes(message.encode()))\n",
" \n",
" while True:\n",
" try:\n",
" resp = sock.recv(20) # Read in some number of bytes -- balance this\n",
" except BlockingIOError:\n",
" print(\"sleeping 1\") # Do whatever you want here, this just\n",
" time.sleep(2) # illustrates that it's nonblocking\n",
" else:\n",
" if resp:\n",
" buffer.write(resp) # Write to the BytesIO object\n",
" elif not resp:\n",
" print(\"Server didnt respond\")\n",
" break \n",
" buffer.seek(0) # Set the file pointer to the SoF\n",
" start_index = 0 # Count the number of characters processed\n",
" \n",
" line = buffer.readline()\n",
" start_index += len(line)\n",
" print(line)\n",
" \n",
" if start_index:\n",
" buffer.seek(start_index)\n",
" remaining = buffer.read()\n",
" buffer.truncate(0)\n",
" buffer.seek(0)\n",
" buffer.write(remaining)\n",
" else:\n",
" buffer.seek(0, 2)\n",
" \n",
" if line == b'<optimizer>\\n':\n",
" optinitialized = True\n",
" \n",
"sock.close()\n",
"print(optinitialized)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" # connecting to OptimizerServer using asyncio methode\n",
"\n",
"optinitialized1 = False\n",
"\n",
"async def tcp_opt_client(server, port,enable, optinitialized):\n",
" \n",
" if enable != \"true\":\n",
" print(\"Optimizer Server cant be contacted according to configurations.\")\n",
" elif optinitialized:\n",
" print(\"Connection to Optimizer Server is already stablished.\")\n",
" elif enable == \"true\" and optinitialized == False:\n",
" print(\"Starting the connection...\")\n",
" optreader, optwriter = await asyncio.open_connection(server, int(port))\n",
" \n",
" message = \"<who>\\n\"\n",
" #optwriter.write(message.encode(\"utf-8\"))\n",
" #await optwriter.drain()\n",
" \n",
" #print(\"Message sent to optserver...\")\n",
" #print(message.encode(\"utf-8\"))\n",
" \n",
" while True:\n",
" optwriter.write(message.encode(\"utf-8\"))\n",
" await optwriter.drain()\n",
" \n",
" print(\"Message sent to optserver...\")\n",
" print(message.encode(\"utf-8\"))\n",
" \n",
" print(\"wants to read from optserver...\")\n",
" line = await optreader.readline()\n",
" print(\"read from optserver...\")\n",
" \n",
" line = line.decode(\"utf-8\")\n",
" print(f'Received: {line!r}')\n",
" \n",
" if not line:\n",
" print(\"No response from Server!\")\n",
" optinitialized = False\n",
" break\n",
" \n",
" #line = line.decode()\n",
" \n",
" if line != \"<optimizer>\\n\":\n",
" print(f'Received wrong data: {line!r}')\n",
" optinitialized = False\n",
" break\n",
" \n",
" if line == \"<optimizer>\\n\":\n",
" \n",
" print(f'Received: {line!r}')\n",
" optinitialized = True\n",
" break \n",
" \n",
" \n",
" # Ignore the body, close the socket\n",
" #optwriter.close()\n",
" #await optwriter.wait_closed()\n",
" print(optinitialized)\n",
" \n",
"await tcp_opt_client(optservername, optserverport, optenable, optinitialized1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#nonblocking TCP Connection to ScondoServer\n",
"\n",
"initialized=False\n",
"\n",
"sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
"server_address = (servername, int(serverport))\n",
"sock.connect(server_address)\n",
"sock.setblocking(False)\n",
"\n",
"\n",
"with BytesIO() as buffer:\n",
" while True:\n",
" try:\n",
" resp = sock.recv(15) # Read in some number of bytes \n",
" except BlockingIOError:\n",
" print(\"sleeping 1\") # Do whatever you want here, this just\n",
" time.sleep(2) # illustrates that it's nonblocking\n",
" else:\n",
" if resp:\n",
" buffer.write(resp) # Write to the BytesIO object\n",
" elif not resp:\n",
" print(\"Server didnt respond\")\n",
" break \n",
" buffer.seek(0) # Set the file pointer to the SoF\n",
" start_index = 0 # Count the number of characters processed\n",
" \n",
" line = buffer.readline()\n",
" start_index += len(line)\n",
" print(line)\n",
" \n",
" if start_index:\n",
" buffer.seek(start_index)\n",
" remaining = buffer.read()\n",
" buffer.truncate(0)\n",
" buffer.seek(0)\n",
" buffer.write(remaining)\n",
" else:\n",
" buffer.seek(0, 2)\n",
" \n",
" if line == b'<SecondoOk/>\\n':\n",
" message = \"<Connect>\\n\" + user + \"\\n\" + passw + \"\\n\" + \"</Connect>\\n\"\n",
" sock.send(bytes(message.encode()))\n",
" while True:\n",
" try:\n",
" resp = sock.recv(80) # Read in some number of bytes \n",
" except BlockingIOError:\n",
" print(\"sleeping 2\") # Do whatever \n",
" time.sleep(2) # illustrates that it's nonblocking\n",
" else:\n",
" if resp:\n",
" buffer.flush()\n",
" buffer.write(resp) # Write to the BytesIO object\n",
" elif not resp: \n",
" print(\"Server didnt respond\")\n",
" break \n",
" buffer.seek(0) # Set the file pointer to the SoF\n",
" start_index1 = 0 # Count the number of characters processed \n",
" line = buffer.readline()\n",
" start_index1 += len(line)\n",
" print(line)\n",
" if line == b'':\n",
" initialized = False\n",
" print(\"second line is null\")\n",
" #break \n",
" elif line == b'<SecondoIntro>\\n':\n",
" while True:\n",
" line = buffer.readline()\n",
" start_index1 += len(line)\n",
" print(line)\n",
" if line == b'':\n",
" initialized = False\n",
" print(\"third line is null\")\n",
" break \n",
" elif line == b'</SecondoIntro>\\n':\n",
" initialized = True\n",
" break\n",
" elif line == b'<SecondoError>\\n':\n",
" initialized = False\n",
" print(\"Error reading from Server!\")\n",
" break \n",
" elif line == b'<SecondoError>\\n':\n",
" initialized = False\n",
" print(\"Error reading from Server!\")\n",
" \n",
" if start_index1:\n",
" buffer.seek(start_index1)\n",
" remaining = buffer.read()\n",
" buffer.truncate(0)\n",
" buffer.seek(0)\n",
" buffer.write(remaining)\n",
" else:\n",
" buffer.seek(0, 2)\n",
" \n",
" \n",
"sock.close()\n",
"print(initialized)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Defining Datatype\n",
"\n",
"import abc\n",
"class Orderable(abc.ABC):\n",
" @abc.abstractmethod\n",
" def before(self, Type):\n",
" pass\n",
" @abc.abstractmethod\n",
" def after(self, Type):\n",
" pass\n",
" @abc.abstractmethod\n",
" def adjacent(self, Type):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Defining Datatype\n",
"\n",
"class GeneralTypeIF(abc.ABC):\n",
" @abc.abstractmethod\n",
" def isDefined(self):\n",
" pass\n",
" @abc.abstractmethod\n",
" def setDefined(defined):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bool Datatype\n",
"\n",
"class BaseBoolIF(GeneralTypeIF, Orderable):\n",
" \n",
" @abc.abstractmethod\n",
" def getValue(self):\n",
" pass "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Defining Datatype\n",
"\n",
"class GeneralType(GeneralTypeIF):\n",
" \n",
" defined = 0\n",
" bool(defined)\n",
" \n",
" def __init__(self):\n",
" self.__defined = false\n",
" \n",
" def isDefined(self):\n",
" return defined\n",
" \n",
" def setDefined(self, defined):\n",
" self.defined = defined\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bool Datatype\n",
"\n",
"class Bool(GeneralType, BaseBoolIF):\n",
" \n",
" value = 0\n",
" bool(value)\n",
" \n",
" def __init__(self):\n",
" self.__value = false\n",
" \n",
" def __init__(self, value):\n",
" self.__value = value\n",
" setDefined(true)\n",
" \n",
" def isDefined(self, original):\n",
" this.value = original.getValue()\n",
" setDefined(original.isDefined())\n",
" \n",
" def compareTo(otherBool):\n",
" self.defined = defined\n",
" \n",
" def __eq__(self, other):\n",
" return self.value == other.getvalue()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('bnl\\x00\\x01\\x00\\x02\\x0b\\x04'.encode('utf-8'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int.from_bytes(b'bnl\\x00\\x01\\x00\\x02\\x0b\\x04\\r\\x00\\r\\x00\\x13\\x00\\x0b\\x00',byteorder='big')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'.'.join(str(ord(c)) for c in 'bnl\\x00\\x01\\x00\\x02\\x0b\\x04\\r\\x00\\r\\x00\\x13\\x00\\x0b\\x00</SecondoResponse>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'\\x0b\\x01\\x11\\x06'.encode()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bs = b'r'\n",
"ints = list(bs)\n",
"print(ints)\n",
"print('dann')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Related classes and Datastructures for Secondo-Datatypes (int,bool,real,string,text,tuple,relation)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#equivalent of MemoryAttribute.java\n",
"import enum\n",
"from interface import implements, Interface\n",
"\n",
"class GeneralType(Interface, GeneralObject):\n",
" \n",
" class PyType(enum.Enum):\n",
" boolean = AttributeBool.class\n",
" integer = AttributeInt.class\n",
" real = AttributeReal.class\n",
" string = AttributeString.class\n",
" text = AttributeText.class\n",
" \n",
" #def __init__(self, TypeClass):\n",
" #self.TypeClass = TypeClass\n",
" \n",
" def InFromList(self,list):\n",
" pass\n",
" \n",
" def OutToList(self):\n",
" pass\n",
" \n",
" def getTypeName(self, TypeClass):\n",
" for ptype in PyType:\n",
" #if issubclass(ptype.TypeClass,TypeClass):\n",
" if issubclass(ptype.value, TypeClass):\n",
" if str(ptype.name) == \"integer\": \n",
" return \"int\";\n",
" if str(ptype.name) == \"boolean\": \n",
" return \"bool\";\n",
" else:\n",
" return str(ptype.name)\n",
" return\n",
" \n",
" \n",
" def getAllTypeNames(self):\n",
" result = set()\n",
" for ptype in PyType:\n",
" if str(ptype.name) == \"integer\":\n",
" result.add(\"int\")\n",
" if str(ptype.name) == \"boolean\":\n",
" result.add(\"bool\")\n",
" else:\n",
" result.add(str(ptype.name))\n",
" return result\n",
" \n",
" def getTypeClass(self, TypeName):\n",
" if TypeName == \"int\":\n",
" name = \"integer\"\n",
" elif TypeName == \"bool\":\n",
" name = \"boolean\"\n",
" else:\n",
" name = TypeName\n",
" for ptype in PyType:\n",
" if str(ptype.name) == name:\n",
" #return type.attributeClass\n",
" return type.value\n",
" return\n",
" \n",
" \n",
" def getAllTypeClasses()\n",
" result = set()\n",
" for ptype in PyType:\n",
" result.add(ptype.value);\n",
"\n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from interface import implements, Interface\n",
"\n",
"class Generalobject(Interface):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from interface import implements, Interface\n",
"import enum\n",
"import GeneralType\n",
"\n",
"class Generalobjects(Interface):\n",
" class PyObject(enum.Enum):\n",
" Relation = \"MemoryRelation.class)\" \n",
" Tuple = \"MemoryTuple.class\" \n",
" GeneralType = \"GeneralType.class\"\n",
" "
]
}
],
"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
}