MCPcopy Index your code
hub / github.com/github/spec-kit / _read_integration_json_data

Function _read_integration_json_data

src/specify_cli/integration_state.py:28–70  ·  view source on GitHub ↗

Read raw integration state without normalizing or raising. Returns ``(data, None)`` when the JSON object is readable and supported, ``(None, None)`` when the file is absent, and ``(None, error)`` for parse, schema, encoding, or filesystem failures.

(
    project_root: Path,
)

Source from the content-addressed store, hash-verified

26
27
28def _read_integration_json_data(
29 project_root: Path,
30) -> tuple[dict[str, Any] | None, IntegrationReadError | None]:
31 """Read raw integration state without normalizing or raising.
32
33 Returns ``(data, None)`` when the JSON object is readable and supported,
34 ``(None, None)`` when the file is absent, and ``(None, error)`` for parse,
35 schema, encoding, or filesystem failures.
36 """
37 path = project_root / INTEGRATION_JSON
38 # Avoid Path.exists() / Path.is_file() as a pre-check: both return False
39 # on some OSErrors (e.g. permission errors during stat), which would
40 # silently treat an unreadable-but-present file as missing. Attempt the
41 # read directly and distinguish FileNotFoundError (genuinely absent) from
42 # other OSErrors (which become loud errors via the IntegrationReadError
43 # path).
44 try:
45 raw = path.read_text(encoding="utf-8")
46 except FileNotFoundError:
47 return None, None
48 except IsADirectoryError as exc:
49 return None, IntegrationReadError(
50 kind="os",
51 detail=f"{path} exists but is not a regular file: {exc}",
52 )
53 except UnicodeDecodeError as exc:
54 return None, IntegrationReadError(kind="decode", detail=str(exc))
55 except OSError as exc:
56 return None, IntegrationReadError(kind="os", detail=str(exc))
57 try:
58 data = json.loads(raw)
59 except json.JSONDecodeError as exc:
60 return None, IntegrationReadError(kind="decode", detail=str(exc))
61 if not isinstance(data, dict):
62 return None, IntegrationReadError(kind="not_object", detail=type(data).__name__)
63 schema = data.get("integration_state_schema")
64 if (
65 isinstance(schema, int)
66 and not isinstance(schema, bool)
67 and schema > INTEGRATION_STATE_SCHEMA
68 ):
69 return None, IntegrationReadError(kind="schema_too_new", schema=schema)
70 return data, None
71
72
73def try_read_integration_json(

Callers 2

Calls 3

read_textMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected