diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/__init__.py | 1 | ||||
-rw-r--r-- | src/backend/getfs.py | 2 | ||||
-rw-r--r-- | src/backend/getnews.py | 116 | ||||
-rwxr-xr-x | src/frontend/cli/sisyphus-cli.py | 51 |
4 files changed, 168 insertions, 2 deletions
diff --git a/src/backend/__init__.py b/src/backend/__init__.py index 0e54ce9..21dec83 100644 --- a/src/backend/__init__.py +++ b/src/backend/__init__.py @@ -3,6 +3,7 @@ from .dlbinpkg import * from .getclr import * from .getenv import * from .getfs import * +from .getnews import * from .killemerge import * from .pkgadd import * from .pkgremove import * diff --git a/src/backend/getfs.py b/src/backend/getfs.py index 77e0b2d..27da9ba 100644 --- a/src/backend/getfs.py +++ b/src/backend/getfs.py @@ -17,6 +17,8 @@ p_cch_dir = '/var/cache/packages' p_dst_dir = '/var/cache/distfiles' p_mtd_dir = '/var/cache/edb' +s_cfg_dir = '/etc/sisyphus' + rmt_pcsv = '/var/lib/sisyphus/csv/remotePackagesPre.csv' rmt_dcsv = '/var/lib/sisyphus/csv/remoteDescriptionsPre.csv' lcl_pcsv = '/var/lib/sisyphus/csv/localPackagesPre.csv' diff --git a/src/backend/getnews.py b/src/backend/getnews.py new file mode 100644 index 0000000..ce56df4 --- /dev/null +++ b/src/backend/getnews.py @@ -0,0 +1,116 @@ +#!/usr/bin/python3 + +import os +import sisyphus.getclr +import sisyphus.getfs + +NEWS_DIR = "news" + +N_NEWS_FILE = os.path.join(sisyphus.getfs.p_cfg_dir, + os.path.join(NEWS_DIR, "n_news.txt")) +R_NEWS_FILE = os.path.join(sisyphus.getfs.s_cfg_dir, + os.path.join(NEWS_DIR, "r_news.txt")) +DELIMITER = "---" + + +def ld_n_news(): + with open(N_NEWS_FILE, "r") as file: + content = file.read() + articles = content.split(DELIMITER) + return [article.strip() for article in articles if article.strip()] + + +def ld_r_news(): + try: + with open(R_NEWS_FILE, "r") as file: + return [int(line.strip()) for line in file.readlines()] + except FileNotFoundError: + return [] + + +def save_r_news(r_news): + with open(R_NEWS_FILE, "w") as file: + for index in r_news: + file.write(f"{index}\n") + + +def mark_read(article_nr): + n_news = ld_n_news() + + if 1 <= article_nr <= len(n_news): + article_index = article_nr - 1 + r_news_index = ld_r_news() + + if article_index in r_news_index: + print(f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} is already marked as {sisyphus.getclr.green}read{sisyphus.getclr.reset}.") + else: + r_news_index.append(article_index) + save_r_news(r_news_index) + print( + f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} marked as {sisyphus.getclr.green}read{sisyphus.getclr.reset}.") + else: + print( + f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} doesn't exist.") + + +def mark_unread(article_nr): + n_news = ld_n_news() + + if 1 <= article_nr <= len(n_news): + article_index = article_nr - 1 + r_news_index = ld_r_news() + + if article_index not in r_news_index: + print(f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} is already marked as {sisyphus.getclr.green}unread{sisyphus.getclr.reset}.") + else: + r_news_index.remove(article_index) + save_r_news(r_news_index) + print( + f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} marked as {sisyphus.getclr.green}unread{sisyphus.getclr.reset}.") + else: + print( + f"\nArticle {sisyphus.getclr.green}{article_nr}{sisyphus.getclr.reset} doesn't exist.") + + +def list_all_news(): + n_news = ld_n_news() + r_news_index = ld_r_news() + + for index, news in enumerate(n_news): + status = "Read" if index in r_news_index else "Unread" + print( + f"\nArticle {sisyphus.getclr.green}{index + 1}{sisyphus.getclr.reset} ({status}):\n{news}") + + +def check_n_news(): + n_news = ld_n_news() + r_news_index = ld_r_news() + + unread_count = len(n_news) - len(r_news_index) + + if unread_count > 0: + print( + f"\nThere are {sisyphus.getclr.bright_red}{unread_count}{sisyphus.getclr.reset} unread news article(s).") + else: + print( + f"\nThere are {sisyphus.getclr.green}{unread_count}{sisyphus.getclr.reset} unread news article(s).") + + +def start(check=False, list=False, read=False, unread=False, article_nr=None): + if check: + check_n_news() + + if list: + list_all_news() + + if read: + if article_nr is not None: + mark_read(article_nr) + else: + print("\nError: No article number provided to mark as read.") + + if unread: + if article_nr is not None: + mark_unread(article_nr) + else: + print("\nError: No article number provided to mark as unread.") diff --git a/src/frontend/cli/sisyphus-cli.py b/src/frontend/cli/sisyphus-cli.py index 33c45ff..053f111 100755 --- a/src/frontend/cli/sisyphus-cli.py +++ b/src/frontend/cli/sisyphus-cli.py @@ -8,8 +8,11 @@ import sys app = typer.Typer() mirrorSetup = typer.Typer() +getNews = typer.Typer() app.add_typer(mirrorSetup, name="mirror", help='List/Set the active binhost (binary repository) mirror.') +app.add_typer(getNews, name="news", + help='Check/List/Mark Read/Mark Unread news articles.') @app.callback() @@ -324,7 +327,7 @@ def sysinfo(): @mirrorSetup.command("list") -def mirrorlist(): +def listmirrors(): """ List available binary package repository mirrors (the active one is marked with *).\n \n @@ -335,7 +338,7 @@ def mirrorlist(): @mirrorSetup.command("set") -def mirrorset(index: int): +def setmirror(index: int): """ Change the binary package repository to the selected mirror.\n \n @@ -346,6 +349,50 @@ def mirrorset(index: int): sisyphus.setmirror.setActive(index) +@getNews.command("check") +def checknews(): + """ + Check for unread news articles,\n + \n + * Example:\n + sisyphus news check\n + """ + sisyphus.getnews.start(check=True) + + +@getNews.command("list") +def listnews(): + """ + List all news articles.\n + \n + * Example:\n + sisyphus news list\n + """ + sisyphus.getnews.start(list=True) + + +@getNews.command("read") +def readnews(index: int): + """ + Mark a news article as read.\n + \n + * Example:\n + sisyphus news read 1\n + """ + sisyphus.getnews.start(read=True, article_nr=index) + + +@getNews.command("unread") +def unreadnews(index: int): + """ + Mark a news article as unread.\n + \n + * Example:\n + sisyphus news unread 2\n + """ + sisyphus.getnews.start(unread=True, article_nr=index) + + if __name__ == "__main__": if len(sys.argv) > 1 and not '--help' in sys.argv: sisyphus.setjobs.start() |