File size: 1,580 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""TOML-file config backend (default, single-instance)."""

import asyncio
import os
from pathlib import Path
from typing import Any

import tomli_w
import tomllib

from .base import ConfigBackend
from ..loader import _deep_merge as deep_merge


class TomlConfigBackend(ConfigBackend):
    """Stores user overrides in a local TOML file.

    Flat-key concern doesn't apply here: TOML is the native format and the
    file is small enough that a full rewrite is negligible for local use.
    Version token = file mtime.
    """

    def __init__(self, path: Path) -> None:
        self._path = path

    async def load(self) -> dict[str, Any]:
        if not self._path.exists():
            return {}
        return await asyncio.to_thread(self._read)

    async def apply_patch(self, patch: dict[str, Any]) -> None:
        await asyncio.to_thread(self._merge_write, patch)

    async def version(self) -> object:
        return _mtime(self._path)

    def _read(self) -> dict[str, Any]:
        with open(self._path, "rb") as fh:
            return tomllib.load(fh)

    def _merge_write(self, patch: dict[str, Any]) -> None:
        existing = {}
        if self._path.exists():
            with open(self._path, "rb") as fh:
                existing = tomllib.load(fh)
        merged = deep_merge(existing, patch)
        self._path.parent.mkdir(parents=True, exist_ok=True)
        with open(self._path, "wb") as fh:
            tomli_w.dump(merged, fh)


def _mtime(path: Path) -> float:
    try:
        return os.stat(path).st_mtime
    except OSError:
        return 0.0