Skip to content

Commit

Permalink
JsonFileUpdater pre-final
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Sep 8, 2024
1 parent d4510ba commit 29978f8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
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
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)

0 comments on commit 29978f8

Please sign in to comment.