testsAndMisc/python_pkg/vscode_optimizer/_optimize.py
Krzysztof kuhy Rudnicki 377a352d48
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
vscode_optimizer: split _optimize.py 498 -> 100 + 5 modules
The last python file over the 250-line cap. Now that it sits at 100% line
and branch coverage, split along its natural seams:

  _types.py     48  the three dataclasses, so nothing imports a sibling
                    merely to name a type (which would have made a cycle)
  _render.py    69  the ANSI palette and the printers
  _config_io.py 104 variant discovery, JSONC parsing, reads and writes
  _hardware.py  106 _run and the four probes
  _plan.py      156 the pure hardware -> proposed-changes functions
  _optimize.py  100 the CLI entry: _apply_variant and main

Definitions were moved by line range rather than retyped. A throwaway
ast.dump identity check against HEAD confirms all 26 top-level definitions
are unchanged - formatting-blind, logic-sensitive.

The first attempt raised NameError: name '_BO' is not defined at runtime;
_BO was used by main() but missing from the _render import list. Rather than
fix that one failure alone, an AST scan over all six modules for names used
but not bound confirmed nothing else was missing.

Test imports now name the owning module; no re-export barrels. One patch
target genuinely moved - the guard-rail test patches _config_io.Path, since
that is the namespace resolving the name when _discover_variants is called
directly rather than through main().

Verified: same 98 tests pass with every module at 100% line and branch;
python3 -m python_pkg.vscode_optimizer --dry-run reproduces the same
18-change plan and writes nothing; repo-wide pytest 1846 passed at 100%;
over-cap files 36 -> 35.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 13:15:50 +02:00

104 lines
2.9 KiB
Python

"""Auto-optimize VS Code settings based on detected hardware.
Run as ``python3 -m python_pkg.vscode_optimizer [--dry-run] [--yes]``.
"""
from __future__ import annotations
import argparse
import sys
from typing import TYPE_CHECKING
from python_pkg.vscode_optimizer._config_io import (
_backup,
_discover_variants,
_read_flags,
_read_settings,
_write_flags,
_write_settings,
)
from python_pkg.vscode_optimizer._hardware import _detect_hardware
from python_pkg.vscode_optimizer._plan import _compute_opts, _gpu_flags
from python_pkg.vscode_optimizer._render import (
_BO,
_G,
_R,
_Y,
_hdr,
_out,
_show_hw,
_show_plan,
)
if TYPE_CHECKING:
from python_pkg.vscode_optimizer._types import _Hw, _Variant
def _apply_variant(
v: _Variant,
hw: _Hw,
ideal_flags: list[str],
*,
dry_run: bool,
auto_yes: bool,
) -> None:
"""Compute and apply optimizations for a single variant."""
_hdr(f"Optimizing: {v.name}")
current = _read_settings(v.settings)
opts = _compute_opts(hw, current)
old_flags = _read_flags(v.flags)
merged = list(dict.fromkeys(old_flags + ideal_flags))
_show_plan(opts, ideal_flags, old_flags)
flag_changed = merged != old_flags
if not opts and not flag_changed:
_out(f"\n {_G}Nothing to do for {v.name}.{_R}")
return
if dry_run:
_out(f"\n {_Y}(dry-run) No files modified.{_R}")
return
if not auto_yes:
ans = input(f"\n Apply changes to {v.name}? [y/N] ").strip()
if ans.lower() not in ("y", "yes"):
_out(" Skipped.")
return
if opts:
bak = _backup(v.settings)
if bak:
_out(f" Backup: {bak}")
_write_settings(v.settings, current, opts)
_out(f" {_G}\u2713 settings.json updated{_R}")
if flag_changed:
bak = _backup(v.flags)
if bak:
_out(f" Backup: {bak}")
_write_flags(v.flags, merged)
_out(f" {_G}\u2713 {v.flags.name} updated{_R}")
def main() -> None:
"""Entry point: detect hardware, compute optimizations, and apply."""
ap = argparse.ArgumentParser(
description="Auto-optimize VS Code for current hardware."
)
ap.add_argument("--dry-run", action="store_true", help="Preview without writing.")
ap.add_argument("--yes", "-y", action="store_true", help="Skip confirmation.")
args = ap.parse_args()
hw = _detect_hardware()
_show_hw(hw)
variants = _discover_variants()
if not variants:
_out(f"\n{_Y}No VS Code installation found.{_R}")
sys.exit(1)
_hdr("VS Code Installations")
for v in variants:
_out(f" {_G}\u2022{_R} {v.name} ({v.settings})")
ideal = _gpu_flags(hw)
for v in variants:
_apply_variant(v, hw, ideal, dry_run=args.dry_run, auto_yes=args.yes)
_hdr("Done")
_out(f" {_BO}Restart VS Code{_R} to apply the changes.\n")
if __name__ == "__main__":
main()