-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
- Loading branch information
Showing
19 changed files
with
242 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Remove top level module variables from the stdlib sys module.\n", | ||
"This is based of the types detected on the sys module in a fimrware stub for esp32 micropython\n", | ||
"\n", | ||
"the approach is very simple, we just comment the top level variables from the sys module\n", | ||
"this assumes that all defenitions are on a single line, which may or may not be true\n", | ||
"\n", | ||
"A more mature approach would use cstlib to parse the module and remove the variables\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from typing import Set\n", | ||
"import re\n", | ||
"from pathlib import Path\n", | ||
"\n", | ||
"RE_VAR_TYPE = r\"[a-zA-Z0-9_]*\\s*: \"\n", | ||
"RE_VAR_ASSIGN = r\"[a-zA-Z0-9_]*\\s*= \"\n", | ||
"\n", | ||
"\n", | ||
"def read_stubfile_toplevel(stubfile: Path) -> Set[str]:\n", | ||
" # read the stubfile into a list of lines\n", | ||
" with open(stubfile, \"r\") as f:\n", | ||
" lines = f.readlines()\n", | ||
" # remove the newlines\n", | ||
" lines = [line.rstrip(\"\\n\") for line in lines]\n", | ||
"\n", | ||
" # # find all lines that start with the regex \"def [a-zA-Z0-9_]*\\(\"\n", | ||
" # deflines = []\n", | ||
" # for line in lines:\n", | ||
" # if re.match(r\"def [a-zA-Z0-9_]*\\s*\\(\", line):\n", | ||
" # deflines.append(line)\n", | ||
"\n", | ||
" # # find all lines that start with the regex \"class [a-zA-Z0-9_]*\\(\"\n", | ||
" # classlines = []\n", | ||
" # for line in lines:\n", | ||
" # if re.match(r\"class [a-zA-Z0-9_]*\\s*\\(\", line):\n", | ||
" # classlines.append(line)\n", | ||
"\n", | ||
" # find all lines that start with the regex \"[a-zA-Z0-9_]*: \"\n", | ||
" typelines = []\n", | ||
" for line in lines:\n", | ||
" if re.match(RE_VAR_TYPE, line):\n", | ||
" typelines.append(line)\n", | ||
"\n", | ||
" # find all lines that start with the regex \"[a-zA-Z0-9_]* = \"\n", | ||
" varlines = []\n", | ||
" for line in lines:\n", | ||
" if re.match(RE_VAR_ASSIGN, line):\n", | ||
" varlines.append(line)\n", | ||
"\n", | ||
" toplevel_vars = set()\n", | ||
" toplevel_vars = toplevel_vars.union([v.split(\":\")[0].strip() for v in typelines])\n", | ||
" toplevel_vars = toplevel_vars.union([v.split(\"=\")[0].strip() for v in varlines])\n", | ||
" return toplevel_vars" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def patch_stdlib_toplevel(stubfile: Path, toplevel_vars: Set[str]) -> int:\n", | ||
" # read the stubfile into a list of lines\n", | ||
" with open(stubfile, \"r\") as f:\n", | ||
" lines = f.readlines()\n", | ||
" # remove the newlines\n", | ||
" updated = 0\n", | ||
" for i, line in enumerate(lines):\n", | ||
" # comment out the lines that are not toplevel vars\n", | ||
" if re.match(RE_VAR_TYPE, line):\n", | ||
" varname = line.split(\":\")[0].strip()\n", | ||
" if not varname in toplevel_vars and varname[0] != \"_\":\n", | ||
" lines[i] = \"# \" + line\n", | ||
" updated += 1\n", | ||
" elif re.match(RE_VAR_ASSIGN, line):\n", | ||
" varname = line.split(\"=\")[0].strip()\n", | ||
" if not varname in toplevel_vars and varname[0] != \"_\":\n", | ||
" lines[i] = \"# \" + line\n", | ||
" updated += 1\n", | ||
"\n", | ||
" # write the stubfile back\n", | ||
" with open(stubfile, \"w\") as f:\n", | ||
" f.writelines(lines)\n", | ||
"\n", | ||
" return updated" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'stderr', 'argv', 'implementation', 'ps2', 'stdin', 'stdout', 'platform', 'maxsize', 'byteorder', 'ps1', 'modules', 'version', 'version_info', 'path'}\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"27" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"stubfile = \"C:\\\\develop\\\\MyPython\\\\micropython-stubs\\\\publish\\\\micropython-latest-esp32-stubs\\\\sys.pyi\"\n", | ||
"stdlib_stub = Path(\n", | ||
" \"C:\\\\develop\\\\MyPython\\\\micropython-stubs\\\\publish\\\\micropython-stdlib-stubs\\\\stdlib\\\\sys\\\\__init__.pyi\"\n", | ||
")\n", | ||
"toplevel_vars = read_stubfile_toplevel(stubfile)\n", | ||
"print(toplevel_vars)\n", | ||
"\n", | ||
"patch_stdlib_toplevel(stdlib_stub, toplevel_vars)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv", | ||
"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.11.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
#type: ignore | ||
# currently the webrepl module is excluded from creatstubs | ||
# https://github.com/Josverl/micropython-stubber/blob/91394e70a168cfea954f0137faf38d769ae372e8/src/stubber/update_module_list.py#L91 | ||
# # https://github.com/Josverl/micropython-stubber/blob/91394e70a168cfea954f0137faf38d769ae372e8/src/stubber/update_module_list.py#L91 | ||
|
||
import webrepl | ||
# https://docs.micropython.org/en/latest/esp32/quickref.html#webrepl-web-browser-interactive-prompt | ||
# | ||
import webrepl_setup | ||
# import webrepl | ||
# # https://docs.micropython.org/en/latest/esp32/quickref.html#webrepl-web-browser-interactive-prompt | ||
# # | ||
# import webrepl_setup | ||
|
||
# and following on-screen instructions. After reboot, it will be available for connection. If you disabled automatic start-up on boot, you may run configured daemon on demand using: | ||
# # and following on-screen instructions. After reboot, it will be available for connection. If you disabled automatic start-up on boot, you may run configured daemon on demand using: | ||
|
||
|
||
webrepl.start() | ||
# webrepl.start() | ||
|
||
# or, start with a specific password | ||
webrepl.start(password="mypass") | ||
# # or, start with a specific password | ||
# webrepl.start(password="mypass") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from socket import * # type: ignore | ||
from socket import * | ||
|
||
# socket.socket | ||
# Create STREAM TCP socket | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.