fdf
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from typing import List, Dict, Any, Sequence
|
||||
from typing import List, Dict, Any, Sequence, Optional
|
||||
from SYS.cmdlet_spec import Cmdlet, CmdletArg
|
||||
from SYS.logger import log
|
||||
from SYS.result_table import Table
|
||||
@@ -12,22 +12,45 @@ ADJECTIVE_FILE = os.path.join(
|
||||
"cmdnat",
|
||||
"adjective.json"
|
||||
)
|
||||
_ADJECTIVE_CACHE: Optional[Dict[str, List[str]]] = None
|
||||
_ADJECTIVE_CACHE_MTIME_NS: Optional[int] = None
|
||||
|
||||
|
||||
def _load_adjectives() -> Dict[str, List[str]]:
|
||||
global _ADJECTIVE_CACHE, _ADJECTIVE_CACHE_MTIME_NS
|
||||
try:
|
||||
if os.path.exists(ADJECTIVE_FILE):
|
||||
with open(ADJECTIVE_FILE, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
if not os.path.exists(ADJECTIVE_FILE):
|
||||
_ADJECTIVE_CACHE = {}
|
||||
_ADJECTIVE_CACHE_MTIME_NS = None
|
||||
return {}
|
||||
|
||||
current_mtime_ns = os.stat(ADJECTIVE_FILE).st_mtime_ns
|
||||
if (_ADJECTIVE_CACHE is not None and
|
||||
_ADJECTIVE_CACHE_MTIME_NS == current_mtime_ns):
|
||||
return _ADJECTIVE_CACHE
|
||||
|
||||
with open(ADJECTIVE_FILE, "r", encoding="utf-8") as f:
|
||||
loaded = json.load(f)
|
||||
if not isinstance(loaded, dict):
|
||||
loaded = {}
|
||||
|
||||
_ADJECTIVE_CACHE = loaded
|
||||
_ADJECTIVE_CACHE_MTIME_NS = current_mtime_ns
|
||||
return _ADJECTIVE_CACHE
|
||||
except Exception as e:
|
||||
log(f"Error loading adjectives: {e}", file=sys.stderr)
|
||||
_ADJECTIVE_CACHE = {}
|
||||
_ADJECTIVE_CACHE_MTIME_NS = None
|
||||
return {}
|
||||
|
||||
|
||||
def _save_adjectives(data: Dict[str, List[str]]) -> bool:
|
||||
global _ADJECTIVE_CACHE, _ADJECTIVE_CACHE_MTIME_NS
|
||||
try:
|
||||
with open(ADJECTIVE_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
_ADJECTIVE_CACHE = data
|
||||
_ADJECTIVE_CACHE_MTIME_NS = os.stat(ADJECTIVE_FILE).st_mtime_ns
|
||||
return True
|
||||
except Exception as e:
|
||||
log(f"Error saving adjectives: {e}", file=sys.stderr)
|
||||
|
||||
Reference in New Issue
Block a user