6b0668f1a9
The migration commit (8f11340b) replaced 'from src.models import X'
with 'from src.<destination> import X' in EVERY file including the
destination files themselves. This created self-imports like
'from src.external_editor import ExternalEditorConfig' in
src/external_editor.py (which defines ExternalEditorConfig locally).
This fix removes the spurious self-imports from the 5 destination
files that were affected:
- src/external_editor.py (3 lines removed: 1 top-level + 2 in
function bodies that my migration
missed on the first pass)
- src/personas.py (1 line removed)
- src/tool_bias.py (1 line removed)
- src/tool_presets.py (1 line removed)
- src/workspace_manager.py (1 line removed)
The migration in non-destination files is correct and unchanged.
After this fix, the next merge of origin/tier2/module_taxonomy_refactor_20260627
(bringing in the v2 SHIPPED work) will not conflict on these files
because the self-imports are gone; the merge will apply v2's class
definitions cleanly.
The fix was performed by
scripts/tier2/artifacts/post_module_taxonomy_de_cruft_20260627/fix_self_imports.py
which removes 'from src.<module> import X' lines from files where
<module> matches the file's destination module name.
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
import tomllib
|
|
import tomli_w
|
|
|
|
from pathlib import Path
|
|
from typing import Dict, Any, Optional, Union
|
|
|
|
from src import paths
|
|
|
|
|
|
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)
|
|
with open(path, "wb") as f:
|
|
tomli_w.dump(data, f) |