🐛 修复 metadata

This commit is contained in:
Karako 2022-10-07 14:39:58 +08:00
parent 0f343a0e3c
commit 151b01120a
No known key found for this signature in database
GPG Key ID: 5920831B0095D4A0

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Generic, Iterator, KeysView, TypeVar from typing import Any, Generic, ItemsView, Iterator, KeysView, TypeVar
import ujson as json import ujson as json
@ -32,9 +32,9 @@ class Data(dict, Generic[K, V]):
@property @property
def data(self) -> dict[K, V]: def data(self) -> dict[K, V]:
if not (result := _cache.get(self._file_name, None)): if (result := _cache.get(self._file_name)) not in [None, {}]:
self._dict = result self._dict = result
super(Data, self).__init__(result) else:
path = data_dir.joinpath(self._file_name).with_suffix('.json') path = data_dir.joinpath(self._file_name).with_suffix('.json')
if not path.exists(): if not path.exists():
logger.error( logger.error(
@ -53,8 +53,8 @@ class Data(dict, Generic[K, V]):
self._dict = {} self._dict = {}
super(Data, self).__init__() super(Data, self).__init__()
def get(self, key: K) -> V | None: def get(self, key: K, value: Any = None) -> V | None:
return self.data.get(key) return self.data.get(key, value)
def __getitem__(self, key: K) -> V: def __getitem__(self, key: K) -> V:
return self.data.__getitem__(key) return self.data.__getitem__(key)
@ -71,6 +71,9 @@ class Data(dict, Generic[K, V]):
def keys(self) -> KeysView[K, V]: def keys(self) -> KeysView[K, V]:
return self.data.keys() return self.data.keys()
def items(self) -> ItemsView[K, V]:
return self.data.items()
HONEY_DATA: dict[str, dict[StrOrInt, list[str | int]]] = Data('honey') HONEY_DATA: dict[str, dict[StrOrInt, list[str | int]]] = Data('honey')