File size: 1,659 Bytes
9c6594c |
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 58 59 60 |
import os
import re
from typing import TYPE_CHECKING
from tomlkit.api import loads
from tomlkit.toml_document import TOMLDocument
if TYPE_CHECKING:
from _typeshed import StrPath as _StrPath
else:
from typing import Union
_StrPath = Union[str, os.PathLike]
class TOMLFile:
"""
Represents a TOML file.
:param path: path to the TOML file
"""
def __init__(self, path: _StrPath) -> None:
self._path = path
self._linesep = os.linesep
def read(self) -> TOMLDocument:
"""Read the file content as a :class:`tomlkit.toml_document.TOMLDocument`."""
with open(self._path, encoding="utf-8", newline="") as f:
content = f.read()
# check if consistent line endings
num_newline = content.count("\n")
if num_newline > 0:
num_win_eol = content.count("\r\n")
if num_win_eol == num_newline:
self._linesep = "\r\n"
content = content.replace("\r\n", "\n")
elif num_win_eol == 0:
self._linesep = "\n"
else:
self._linesep = "mixed"
return loads(content)
def write(self, data: TOMLDocument) -> None:
"""Write the TOMLDocument to the file."""
content = data.as_string()
# apply linesep
if self._linesep == "\n":
content = content.replace("\r\n", "\n")
elif self._linesep == "\r\n":
content = re.sub(r"(?<!\r)\n", "\r\n", content)
with open(self._path, "w", encoding="utf-8", newline="") as f:
f.write(content)
|