-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,7 @@ cython_debug/ | |
|
||
# Tests | ||
tests.py | ||
tests.json | ||
|
||
# PyPi | ||
.pypirc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from ufpy.path.files import * | ||
from ufpy.path.json_file_updater import * | ||
from ufpy.path.tools import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ( | ||
'JsonFileUpdater', | ||
) | ||
|
||
from typing import Any, Generic, TypeVar | ||
from ujson import dumps, loads | ||
|
||
VT = TypeVar('VT') | ||
|
||
class JsonFileUpdater(Generic[VT]): | ||
def __init__(self, json_file_path: str, indent: int = 4) -> None: | ||
self.path = json_file_path | ||
self.indent = indent | ||
self.__d: dict[str, VT] | None = None | ||
|
||
def __load(self) -> dict[str, VT]: | ||
with open(self.path, encoding='utf-8') as f: | ||
r = loads(f.read()) | ||
return r | ||
|
||
def __write(self, d: dict[str, VT]) -> None: | ||
with open(self.path, 'w', encoding='utf-8') as f: | ||
f.write(dumps( | ||
d, | ||
ensure_ascii=False, | ||
indent=self.indent | ||
)) | ||
|
||
def __get_dict(self, path: str, __dict: dict[str, Any]) -> dict[str, Any]: | ||
if not ' / ' in path: | ||
return __dict | ||
keys = path.split(' / ')[:-1] | ||
|
||
r = __dict | ||
for i in keys: | ||
r = r[i] | ||
return r | ||
|
||
def __getitem__(self, key: str) -> VT: | ||
if self.__d == None: | ||
d = self.__load() | ||
else: | ||
d = self.__d | ||
|
||
keys = key.split(' / ') | ||
|
||
return self.__get_dict(key, d)[keys[-1]] | ||
|
||
def __setitem__(self, key: str, value: VT) -> None: | ||
if self.__d == None: | ||
d = self.__load() | ||
|
||
keys = key.split(' / ') | ||
|
||
r = d | ||
for i in keys: | ||
r.setdefault(i, {}) | ||
r = r[i] | ||
|
||
d2 = self.__get_dict(key, d) | ||
d2[keys[-1]] = value | ||
|
||
self.__write(d) | ||
else: | ||
keys = key.split(' / ') | ||
|
||
r = self.__d | ||
for i in keys: | ||
r.setdefault(i, {}) | ||
r = r[i] | ||
|
||
d2 = self.__get_dict(key, self.__d) | ||
d2[keys[-1]] = value | ||
def __enter__(self) -> JsonFileUpdater: | ||
self.__d = self.__load() | ||
return self | ||
|
||
def __exit__(self, exception_type, exception_value, traceback) -> None: | ||
self.__write(self.__d) | ||
self.__d = None | ||
|
||
def __repr__(self) -> str: | ||
if self.__d == None: | ||
return dumps( | ||
self.__load(), | ||
ensure_ascii=False, | ||
indent=self.indent | ||
) | ||
return repr(self.__d) |