{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from auto_everything.io import IO\n",
    "io_ = IO()\n",
    "\n",
    "proto_string = io_.read(\"./test_protobuff_code.proto\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['service',\n",
      " 'Greeter',\n",
      " 'rpc SayHello (hello_request) returns (HelloReply);\\n'\n",
      " '    rpc SendVoice(stream Voice_Request) returns (Empty);\\n'\n",
      " '    rpc GetCurrentUsersUUID(Empty) returns (CurrentUsersUUIDReply);\\n'\n",
      " '    rpc say_hello (hello_request) returns (HelloReply);']\n",
      "\n",
      "['enum', 'UserStatus', 'OFFLINE = 0;\\n    ONLINE = 1;']\n",
      "\n",
      "['message', 'Empty', '']\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "\n",
    "\n",
    "code_block_list = re.findall(r\"(?P<type>\\w+)\\s+(?P<object_name>\\w+)\\s+\\{(?P<properties>(\\s*.*?\\s*)+)\\}\", proto_string, re.DOTALL)\n",
    "code_block_list = [\n",
    "            [string for string in one][:3]\n",
    "            for one in code_block_list\n",
    "        ]\n",
    "\n",
    "pprint(code_block_list[0])\n",
    "print()\n",
    "pprint(code_block_list[1])\n",
    "print()\n",
    "pprint(code_block_list[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['service',\n",
      "  'Greeter',\n",
      "  'rpc SayHello (hello_request) returns (HelloReply);\\n'\n",
      "  '    rpc SendVoice(stream Voice_Request) returns (Empty);\\n'\n",
      "  '    rpc GetCurrentUsersUUID(Empty) returns (CurrentUsersUUIDReply);\\n'\n",
      "  '    rpc say_hello (hello_request) returns (HelloReply);'],\n",
      " ['enum', 'UserStatus', [('', 'string', 'OFFLINE'), ('', 'string', 'ONLINE')]],\n",
      " ['message', 'Empty', []],\n",
      " ['message',\n",
      "  'hello_request',\n",
      "  [('', 'string', 'name'), ('', 'UserStatus', 'user_status')]],\n",
      " ['message', 'HelloReply', [('', 'string', 'message')]],\n",
      " ['message',\n",
      "  'Voice_Request',\n",
      "  [('', 'string', 'uuid'),\n",
      "   ('', 'int64', 'timestamp'),\n",
      "   ('', 'string', 'voice')]],\n",
      " ['message', 'VoiceReply', [('', 'string', 'error'), ('', 'bool', 'success')]],\n",
      " ['message', 'CurrentUsersUUIDReply', [('repeated', 'string', 'uuid')]]]\n"
     ]
    }
   ],
   "source": [
    "from typing import Any\n",
    "\n",
    "\n",
    "new_parsed_object_list: list[Any] = []\n",
    "for one in code_block_list.copy():\n",
    "    if len(one) == 3:\n",
    "        type_ = one[0]\n",
    "        class_name = one[1]\n",
    "        content = one[2]\n",
    "\n",
    "        if (type_ == \"service\"):\n",
    "            new_parsed_object_list.append([type_.strip(), class_name.strip(), content])\n",
    "        else:\n",
    "            property_text = \" \" + content.strip()\n",
    "            if len(property_text) == 0:\n",
    "                continue\n",
    "\n",
    "            property_list = []\n",
    "            if (type_ == \"message\"):\n",
    "                property_list = re.findall(r\"(?P<feature>\\w*)\\s+(?P<type>\\w+)\\s+(?P<property>\\w+)\\s+=\\s+\\d+;\", property_text)\n",
    "                #print(property_list)\n",
    "            elif (type_ == \"enum\"):\n",
    "                property_list = re.findall(r\"(?P<property>\\w+)\\s+=\\s+\\d+;\", property_text)\n",
    "                property_list = [('','string', one) for one in property_list]\n",
    "                #print(property_list)\n",
    "\n",
    "            new_parsed_object_list.append([type_.strip(), class_name.strip(), property_list])\n",
    "\n",
    "        #print(new_parsed_object_list[-1])\n",
    "\n",
    "pprint(new_parsed_object_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "class_name_list = [one[1].strip() for one in new_parsed_object_list]\n",
    "for one in class_name_list:\n",
    "    if class_name_list.count(one) > 1:\n",
    "        raise Exception(\"You must make sure there has no duplicated class/message name.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"UserStatus\": {\n",
      "        \"OFFLINE\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"OFFLINE\",\n",
      "            \"feature\": \"\"\n",
      "        },\n",
      "        \"ONLINE\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"ONLINE\",\n",
      "            \"feature\": \"\"\n",
      "        }\n",
      "    },\n",
      "    \"Empty\": {},\n",
      "    \"hello_request\": {\n",
      "        \"name\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"name\",\n",
      "            \"feature\": \"\"\n",
      "        },\n",
      "        \"user_status\": {\n",
      "            \"type\": \"UserStatus\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"user_status\",\n",
      "            \"feature\": \"\"\n",
      "        }\n",
      "    },\n",
      "    \"HelloReply\": {\n",
      "        \"message\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"message\",\n",
      "            \"feature\": \"\"\n",
      "        }\n",
      "    },\n",
      "    \"Voice_Request\": {\n",
      "        \"uuid\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"uuid\",\n",
      "            \"feature\": \"\"\n",
      "        },\n",
      "        \"timestamp\": {\n",
      "            \"type\": \"int64\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"timestamp\",\n",
      "            \"feature\": \"\"\n",
      "        },\n",
      "        \"voice\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"voice\",\n",
      "            \"feature\": \"\"\n",
      "        }\n",
      "    },\n",
      "    \"VoiceReply\": {\n",
      "        \"error\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"error\",\n",
      "            \"feature\": \"\"\n",
      "        },\n",
      "        \"success\": {\n",
      "            \"type\": \"bool\",\n",
      "            \"is_list\": false,\n",
      "            \"name\": \"success\",\n",
      "            \"feature\": \"\"\n",
      "        }\n",
      "    },\n",
      "    \"CurrentUsersUUIDReply\": {\n",
      "        \"uuid\": {\n",
      "            \"type\": \"string\",\n",
      "            \"is_list\": true,\n",
      "            \"name\": \"uuid\",\n",
      "            \"feature\": \"repeated\"\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "defination_tree = {}\n",
    "for one in new_parsed_object_list.copy():\n",
    "    code_block_type = one[0].strip()\n",
    "    class_name = one[1].strip()\n",
    "    variable_list = one[2]\n",
    "\n",
    "    if code_block_type == \"service\":\n",
    "        continue\n",
    "\n",
    "    name_list = [one[2] for one in variable_list]\n",
    "    for one in name_list:\n",
    "        if name_list.count(one) > 1:\n",
    "            raise Exception(\"You must make sure there has no duplicated variable name.\")\n",
    "\n",
    "    defination_tree[class_name] = {}\n",
    "    for one_variable in variable_list:\n",
    "        feature = one_variable[0]\n",
    "        type = one_variable[1]\n",
    "        name = one_variable[2]\n",
    "\n",
    "        # code_block_type_for_this_variable = code_block_type\n",
    "        # for one in new_parsed_object_list.copy():\n",
    "        #     temp_code_block_type = one[0].strip()\n",
    "        #     temp_class_name = one[1].strip()\n",
    "        #     if temp_code_block_type != \"enum\":\n",
    "        #         continue\n",
    "        #     if type == temp_class_name:\n",
    "        #         code_block_type_for_this_variable = temp_code_block_type\n",
    "        #         break\n",
    "\n",
    "        defination_tree[class_name][name] = {\n",
    "            # \"code_block_type\": code_block_type_for_this_variable,\n",
    "            \"type\": type, \n",
    "            \"is_list\": True if feature == \"repeated\" else False,\n",
    "            \"name\": name,\n",
    "            \"feature\": feature\n",
    "        }\n",
    "\n",
    "print(json.dumps(defination_tree, indent=4, sort_keys=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, './build/test_protobuff_code.py')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import test_protobuff_code \n",
    "hello = test_protobuff_code.HelloReply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "hello.from_dict({\n",
    "    'message': \"hi\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HelloReply(message='hi')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.8 64-bit",
   "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.10.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
