Private
Public Access
0
0
Files
manual_slop/src/workspace_manager.py
T
ed b3aeaa4376 fix(post_de_cruft_iter2): fix 3 pre-existing test failures + lazy tomli_w imports
1. tier-1-unit-core::test_audit_script_exits_zero
   - audit_main_thread_imports.py failed with 3 heavy top-level imports
   - Made tomli_w lazy in src/personas.py, src/tool_presets.py, src/workspace_manager.py
   - Made 'from scripts import py_struct_tools' lazy inside src/mcp_client.py:dispatch()
   - Audit now exits 0 (28 files in main-thread import graph, no heavy top-level imports)

2. tier-2-mock-app-headless::test_status_endpoint_authorized
   - /status endpoint goes through _api_status() which returns controller.ai_status (default 'idle'),
     not the literal 'ok' string the test expected
   - Updated test to expect 'idle' (the actual ai_status default for a fresh controller)

3. tier-3-live_gui::test_auto_switch_sim
   - _capture_workspace_profile() in src/gui_2.py referenced 'WorkspaceProfile' as a bare name,
     but the module had only 'from src import workspace_manager' (the module, not the class)
   - Added 'from src.workspace_manager import WorkspaceProfile' to fix the NameError
   - Profile save/load round-trip now works; auto-switch fires Tier 3 bound profile

Additional test fixes (uncovered by full run):
- tests/test_cruft_removal.py: patch 'src.mcp_client.py_struct_tools' no longer works
  (lazy import means the attribute doesn't exist). Patched 'scripts.py_struct_tools.py_remove_def'
  and '.py_move_def' directly at the source module.
- tests/test_command_palette_sim.py: 'from src.command_palette' was deleted in
  module_taxonomy_refactor; updated to 'from src.commands' (which now hosts _close_palette,
  _execute, and Command after the merge).

Production fix:
- src/presets.py:save_preset now raises ValueError when scope='project' but
  project_root is None (fail-fast per error_handling.md, prevents silent
  write to '.').

Type registry regenerated to reflect new line numbers.
2026-06-27 10:17:51 -04:00

111 lines
3.4 KiB
Python

import tomllib
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Any, Optional, Union
from src import paths
from src.type_aliases import Metadata
@dataclass
class WorkspaceProfile:
name: str
ini_content: str
show_windows: Dict[str, bool]
panel_states: Metadata
def to_dict(self) -> Metadata:
return {
"ini_content": self.ini_content,
"show_windows": self.show_windows,
"panel_states": self.panel_states,
}
@classmethod
def from_dict(cls, name: str, data: Metadata) -> "WorkspaceProfile":
return cls(
name = name,
ini_content = data.get("ini_content", ""),
show_windows = data.get("show_windows", {}),
panel_states = data.get("panel_states", {}),
)
class WorkspaceManager:
"""Manages Workspace profiles across global and project-specific files."""
def __init__(self, project_root: Optional[Union[str, Path]] = None):
if project_root:
self.project_root = Path(project_root).resolve()
else:
self.project_root = None
def _get_path(self, scope: str) -> Path:
if scope == "global":
return paths.get_global_workspace_profiles_path()
elif scope == "project":
if not self.project_root:
raise ValueError("Project root is not set, cannot resolve project scope.")
return paths.get_project_workspace_profiles_path(self.project_root)
else:
raise ValueError("Invalid scope, must be 'global' or 'project'")
def load_all_profiles(self) -> Dict[str, WorkspaceProfile]:
"""
Merges global and project profiles into a single dictionary.
[C: tests/test_workspace_manager.py:test_delete_profile, tests/test_workspace_manager.py:test_load_all_profiles_merged, tests/test_workspace_manager.py:test_save_profile_global_and_project]
"""
profiles = {}
global_path = paths.get_global_workspace_profiles_path()
global_data = self._load_file(global_path)
for name, data in global_data.get("profiles", {}).items():
profiles[name] = WorkspaceProfile.from_dict(name, data)
if self.project_root:
project_path = paths.get_project_workspace_profiles_path(self.project_root)
project_data = self._load_file(project_path)
for name, data in project_data.get("profiles", {}).items():
profiles[name] = WorkspaceProfile.from_dict(name, data)
return profiles
def save_profile(self, profile: WorkspaceProfile, scope: str = "project") -> None:
"""
[C: tests/test_workspace_manager.py:test_delete_profile, tests/test_workspace_manager.py:test_save_profile_global_and_project]
"""
path = self._get_path(scope)
data = self._load_file(path)
if "profiles" not in data:
data["profiles"] = {}
data["profiles"][profile.name] = profile.to_dict()
self._save_file(path, data)
def delete_profile(self, name: str, scope: str = "project") -> None:
"""
[C: tests/test_workspace_manager.py:test_delete_profile]
"""
path = self._get_path(scope)
data = self._load_file(path)
if "profiles" in data and name in data["profiles"]:
del data["profiles"][name]
self._save_file(path, data)
def _load_file(self, path: Path) -> Dict[str, Any]:
if not path.exists():
return {}
try:
with open(path, "rb") as f:
return tomllib.load(f)
except Exception:
return {}
def _save_file(self, path: Path, data: Dict[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
# tomli_w is loaded on-demand to keep the main-thread import graph lean.
import tomli_w
with open(path, "wb") as f:
tomli_w.dump(data, f)