codex-safe-switch 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ __version__ = "1.0.0"
@@ -0,0 +1,69 @@
1
+ """TOML provider-section helper for codex-safe-switch.
2
+
3
+ Defines which keys belong to a "provider" (and thus get swapped between profiles)
4
+ vs which are local state (preserved across switches).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from pathlib import Path
10
+
11
+ import tomlkit
12
+
13
+ # Top-level scalar keys that belong to a provider profile.
14
+ PROVIDER_TOP_KEYS = frozenset({
15
+ "model",
16
+ "model_provider",
17
+ "model_reasoning_effort",
18
+ "model_reasoning_summary",
19
+ "model_verbosity",
20
+ "wire_api",
21
+ "disable_response_storage",
22
+ "preferred_auth_method",
23
+ })
24
+
25
+ # Top-level tables that belong to a provider profile.
26
+ PROVIDER_TABLES = frozenset({"model_providers"})
27
+
28
+
29
+ def load(path: Path):
30
+ if not path.exists() or path.stat().st_size == 0:
31
+ return tomlkit.document()
32
+ return tomlkit.parse(path.read_text())
33
+
34
+
35
+ def extract(src: Path, dst: Path) -> None:
36
+ """Write the provider-related slice of src into dst as a standalone toml."""
37
+ doc = load(src)
38
+ out = tomlkit.document()
39
+ for k in PROVIDER_TOP_KEYS:
40
+ if k in doc:
41
+ out[k] = doc[k]
42
+ for t in PROVIDER_TABLES:
43
+ if t in doc:
44
+ out[t] = doc[t]
45
+ dst.write_text(tomlkit.dumps(out))
46
+
47
+
48
+ def merge(current: Path, profile_provider: Path, out_path: Path) -> None:
49
+ """Strip provider section from current config, then append profile's provider.toml.
50
+
51
+ Result keeps all local state (projects.*, tui.*, plugins.*, marketplaces.*, ...)
52
+ and adopts the profile's provider settings.
53
+ """
54
+ current_doc = load(current)
55
+ profile_doc = load(profile_provider)
56
+
57
+ for k in PROVIDER_TOP_KEYS:
58
+ if k in current_doc:
59
+ del current_doc[k]
60
+ for t in PROVIDER_TABLES:
61
+ if t in current_doc:
62
+ del current_doc[t]
63
+
64
+ for k in profile_doc:
65
+ # tomlkit Document doesn't allow re-parenting a value owned by another doc
66
+ # cleanly, so round-trip through dumps/parse for safety.
67
+ current_doc[k] = tomlkit.parse(tomlkit.dumps({k: profile_doc[k]}))[k]
68
+
69
+ out_path.write_text(tomlkit.dumps(current_doc))