{ "cells": [ { "cell_type": "markdown", "id": "f544e4e0-4968-4de0-b33c-35b94aa89da5", "metadata": {}, "source": [ "# Schema Updater\n", "\n", "> This is not a demonstration of functionality, but an iterative notebook for updating the ipydrawio JSON schema, when it changes.\n", "> The specific version of the files at play are found in `dodo.py`" ] }, { "cell_type": "code", "execution_count": null, "id": "b115e4b7-61c2-4baf-b841-c383db1edbca", "metadata": {}, "outputs": [], "source": [ "import json\n", "import re\n", "import subprocess\n", "import tempfile\n", "from copy import deepcopy\n", "from pathlib import Path\n", "\n", "import IPython\n", "import lxml.etree as ET\n", "import pandas" ] }, { "cell_type": "code", "execution_count": null, "id": "e10946b0-d498-4d14-8cd0-da9e7c7b830f", "metadata": {}, "outputs": [], "source": [ "ROOT = Path(\"../..\").resolve()\n", "assert ROOT.name == \"ipydrawio\"\n", "assert (ROOT / \".git\").exists()" ] }, { "cell_type": "markdown", "id": "96c17607-554d-4b50-b506-55bd51497604", "metadata": {}, "source": [ "The user settings schema in lab is the source of truth for many elements of the " ] }, { "cell_type": "code", "execution_count": null, "id": "e035378e-3b3c-4eb1-9fa4-ccc5d0ab83ed", "metadata": {}, "outputs": [], "source": [ "SCHEMA_PATH = ROOT / \"packages/ipydrawio/schema/plugin.json\"\n", "SCHEMA = json.loads(SCHEMA_PATH.read_text(encoding=\"utf-8\"))\n", "IPython.display.JSON(SCHEMA)" ] }, { "cell_type": "markdown", "id": "eb44b16b-98c1-4baf-ada3-7caaecfa13ac", "metadata": {}, "source": [ "This is a continuous check as to whether git is happy" ] }, { "cell_type": "code", "execution_count": null, "id": "11c57ef8-70b3-4f7e-bc47-a7fb821dd304", "metadata": {}, "outputs": [], "source": [ "SCHEMA_PATH.write_text(json.dumps(SCHEMA, indent=2, sort_keys=True), encoding=\"utf-8\")\n", "!jlpm prettier --write {ROOT}/packages/ipydrawio/schema/plugin.json\n", "!git diff {ROOT}/packages/ipydrawio/schema/plugin.json || echo 'no change'" ] }, { "cell_type": "code", "execution_count": null, "id": "cea18c8c-63ea-476c-bfc9-b586c651e20f", "metadata": {}, "outputs": [], "source": [ "paths = [*(ROOT / \"build/fetched\").glob(\"*.html\")]\n", "paths" ] }, { "cell_type": "markdown", "id": "b607c248-f4fb-46a9-811d-611126ca5508", "metadata": {}, "source": [ "# The URL Params" ] }, { "cell_type": "code", "execution_count": null, "id": "a45709f3-de69-4d57-bc63-2e04f14d13ac", "metadata": {}, "outputs": [], "source": [ "h_url_params = paths[0]\n", "IPython.display.IFrame(\n", " f\"/files/{h_url_params}\",\n", " height='600px\" style=\"height: 100%;',\n", " width=\"100%\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "2bd5c728-346b-451d-88ab-3f4a0b29de69", "metadata": {}, "outputs": [], "source": [ "x_url_params = ET.HTML(h_url_params.read_bytes())\n", "x_url_params" ] }, { "cell_type": "code", "execution_count": null, "id": "4c59d50d-dd52-4dab-a790-0d7543f9db63", "metadata": {}, "outputs": [], "source": [ "li_elements = x_url_params.xpath(\"//li/code[contains(text(), '=')]\")\n", "li = li_elements[0]" ] }, { "cell_type": "markdown", "id": "42a22e6f-c4af-4430-895b-9a5b22c01190", "metadata": {}, "source": [ "We're interested in the context around the definition." ] }, { "cell_type": "code", "execution_count": null, "id": "77a3c450-0637-4141-b3e3-660f5a389899", "metadata": {}, "outputs": [], "source": [ "ET.tostring(li.getparent(), pretty_print=True, encoding=str)" ] }, { "cell_type": "markdown", "id": "6248c5ec-4fb2-4dc9-9931-93e505eb1dc6", "metadata": {}, "source": [ "# Clean up the description\n", "\n", "The raw XML is not very nice to embed in JSON. So many double quotes!" ] }, { "cell_type": "code", "execution_count": null, "id": "32a00d47-2a0a-421e-9d06-35a6389c57bb", "metadata": {}, "outputs": [], "source": [ "def one_md_desc(li):\n", " with tempfile.TemporaryDirectory() as td:\n", " tdp = Path(td)\n", " html = tdp / \"test1.html\"\n", " md = tdp / \"test1.md\"\n", " html.write_text(\n", " f\"