clan-cli: clan_cli.locked_open -> clan_lib.locked_open
This commit is contained in:
33
pkgs/clan-cli/clan_lib/locked_open/__init__.py
Normal file
33
pkgs/clan-cli/clan_lib/locked_open/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import fcntl
|
||||
import json
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from clan_cli.jsonrpc import ClanJSONEncoder
|
||||
|
||||
from clan_lib.dirs import user_history_file
|
||||
|
||||
|
||||
@contextmanager
|
||||
def locked_open(filename: Path, mode: str = "r") -> Generator:
|
||||
"""
|
||||
This is a context manager that provides an advisory write lock on the file specified by `filename` when entering the context, and releases the lock when leaving the context. The lock is acquired using the `fcntl` module's `LOCK_EX` flag, which applies an exclusive write lock to the file.
|
||||
"""
|
||||
with filename.open(mode) as fd:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX)
|
||||
yield fd
|
||||
fcntl.flock(fd, fcntl.LOCK_UN)
|
||||
|
||||
|
||||
def write_history_file(data: Any) -> None:
|
||||
with locked_open(user_history_file(), "w+") as f:
|
||||
f.write(json.dumps(data, cls=ClanJSONEncoder, indent=4))
|
||||
|
||||
|
||||
def read_history_file() -> list[dict]:
|
||||
with locked_open(user_history_file(), "r") as f:
|
||||
content: str = f.read()
|
||||
parsed: list[dict] = json.loads(content)
|
||||
return parsed
|
||||
Reference in New Issue
Block a user