Skip to content

Commit

Permalink
feat: allow to remember read news items
Browse files Browse the repository at this point in the history
The state is stored in $XDG_STATE_HOME/ruyi/news.read.txt. The stored
data is just plain text, with one alphabetically-sorted news item ID per
line.
  • Loading branch information
xen0n committed Jan 13, 2024
1 parent 135dc70 commit 3c4fd07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ruyi/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from xdg import BaseDirectory

from .. import log, argv0
from .news import NewsReadStatusStore


DEFAULT_REPO_URL = "https://mirror.iscas.ac.cn/git/ruyisdk/packages-index.git"
Expand All @@ -33,6 +34,8 @@ def __init__(self) -> None:
self.override_repo_branch: str | None = None
self.include_prereleases = False

self._news_read_status_store: NewsReadStatusStore | None = None

def apply_config(self, config_data: GlobalConfigRootType) -> None:
if pkgs_cfg := config_data.get("packages"):
self.include_prereleases = pkgs_cfg.get("prereleases", False)
Expand All @@ -45,6 +48,19 @@ def cache_root(self) -> str:
def data_root(self) -> str:
return os.path.join(BaseDirectory.xdg_data_home, self.resource_name)

@property
def state_root(self) -> str:
return os.path.join(BaseDirectory.xdg_state_home, self.resource_name)

@property
def news_read_status(self) -> NewsReadStatusStore:
if self._news_read_status_store is not None:
return self._news_read_status_store

filename = os.path.join(self.ensure_state_dir(), "news.read.txt")
self._news_read_status_store = NewsReadStatusStore(filename)
return self._news_read_status_store

def get_repo_dir(self) -> str:
return self.override_repo_dir or os.path.join(self.cache_root, "packages-index")

Expand Down Expand Up @@ -82,6 +98,10 @@ def ensure_config_dir(cls) -> str:
def ensure_cache_dir(cls) -> str:
return BaseDirectory.save_cache_path(cls.resource_name)

@classmethod
def ensure_state_dir(cls) -> str:
return BaseDirectory.save_state_path(cls.resource_name)

@classmethod
def get_config_file(cls) -> str | None:
# TODO: maybe allow customization of config root
Expand Down
29 changes: 29 additions & 0 deletions ruyi/config/news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class NewsReadStatusStore:
def __init__(self, path: str) -> None:
self._path = path
self._status = set()
self._orig_status = set()

def load(self) -> None:
try:
with open(self._path, "r") as fp:
for l in fp:
self._orig_status.add(l.strip())
except FileNotFoundError:
return

self._status = self._orig_status.copy()

def __contains__(self, key: str) -> bool:
return key in self._status

def add(self, id: str) -> None:
return self._status.add(id)

def save(self) -> None:
if self._status == self._orig_status:
return

content = "".join(f"{id}\n" for id in self._status)
with open(self._path, "w", encoding="utf-8") as fp:
fp.write(content)

0 comments on commit 3c4fd07

Please sign in to comment.