updating and refactoring codebase for improved performance and maintainability

This commit is contained in:
2026-05-03 17:29:32 -07:00
parent b7d3dc5f2d
commit 77cab1bd27
17 changed files with 590 additions and 294 deletions
+38 -11
View File
@@ -10,18 +10,45 @@ import re
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from SYS.logger import debug
# Prompt-toolkit lexer types are optional at import time; fall back to lightweight
# stubs if prompt_toolkit is not available so imports remain safe for testing.
try:
from prompt_toolkit.document import Document
from prompt_toolkit.lexers import Lexer as _PTK_Lexer
except Exception: # pragma: no cover - optional dependency
Document = object # type: ignore
# Fallback to a simple object when prompt_toolkit is not available
_PTK_Lexer = object # type: ignore
# Prompt-toolkit lexer types are optional and expensive (~300ms). Use find_spec
# to detect availability without importing, then lazy-load on first use.
import importlib.util as _importlib_util
_PTK_AVAILABLE: bool = _importlib_util.find_spec("prompt_toolkit") is not None
# Expose a stable name used by the rest of the module
Lexer = _PTK_Lexer
_ptk_Document: Any = None
_ptk_Lexer: Any = None
def _get_ptk_Document() -> Any:
global _ptk_Document
if _ptk_Document is None:
if _PTK_AVAILABLE:
from prompt_toolkit.document import Document as _Doc
_ptk_Document = _Doc
else:
_ptk_Document = object
return _ptk_Document
def _get_ptk_Lexer() -> Any:
global _ptk_Lexer
if _ptk_Lexer is None:
if _PTK_AVAILABLE:
from prompt_toolkit.lexers import Lexer as _Lex
_ptk_Lexer = _Lex
else:
_ptk_Lexer = object
return _ptk_Lexer
# Stable aliases: these resolve lazily the first time they are accessed.
# Code that does `isinstance(x, Document)` or `class Foo(Lexer)` at class-body
# time needs the real object, so we keep module-level names that proxy to the
# lazy getters via __getattr__ on the module. Callers that reference
# Document/Lexer INSIDE functions will always get the real class.
# Populate the module-level names now so that class bodies below can inherit.
Document: Any = _get_ptk_Document()
Lexer: Any = _get_ptk_Lexer()
# Pre-compiled regexes for the lexer (avoid recompiling on every call)
TOKEN_PATTERN = re.compile(