Skip to content

Instantly share code, notes, and snippets.

@kubinka0505
Last active August 2, 2021 20:25
Show Gist options
  • Save kubinka0505/42723812ac6d4ed0b1f80e7a49481f70 to your computer and use it in GitHub Desktop.
Save kubinka0505/42723812ac6d4ed0b1f80e7a49481f70 to your computer and use it in GitHub Desktop.
Inkscape HTML Nodes Delimiter (Google Colab)
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Inkscape HTML Nodes Delimiter",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "AvL9yM5_LT3w"
},
"source": [
"# [Original Python File](https://gist.github.com/kubinka0505/d1d80abf6950d5a93e8582244965b93a) 🐍"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RFzCKZqbLV72",
"cellView": "form"
},
"source": [
"#@title ## <font color=\"red\">❰</font> Open webpage `HTML` file\n",
"\n",
"from zipfile import *\n",
"from shutil import rmtree\n",
"from sys import *; del path\n",
"from os import *; del open\n",
"from google.colab import files\n",
"\n",
"CWD = \"/content/\"\n",
"rmtree(CWD)\n",
"mkdir(CWD)\n",
"chdir(CWD)\n",
"\n",
"#---#\n",
"\n",
"__author__\t= \"kubinka0505\"\n",
"__copyright__\t= __author__\n",
"__credits__\t= __author__\n",
"__version__\t= \"1.0\"\n",
"__date__\t= \"02.08.2021\"\n",
"__status__\t= \"Mature\"\n",
"__license__\t= \"Apache 2.0\"\n",
"\n",
"#---#\n",
"\n",
"def Folder_Size():\n",
"\t\"\"\"Calculates folder size recursively\"\"\"\n",
"\tSize = 0\n",
"\tfor Root, Dirs, Files in walk(\".\"):\n",
"\t\tfor File in Files:\n",
"\t\t\tPath = path.join(Root, File)\n",
"\t\t\tif not path.islink(Path):\n",
"\t\t\t\tSize += path.getsize(Path)\n",
"\treturn Size\n",
"\n",
"def File_Size(Bytes: float) -> str:\n",
"\t\"\"\"Returns human-readable file size\"\"\"\n",
"\tfor Unit in [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\"]:\n",
"\t\tif Bytes < 1024.: break\n",
"\t\tBytes /= 1024.\n",
"\treturn \"{0} {1}\".format(round(Bytes, 2), Unit)\n",
"\n",
"#---#\n",
"\n",
"try:\n",
"\ttry:\n",
"\t\tfiles.upload()\n",
"\texcept KeyboardInterrupt:\n",
"\t\traise IndexError\n",
"\tFile = next(walk(\".\"))[2][0]\n",
"\tif File:\n",
"\t\tFile = open(path.abspath(File), encoding = \"utf-8\")\n",
"\telse:\n",
"\t\traise IndexError\n",
"except IndexError:\n",
"\texit(\"No file given!\")\n",
"\n",
"Folder_Name = File.name.split(\".\")[0] + \"_Nodes\"\n",
"\n",
"#---#\n",
"\n",
"Content\t= File.read()\n",
"Content\t= Content.replace(\"\\t\", \"\")\n",
"Begin\t= Content.find(\"ctx.moveTo(\")\n",
"Content\t= Content[Begin:].split(\"ctx.fill(\")[0].strip().split(\"\\n\")\n",
"\n",
"#---#\n",
"\n",
"Formats = (\"HTM\", \"HTML\")\n",
"if not File.name.upper().endswith(Formats):\n",
"\ttry:\n",
"\t\traise OSError(\"Only single simple webpages format files are supported ({0})\".format(\n",
"\t\t\t\", \".join(Formats).strip()\n",
"\t\t)\n",
"\t)\n",
"\texcept Exception as Error:\n",
"\t\texit(\"{0}: {1}\".format(Error.__class__.__name__, Error))\n",
"\n",
"for Line in Content:\n",
"\tif \"ctx.bezier\" in Line:\n",
"\t\ttry:\n",
"\t\t\traise AttributeError(\"SVG file contains bezier curves\")\n",
"\t\texcept Exception as Error:\n",
"\t\t\texit(\"{0}: {1}\".format(Error.__class__.__name__, Error))\n",
"\n",
"try:\n",
"\tmkdir(Folder_Name)\n",
"except FileExistsError:\n",
"\tpass\n",
"chdir(Folder_Name)\n",
"\n",
"#---#\n",
"\n",
"X = \"\\n\".join([Line.split(\", \")[1].split(\");\")[0] for Line in Content])\n",
"X += \"{0}{1}{0}\".format(\"\\n\", X.split(\"\\n\")[0])\n",
"\n",
"Y = \"\\n\".join([Line.split(\"(\")[1].split(\",\")[0] for Line in Content])\n",
"Y += \"{0}{1}{0}\".format(\"\\n\", Y.split(\"\\n\")[0])\n",
"\n",
"open(\"X.txt\", \"w\", encoding = \"utf-8\").write(X)\n",
"open(\"Y.txt\", \"w\", encoding = \"utf-8\").write(Y)\n",
"\n",
"#---#\n",
"\n",
"print(\"\\nSuccessfully saved {0} nodes ({1})\".format(\n",
"\tlen([Line for Line in Content]),\n",
"\tFile_Size(Folder_Size())\n",
"\t)\n",
")\n",
"\n",
"#---#\n",
"\n",
"print(\"Creating archive...\")\n",
"Archive_Name = path.abspath(Folder_Name + \".zip\")\n",
"Archive = ZipFile(Archive_Name, \"w\", ZIP_LZMA)\n",
"[Archive.write(File) for File in next(walk(\".\"))[2]]\n",
"files.download(Archive_Name)"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment