mirror of
https://github.com/malmeloo/FindMy.py.git
synced 2026-04-22 00:35:51 +02:00
feat: support in-memory files for {from,to}_{json,plist}
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
import plistlib
|
||||
from collections.abc import Mapping
|
||||
@@ -11,7 +12,7 @@ from typing import TypeVar, cast
|
||||
_T = TypeVar("_T", bound=Mapping)
|
||||
|
||||
|
||||
def save_and_return_json(data: _T, dst: str | Path | None) -> _T:
|
||||
def save_and_return_json(data: _T, dst: str | Path | io.TextIOBase | None) -> _T:
|
||||
"""Save and return a JSON-serializable data structure."""
|
||||
if dst is None:
|
||||
return data
|
||||
@@ -19,12 +20,15 @@ def save_and_return_json(data: _T, dst: str | Path | None) -> _T:
|
||||
if isinstance(dst, str):
|
||||
dst = Path(dst)
|
||||
|
||||
dst.write_text(json.dumps(data, indent=4))
|
||||
if isinstance(dst, io.IOBase):
|
||||
json.dump(data, dst, indent=4)
|
||||
elif isinstance(dst, Path):
|
||||
dst.write_text(json.dumps(data, indent=4))
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def read_data_json(val: str | Path | _T) -> _T:
|
||||
def read_data_json(val: str | Path | io.TextIOBase | io.BufferedIOBase | _T) -> _T:
|
||||
"""Read JSON data from a file if a path is passed, or return the argument itself."""
|
||||
if isinstance(val, str):
|
||||
val = Path(val)
|
||||
@@ -32,10 +36,13 @@ def read_data_json(val: str | Path | _T) -> _T:
|
||||
if isinstance(val, Path):
|
||||
val = cast("_T", json.loads(val.read_text()))
|
||||
|
||||
if isinstance(val, io.IOBase):
|
||||
val = cast("_T", json.load(val))
|
||||
|
||||
return val
|
||||
|
||||
|
||||
def save_and_return_plist(data: _T, dst: str | Path | None) -> _T:
|
||||
def save_and_return_plist(data: _T, dst: str | Path | io.BufferedIOBase | None) -> _T:
|
||||
"""Save and return a Plist file."""
|
||||
if dst is None:
|
||||
return data
|
||||
@@ -43,12 +50,15 @@ def save_and_return_plist(data: _T, dst: str | Path | None) -> _T:
|
||||
if isinstance(dst, str):
|
||||
dst = Path(dst)
|
||||
|
||||
dst.write_bytes(plistlib.dumps(data))
|
||||
if isinstance(dst, io.IOBase):
|
||||
dst.write(plistlib.dumps(data))
|
||||
elif isinstance(dst, Path):
|
||||
dst.write_bytes(plistlib.dumps(data))
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def read_data_plist(val: str | Path | _T | bytes) -> _T:
|
||||
def read_data_plist(val: str | Path | io.BufferedIOBase | _T | bytes) -> _T:
|
||||
"""Read Plist data from a file if a path is passed, or return the argument itself."""
|
||||
if isinstance(val, str):
|
||||
val = Path(val)
|
||||
@@ -59,4 +69,7 @@ def read_data_plist(val: str | Path | _T | bytes) -> _T:
|
||||
if isinstance(val, bytes):
|
||||
val = cast("_T", plistlib.loads(val))
|
||||
|
||||
if isinstance(val, io.IOBase):
|
||||
val = cast("_T", plistlib.loads(val.read()))
|
||||
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user