openforma 0.4.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.
- forma_cli/__init__.py +5 -0
- forma_cli/app.py +1139 -0
- forma_cli/config.py +112 -0
- forma_cli/credentials.py +141 -0
- forma_cli/local.py +562 -0
- forma_cli/metadata_api.py +138 -0
- forma_cli/project_artifacts.py +182 -0
- forma_cli/sdk.py +571 -0
- forma_core/__init__.py +9 -0
- forma_core/__main__.py +4 -0
- forma_core/_version.py +3 -0
- forma_core/agents/__init__.py +50 -0
- forma_core/agents/clarification.py +194 -0
- forma_core/agents/component_reconciliation.py +99 -0
- forma_core/agents/context_gathering.py +115 -0
- forma_core/agents/continuous.py +537 -0
- forma_core/agents/contracts.py +247 -0
- forma_core/agents/firecrawl_mcp.py +484 -0
- forma_core/agents/lattice.py +239 -0
- forma_core/agents/orchestrator.py +3724 -0
- forma_core/agents/pipeline.py +843 -0
- forma_core/agents/project_correction.py +83 -0
- forma_core/agents/prompt_compaction.py +428 -0
- forma_core/agents/system_architecture.py +345 -0
- forma_core/agents/video_correction.py +119 -0
- forma_core/agents/web_research_workflow.py +1256 -0
- forma_core/agents/workflows.py +176 -0
- forma_core/assets/__init__.py +1 -0
- forma_core/assets/models.py +156 -0
- forma_core/assets/project.py +17 -0
- forma_core/assets/repository.py +48 -0
- forma_core/assets/service.py +142 -0
- forma_core/assets/validation.py +35 -0
- forma_core/benchmarks/__init__.py +5 -0
- forma_core/benchmarks/ir_triad_v1.py +477 -0
- forma_core/biology/__init__.py +28 -0
- forma_core/biology/benchmark.py +80 -0
- forma_core/biology/ewart_liverchip.py +279 -0
- forma_core/biology/ir.py +25 -0
- forma_core/biology/liverbench.py +416 -0
- forma_core/biology/liverbench_assay.py +251 -0
- forma_core/biology/liverbench_baselines.py +133 -0
- forma_core/biology/liverbench_independent.py +229 -0
- forma_core/biology/liverchip_demo.py +154 -0
- forma_core/biology/liverchip_evidence.py +149 -0
- forma_core/biology/models.py +247 -0
- forma_core/biology/validation.py +72 -0
- forma_core/cad_migrations/__init__.py +6 -0
- forma_core/cad_migrations/cli.py +101 -0
- forma_core/cad_migrations/emitters.py +227 -0
- forma_core/cad_migrations/models.py +121 -0
- forma_core/cad_migrations/planner.py +68 -0
- forma_core/chemistry/__init__.py +5 -0
- forma_core/chemistry/models.py +215 -0
- forma_core/chemistry/stoichiometry.py +272 -0
- forma_core/chemistry/validation.py +179 -0
- forma_core/cli/__init__.py +1 -0
- forma_core/cli/__main__.py +4 -0
- forma_core/cli/main.py +370 -0
- forma_core/config/__init__.py +28 -0
- forma_core/config/compatibility.py +282 -0
- forma_core/config/contract.py +184 -0
- forma_core/config/environment.py +129 -0
- forma_core/config/parti.py +34 -0
- forma_core/config/runtime.py +303 -0
- forma_core/control/__init__.py +10 -0
- forma_core/control/actions.py +136 -0
- forma_core/control/adapters.py +36 -0
- forma_core/control/cli.py +49 -0
- forma_core/control/client.py +124 -0
- forma_core/control/models.py +168 -0
- forma_core/control/opencode_worker.py +80 -0
- forma_core/control/package.py +318 -0
- forma_core/control/rover_scene/__init__.py +1 -0
- forma_core/control/rover_scene/__main__.py +34 -0
- forma_core/control/rover_scene/assets.py +65 -0
- forma_core/control/rover_scene/fleet.py +214 -0
- forma_core/control/rover_scene/host.py +258 -0
- forma_core/control/rover_scene/kinematics.py +35 -0
- forma_core/control/rover_scene/machine.py +347 -0
- forma_core/control/rover_scene/robot.py +166 -0
- forma_core/control/rover_scene/scene.py +206 -0
- forma_core/control/rover_scene/view.py +80 -0
- forma_core/control/rover_scene/web/index.html +25 -0
- forma_core/control/rover_scene/web/viewer.js +148 -0
- forma_core/control/runtime.py +356 -0
- forma_core/control/so101.py +235 -0
- forma_core/control/tasks.py +287 -0
- forma_core/database.py +2362 -0
- forma_core/debug.py +314 -0
- forma_core/domains/__init__.py +7 -0
- forma_core/domains/agents.py +85 -0
- forma_core/domains/base.py +69 -0
- forma_core/domains/benchmark.py +87 -0
- forma_core/domains/cli.py +48 -0
- forma_core/domains/compatibility.py +234 -0
- forma_core/domains/composition.py +372 -0
- forma_core/domains/diagnostics.py +15 -0
- forma_core/domains/generation.py +479 -0
- forma_core/domains/interaction.py +301 -0
- forma_core/domains/planning.py +140 -0
- forma_core/domains/providers.py +214 -0
- forma_core/domains/registry.py +267 -0
- forma_core/domains/selection.py +75 -0
- forma_core/domains/storage.py +60 -0
- forma_core/domains/tools.py +180 -0
- forma_core/electronics/__init__.py +1 -0
- forma_core/electronics/models.py +84 -0
- forma_core/electronics/primitives.py +43 -0
- forma_core/electronics/rules.py +340 -0
- forma_core/external_sources.py +497 -0
- forma_core/fabricator/__init__.py +33 -0
- forma_core/fabricator/__main__.py +7 -0
- forma_core/fabricator/main.py +578 -0
- forma_core/firmware/__init__.py +60 -0
- forma_core/firmware/drivers.py +140 -0
- forma_core/firmware/examples.py +503 -0
- forma_core/firmware/generate.py +520 -0
- forma_core/firmware/models.py +34 -0
- forma_core/firmware/sil/__init__.py +5 -0
- forma_core/firmware/sil/adapter.py +71 -0
- forma_core/firmware/sil/build.py +49 -0
- forma_core/firmware/sil/native/CMakeLists.txt +9 -0
- forma_core/firmware/sil/native/bridge.cpp +112 -0
- forma_core/firmware/sil/native/controller.cpp +123 -0
- forma_core/firmware/sil/native/controller.h +49 -0
- forma_core/firmware/sil/simulator.py +139 -0
- forma_core/firmware/targets.py +111 -0
- forma_core/firmware/validation.py +130 -0
- forma_core/fluidics/__init__.py +6 -0
- forma_core/fluidics/benchmark.py +217 -0
- forma_core/fluidics/calculations.py +79 -0
- forma_core/fluidics/examples.py +54 -0
- forma_core/fluidics/ir.py +25 -0
- forma_core/fluidics/models.py +132 -0
- forma_core/fluidics/topology.py +47 -0
- forma_core/fluidics/validation.py +134 -0
- forma_core/generation.py +32 -0
- forma_core/geometry_bindings/__init__.py +20 -0
- forma_core/geometry_bindings/cad_adapter.py +80 -0
- forma_core/geometry_bindings/models.py +163 -0
- forma_core/geometry_bindings/resolution.py +340 -0
- forma_core/geometry_bindings/tools.py +125 -0
- forma_core/image_providers.py +3049 -0
- forma_core/images.py +3 -0
- forma_core/integrations/__init__.py +1 -0
- forma_core/integrations/huggingface/__init__.py +29 -0
- forma_core/integrations/huggingface/huggingface_artifacts.py +272 -0
- forma_core/jobs/__init__.py +1 -0
- forma_core/jobs/context.py +277 -0
- forma_core/jobs/continuous.py +719 -0
- forma_core/jobs/metrics.py +124 -0
- forma_core/jobs/migrations.py +161 -0
- forma_core/jobs/persistence.py +28 -0
- forma_core/jobs/repositories.py +422 -0
- forma_core/jobs/schema.py +28 -0
- forma_core/jobs/source_usage.py +230 -0
- forma_core/jobs/store.py +476 -0
- forma_core/llm.py +3 -0
- forma_core/llm_providers.py +2682 -0
- forma_core/logs.py +107 -0
- forma_core/mechanics/__init__.py +1 -0
- forma_core/mechanics/gear_benchmark.py +85 -0
- forma_core/mechanics/mechanism_benchmarks.py +345 -0
- forma_core/mechanics/models.py +72 -0
- forma_core/mechanics/primitives.py +109 -0
- forma_core/mechanics/solid_cad.py +83 -0
- forma_core/observability.py +237 -0
- forma_core/openai_streams.py +780 -0
- forma_core/opencode/__init__.py +25 -0
- forma_core/opencode/architecture.py +82 -0
- forma_core/opencode/capabilities.py +130 -0
- forma_core/opencode/models.py +419 -0
- forma_core/opencode/persistence.py +62 -0
- forma_core/opencode/public_events.py +91 -0
- forma_core/opencode/schema.py +28 -0
- forma_core/opencode/store.py +874 -0
- forma_core/persistence/__init__.py +16 -0
- forma_core/persistence/base.py +38 -0
- forma_core/persistence/images.py +598 -0
- forma_core/persistence/migrations.py +200 -0
- forma_core/persistence/models.py +439 -0
- forma_core/persistence/project_artifacts.py +341 -0
- forma_core/persistence/project_reconciliation.py +1094 -0
- forma_core/persistence/providers/__init__.py +4 -0
- forma_core/persistence/providers/sqlite.py +114 -0
- forma_core/persistence/providers/supabase.py +31 -0
- forma_core/persistence/repositories/__init__.py +9 -0
- forma_core/persistence/repositories/base.py +335 -0
- forma_core/persistence/repositories/sqlite.py +1571 -0
- forma_core/persistence/repositories/supabase.py +1576 -0
- forma_core/persistence/schema.py +176 -0
- forma_core/processes/__init__.py +5 -0
- forma_core/processes/models.py +338 -0
- forma_core/processes/validation.py +141 -0
- forma_core/project_list_cache.py +316 -0
- forma_core/prompt_continuity.py +387 -0
- forma_core/providers/__init__.py +39 -0
- forma_core/providers/base.py +28 -0
- forma_core/providers/models.py +138 -0
- forma_core/providers/openai.py +92 -0
- forma_core/providers/openai_compatible.py +190 -0
- forma_core/providers/registry.py +68 -0
- forma_core/providers/registry_utils.py +46 -0
- forma_core/py.typed +1 -0
- forma_core/runtime.py +3 -0
- forma_core/selectors.py +36 -0
- forma_core/signups/__init__.py +5 -0
- forma_core/signups/models.py +49 -0
- forma_core/terminal/__init__.py +1 -0
- forma_core/terminal/dashboard.py +475 -0
- forma_core/terminal/images.py +156 -0
- forma_core/thermal/__init__.py +5 -0
- forma_core/thermal/models.py +361 -0
- forma_core/thermal/validation.py +175 -0
- forma_core/user_integrations.py +2069 -0
- forma_core/utils.py +291 -0
- forma_core/validation.py +83 -0
- forma_core/vertex_auth.py +92 -0
- forma_core/video_prompts.py +193 -0
- forma_core/video_review.py +1006 -0
- forma_core/wiring.py +356 -0
- forma_core/workers/__init__.py +120 -0
- forma_core/workers/contracts.py +183 -0
- forma_core/workers/generation.py +753 -0
- forma_core/workers/orchestration.py +805 -0
- forma_core/workers/registry.py +222 -0
- forma_core/workers/reverse_engineering.py +272 -0
- forma_core/workers/validation.py +291 -0
- forma_core/workspaces/__init__.py +5 -0
- forma_core/workspaces/chats/__init__.py +11 -0
- forma_core/workspaces/chats/models.py +48 -0
- forma_core/workspaces/context/__init__.py +15 -0
- forma_core/workspaces/context/agent.py +260 -0
- forma_core/workspaces/context/models.py +117 -0
- forma_core/workspaces/context/pdf.py +418 -0
- forma_core/workspaces/design_briefs/__init__.py +25 -0
- forma_core/workspaces/design_briefs/models.py +85 -0
- forma_core/workspaces/design_briefs/references.py +56 -0
- forma_core/workspaces/models.py +41 -0
- forma_core/workspaces/projects/__init__.py +88 -0
- forma_core/workspaces/projects/cad_generation.py +1016 -0
- forma_core/workspaces/projects/context_governance.py +365 -0
- forma_core/workspaces/projects/design_lifecycle.py +668 -0
- forma_core/workspaces/projects/fabrication/__init__.py +54 -0
- forma_core/workspaces/projects/fabrication/demo_printers.py +236 -0
- forma_core/workspaces/projects/fabrication/models.py +120 -0
- forma_core/workspaces/projects/fabrication/printer_config.py +205 -0
- forma_core/workspaces/projects/fabrication/slicers/__init__.py +7 -0
- forma_core/workspaces/projects/fabrication/slicers/base.py +25 -0
- forma_core/workspaces/projects/fabrication/slicers/cura.py +125 -0
- forma_core/workspaces/projects/fabrication/slicers/orca.py +208 -0
- forma_core/workspaces/projects/fabrication/slicing.py +202 -0
- forma_core/workspaces/projects/fabrication/validation.py +103 -0
- forma_core/workspaces/projects/gear_benchmark.py +6 -0
- forma_core/workspaces/projects/generation_mode.py +70 -0
- forma_core/workspaces/projects/identity.py +28 -0
- forma_core/workspaces/projects/iteration.py +680 -0
- forma_core/workspaces/projects/manifest.py +362 -0
- forma_core/workspaces/projects/mechanism_benchmarks.py +12 -0
- forma_core/workspaces/projects/models.py +913 -0
- forma_core/workspaces/projects/objects.py +731 -0
- forma_core/workspaces/projects/outcomes.py +47 -0
- forma_core/workspaces/projects/output.py +735 -0
- forma_core/workspaces/projects/resolver.py +397 -0
- forma_core/workspaces/projects/solid_cad.py +8 -0
- forma_core/workspaces/projects/state.py +622 -0
- forma_core/workspaces/readiness/__init__.py +28 -0
- forma_core/workspaces/readiness/evaluator.py +103 -0
- forma_core/workspaces/readiness/models.py +97 -0
- forma_core/workspaces/readiness/service.py +225 -0
- forma_core/workspaces/reverse_engineering/__init__.py +37 -0
- forma_core/workspaces/reverse_engineering/analyzer.py +246 -0
- forma_core/workspaces/reverse_engineering/models.py +128 -0
- forma_core/workspaces/validation/__init__.py +37 -0
- forma_core/workspaces/validation/evaluator.py +327 -0
- forma_core/workspaces/validation/models.py +146 -0
- forma_core/workspaces/validation/service.py +212 -0
- forma_core/workspaces/workflow/__init__.py +29 -0
- forma_core/workspaces/workflow/guard.py +33 -0
- forma_core/workspaces/workflow/models.py +77 -0
- forma_core/workspaces/workflow/service.py +287 -0
- openforma-0.4.0.dist-info/METADATA +247 -0
- openforma-0.4.0.dist-info/RECORD +288 -0
- openforma-0.4.0.dist-info/WHEEL +5 -0
- openforma-0.4.0.dist-info/entry_points.txt +4 -0
- openforma-0.4.0.dist-info/licenses/LICENSE +373 -0
- openforma-0.4.0.dist-info/top_level.txt +2 -0
forma_cli/config.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Non-secret CLI configuration and local project linkage."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import tomllib
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Cloud CLI commands target the hosted Forma deployment by default. Set
|
|
13
|
+
# FORMA_API_URL to a local API URL when developing against a local server.
|
|
14
|
+
DEFAULT_API_URL = "https://caid-technologies.us/api"
|
|
15
|
+
CONFIG_FILENAME = "config.json"
|
|
16
|
+
LINKAGE_FILENAME = "project.toml"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class CliConfigError(RuntimeError):
|
|
20
|
+
"""Raised when local CLI configuration is invalid or unavailable."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def config_dir() -> Path:
|
|
24
|
+
configured = os.environ.get("FORMA_CLI_CONFIG_DIR")
|
|
25
|
+
if configured:
|
|
26
|
+
return Path(configured).expanduser()
|
|
27
|
+
if os.name == "nt":
|
|
28
|
+
root = os.environ.get("APPDATA") or str(Path.home() / "AppData" / "Roaming")
|
|
29
|
+
return Path(root) / "Forma"
|
|
30
|
+
return Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) / "forma"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def config_path() -> Path:
|
|
34
|
+
return config_dir() / CONFIG_FILENAME
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def load_config() -> dict[str, Any]:
|
|
38
|
+
path = config_path()
|
|
39
|
+
if not path.exists():
|
|
40
|
+
return {"api_url": os.environ.get("FORMA_API_URL", DEFAULT_API_URL).rstrip("/")}
|
|
41
|
+
try:
|
|
42
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
43
|
+
except (OSError, ValueError) as exc:
|
|
44
|
+
raise CliConfigError(f"Could not read CLI configuration: {path}") from exc
|
|
45
|
+
if not isinstance(payload, dict):
|
|
46
|
+
raise CliConfigError(f"CLI configuration must be a JSON object: {path}")
|
|
47
|
+
return {
|
|
48
|
+
"api_url": str(payload.get("api_url") or os.environ.get("FORMA_API_URL") or DEFAULT_API_URL).rstrip("/"),
|
|
49
|
+
**{key: value for key, value in payload.items() if key != "api_url"},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def save_config(values: dict[str, Any]) -> None:
|
|
54
|
+
path = config_path()
|
|
55
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
temporary = path.with_suffix(".tmp")
|
|
57
|
+
temporary.write_text(json.dumps(values, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
58
|
+
temporary.replace(path)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def api_url() -> str:
|
|
62
|
+
return str(load_config().get("api_url") or DEFAULT_API_URL).rstrip("/")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def linkage_path(project_root: str | Path) -> Path:
|
|
66
|
+
return Path(project_root) / ".forma" / LINKAGE_FILENAME
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def load_linkage(project_root: str | Path) -> dict[str, Any]:
|
|
70
|
+
path = linkage_path(project_root)
|
|
71
|
+
if not path.exists():
|
|
72
|
+
return {"version": 1}
|
|
73
|
+
try:
|
|
74
|
+
payload = tomllib.loads(path.read_text(encoding="utf-8"))
|
|
75
|
+
except (OSError, tomllib.TOMLDecodeError) as exc:
|
|
76
|
+
raise CliConfigError(f"Could not read project linkage: {path}") from exc
|
|
77
|
+
if not isinstance(payload, dict):
|
|
78
|
+
raise CliConfigError(f"Project linkage must be a TOML table: {path}")
|
|
79
|
+
return payload
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _toml_value(value: Any) -> str:
|
|
83
|
+
if value is None:
|
|
84
|
+
return '""'
|
|
85
|
+
if isinstance(value, bool):
|
|
86
|
+
return "true" if value else "false"
|
|
87
|
+
if isinstance(value, int):
|
|
88
|
+
return str(value)
|
|
89
|
+
return json.dumps(str(value))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def save_linkage(project_root: str | Path, values: dict[str, Any]) -> None:
|
|
93
|
+
path = linkage_path(project_root)
|
|
94
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
lines = [f"{key} = {_toml_value(value)}" for key, value in values.items() if value is not None]
|
|
96
|
+
temporary = path.with_suffix(".tmp")
|
|
97
|
+
temporary.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
98
|
+
temporary.replace(path)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
__all__ = [
|
|
102
|
+
"CliConfigError",
|
|
103
|
+
"DEFAULT_API_URL",
|
|
104
|
+
"api_url",
|
|
105
|
+
"config_dir",
|
|
106
|
+
"config_path",
|
|
107
|
+
"linkage_path",
|
|
108
|
+
"load_config",
|
|
109
|
+
"load_linkage",
|
|
110
|
+
"save_config",
|
|
111
|
+
"save_linkage",
|
|
112
|
+
]
|
forma_cli/credentials.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""OS credential-store integration for CLI and local provider secrets."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
import warnings
|
|
10
|
+
|
|
11
|
+
from forma_cli.config import config_dir
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
SERVICE_NAME = "forma-oss"
|
|
15
|
+
SESSION_KEY = "cli-session"
|
|
16
|
+
_PLAINTEXT_ENV = "FORMA_ALLOW_PLAINTEXT_CREDENTIALS"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class CredentialStoreError(RuntimeError):
|
|
20
|
+
"""Raised when secure credential storage cannot be used."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _plaintext_enabled() -> bool:
|
|
24
|
+
return os.environ.get(_PLAINTEXT_ENV, "").strip().lower() in {"1", "true", "yes", "on"}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _keyring_module():
|
|
28
|
+
try:
|
|
29
|
+
import keyring
|
|
30
|
+
except ImportError as exc:
|
|
31
|
+
raise CredentialStoreError(
|
|
32
|
+
"The OS credential store is unavailable because keyring is not installed. "
|
|
33
|
+
f"Install the CLI dependencies or explicitly set {_PLAINTEXT_ENV}=1 for a warned plaintext fallback."
|
|
34
|
+
) from exc
|
|
35
|
+
return keyring
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _plaintext_path() -> Path:
|
|
39
|
+
configured = os.environ.get("FORMA_CLI_PLAINTEXT_CREDENTIALS_PATH")
|
|
40
|
+
if configured:
|
|
41
|
+
return Path(configured).expanduser()
|
|
42
|
+
return config_dir() / "credentials.json"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class CredentialStore:
|
|
46
|
+
"""Store values in keyring, with an opt-in insecure fallback for CI/dev."""
|
|
47
|
+
|
|
48
|
+
def __init__(self, *, service: str = SERVICE_NAME, keyring_backend: Any = None) -> None:
|
|
49
|
+
self.service = service
|
|
50
|
+
self._keyring_backend = keyring_backend
|
|
51
|
+
if _plaintext_enabled():
|
|
52
|
+
warnings.warn(
|
|
53
|
+
f"{_PLAINTEXT_ENV}=1 is enabled; Forma CLI credentials are being stored in plaintext.",
|
|
54
|
+
UserWarning,
|
|
55
|
+
stacklevel=2,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def using_plaintext_fallback(self) -> bool:
|
|
60
|
+
return _plaintext_enabled()
|
|
61
|
+
|
|
62
|
+
def _backend(self) -> Any:
|
|
63
|
+
return self._keyring_backend or _keyring_module()
|
|
64
|
+
|
|
65
|
+
def _load_plaintext(self) -> dict[str, str]:
|
|
66
|
+
path = _plaintext_path()
|
|
67
|
+
if not path.exists():
|
|
68
|
+
return {}
|
|
69
|
+
try:
|
|
70
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
71
|
+
except (OSError, ValueError) as exc:
|
|
72
|
+
raise CredentialStoreError(f"Could not read plaintext credential fallback: {path}") from exc
|
|
73
|
+
return {str(key): str(value) for key, value in payload.items()} if isinstance(payload, dict) else {}
|
|
74
|
+
|
|
75
|
+
def _save_plaintext(self, values: dict[str, str]) -> None:
|
|
76
|
+
path = _plaintext_path()
|
|
77
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
temporary = path.with_suffix(".tmp")
|
|
79
|
+
temporary.write_text(json.dumps(values, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
80
|
+
temporary.replace(path)
|
|
81
|
+
try:
|
|
82
|
+
path.chmod(0o600)
|
|
83
|
+
except OSError:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
def get(self, name: str) -> str | None:
|
|
87
|
+
if _plaintext_enabled():
|
|
88
|
+
return self._load_plaintext().get(name)
|
|
89
|
+
try:
|
|
90
|
+
return self._backend().get_password(self.service, name)
|
|
91
|
+
except Exception as exc:
|
|
92
|
+
raise CredentialStoreError(
|
|
93
|
+
"The OS credential store rejected the credential lookup. "
|
|
94
|
+
f"Set {_PLAINTEXT_ENV}=1 only if an explicit insecure fallback is acceptable."
|
|
95
|
+
) from exc
|
|
96
|
+
|
|
97
|
+
def set(self, name: str, value: str) -> None:
|
|
98
|
+
if not str(value).strip():
|
|
99
|
+
raise CredentialStoreError("Credential value cannot be empty.")
|
|
100
|
+
if _plaintext_enabled():
|
|
101
|
+
values = self._load_plaintext()
|
|
102
|
+
values[name] = value
|
|
103
|
+
self._save_plaintext(values)
|
|
104
|
+
return
|
|
105
|
+
try:
|
|
106
|
+
self._backend().set_password(self.service, name, value)
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
raise CredentialStoreError(
|
|
109
|
+
"The OS credential store rejected the credential write. "
|
|
110
|
+
f"Set {_PLAINTEXT_ENV}=1 only if an explicit insecure fallback is acceptable."
|
|
111
|
+
) from exc
|
|
112
|
+
|
|
113
|
+
def delete(self, name: str) -> None:
|
|
114
|
+
if _plaintext_enabled():
|
|
115
|
+
values = self._load_plaintext()
|
|
116
|
+
values.pop(name, None)
|
|
117
|
+
self._save_plaintext(values)
|
|
118
|
+
return
|
|
119
|
+
try:
|
|
120
|
+
self._backend().delete_password(self.service, name)
|
|
121
|
+
except Exception as exc:
|
|
122
|
+
# keyring backends commonly report missing entries as errors; deletion is idempotent.
|
|
123
|
+
message = str(exc).lower()
|
|
124
|
+
if not any(token in message for token in ("not found", "does not exist", "no such", "missing")):
|
|
125
|
+
raise CredentialStoreError("The OS credential store rejected credential removal.") from exc
|
|
126
|
+
|
|
127
|
+
def get_json(self, name: str) -> dict[str, Any] | None:
|
|
128
|
+
raw = self.get(name)
|
|
129
|
+
if not raw:
|
|
130
|
+
return None
|
|
131
|
+
try:
|
|
132
|
+
payload = json.loads(raw)
|
|
133
|
+
except ValueError as exc:
|
|
134
|
+
raise CredentialStoreError(f"Stored credential {name!r} is invalid.") from exc
|
|
135
|
+
return payload if isinstance(payload, dict) else None
|
|
136
|
+
|
|
137
|
+
def set_json(self, name: str, value: dict[str, Any]) -> None:
|
|
138
|
+
self.set(name, json.dumps(value, sort_keys=True))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
__all__ = ["CredentialStore", "CredentialStoreError", "SERVICE_NAME", "SESSION_KEY"]
|