← Files Dataiku HeadlessARCHIVED FILE

dataiku_mcp/config/files.py

1.77 KB · Nov 27, 2026 · 19:32 UTC

↓ Download file

# Copyright 2026 Dataiku SAS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Shared JSON file operations for transport configuration."""

import json
import os
import tempfile
from pathlib import Path


def read_json_object(path: Path, *, description: str = "Settings file") -> dict:
    """Read a UTF-8 JSON object from path."""
    with open(path, encoding="utf-8") as file:
        document = json.load(file)
    if not isinstance(document, dict):
        raise ValueError(f"{description} must be a JSON object.")
    return document


def write_json_atomic(path: Path, document: dict) -> None:
    """Atomically write a UTF-8 JSON object with user-only permissions."""
    path.parent.mkdir(parents=True, exist_ok=True)
    temp_path: Path | None = None
    try:
        with tempfile.NamedTemporaryFile(
            "w",
            encoding="utf-8",
            dir=path.parent,
            prefix="config.",
            suffix=".tmp",
            delete=False,
        ) as temp_file:
            temp_path = Path(temp_file.name)
            json.dump(document, temp_file, indent=2)
            temp_file.write("\n")
        temp_path.chmod(0o600)
        os.replace(temp_path, path)
    except BaseException:
        if temp_path is not None:
            temp_path.unlink(missing_ok=True)
        raise

SHA-256: 0edb37c148e44720cbdc6127c323156dbe31bc0f2e181dfd979c19399cff2796