Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonFileUpdater #57

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ cython_debug/

# Tests
tests.py
tests.json

# PyPi
.pypirc
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests>=2.31.0
requests>=2.31
ujson>=5.10
2 changes: 1 addition & 1 deletion ufpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def new_func(*args, **kwargs):
UStack = __deprecated("UStack", Stack, '0.2', '0.5')

# Path package
__path_version__ = '0.1'
__path_version__ = '0.2'
from ufpy.path import *

# GitHub package
Expand Down
1 change: 1 addition & 0 deletions ufpy/path/__init__.py
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 *
91 changes: 91 additions & 0 deletions ufpy/path/json_file_updater.py
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)
Loading