Add TomlFile

This commit is contained in:
Joscha 2025-11-03 01:54:34 +01:00
parent ad10c7b5d2
commit 2e9a4e1c98
5 changed files with 65 additions and 2 deletions

View file

@ -2,6 +2,7 @@ from .binary import BinaryFile
from .file import TAG, File
from .json import JsonFile
from .text import TextFile
from .toml import TomlFile
__all__: list[str] = [
"TAG",
@ -9,4 +10,5 @@ __all__: list[str] = [
"File",
"JsonFile",
"TextFile",
"TomlFile",
]

View file

@ -22,7 +22,7 @@ class JsonFileProxy:
data = self.file.data
*parts, last = self.path
for part in parts:
data = data[part]
data = data.setdefault(part, {})
data[last] = value
def tag_here(self, tag: str = TAG) -> None:

46
pasch/file/toml.py Normal file
View file

@ -0,0 +1,46 @@
from dataclasses import dataclass
from typing import Any, Self
import toml
from .file import File
from .text import TextFile
@dataclass
class TomlFileProxy:
file: "TomlFile"
path: tuple[str, ...]
def at(self, *path: str) -> Self:
return TomlFileProxy(self.file, self.path + path)
def set(self, value: Any) -> None:
if not self.path:
self.file.set(value)
data = self.file.data
*parts, last = self.path
for part in parts:
data = data.setdefault(part, {})
data[last] = value
class TomlFile(File):
def __init__(self, data: Any = {}) -> None:
self.data = data
def at(self, *path: str) -> TomlFileProxy:
return TomlFileProxy(self, path)
def set(self, value: Any) -> None:
self.data = value
def to_text(self) -> TextFile:
file = TextFile()
file.tag(comment="#")
file.append(toml.dumps(self.data))
return file
def to_bytes(self) -> bytes:
return self.to_text().to_bytes()

View file

@ -4,7 +4,11 @@ version = "0.0.0"
description = "Python-based Arch System Config Helper"
readme = "README.md"
requires-python = ">=3.13"
dependencies = ["rich>=14.1.0", "xdg-base-dirs>=6.0.2"]
dependencies = [
"rich>=14.1.0",
"toml>=0.10.2",
"xdg-base-dirs>=6.0.2",
]
[tool.ruff.lint]
select = ["RUF", "F"]

11
uv.lock generated
View file

@ -29,12 +29,14 @@ version = "0.0.0"
source = { virtual = "." }
dependencies = [
{ name = "rich" },
{ name = "toml" },
{ name = "xdg-base-dirs" },
]
[package.metadata]
requires-dist = [
{ name = "rich", specifier = ">=14.1.0" },
{ name = "toml", specifier = ">=0.10.2" },
{ name = "xdg-base-dirs", specifier = ">=6.0.2" },
]
@ -60,6 +62,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e3/30/3c4d035596d3cf444529e0b2953ad0466f6049528a879d27534700580395/rich-14.1.0-py3-none-any.whl", hash = "sha256:536f5f1785986d6dbdea3c75205c473f970777b4a0d6c6dd1b696aa05a3fa04f", size = 243368, upload-time = "2025-07-25T07:32:56.73Z" },
]
[[package]]
name = "toml"
version = "0.10.2"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/be/ba/1f744cdc819428fc6b5084ec34d9b30660f6f9daaf70eead706e3203ec3c/toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f", size = 22253, upload-time = "2020-11-01T01:40:22.204Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", size = 16588, upload-time = "2020-11-01T01:40:20.672Z" },
]
[[package]]
name = "xdg-base-dirs"
version = "6.0.2"