From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <gentoo-commits+bounces-1490949-garchives=archives.gentoo.org@lists.gentoo.org> Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 42BBA158030 for <garchives@archives.gentoo.org>; Mon, 27 Feb 2023 06:15:51 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 34DAAE07F9; Mon, 27 Feb 2023 06:15:50 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 129F7E07F9 for <gentoo-commits@lists.gentoo.org>; Mon, 27 Feb 2023 06:15:49 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id EE005335D65 for <gentoo-commits@lists.gentoo.org>; Mon, 27 Feb 2023 06:15:48 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 4F1DA8B5 for <gentoo-commits@lists.gentoo.org>; Mon, 27 Feb 2023 06:15:46 +0000 (UTC) From: "Sam James" <sam@gentoo.org> To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" <sam@gentoo.org> Message-ID: <1677478540.fb4ba0ef70095bd6f17e198674e16d60284ab2dc.sam@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/news.py X-VCS-Directories: lib/portage/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: fb4ba0ef70095bd6f17e198674e16d60284ab2dc X-VCS-Branch: master Date: Mon, 27 Feb 2023 06:15:46 +0000 (UTC) Precedence: bulk List-Post: <mailto:gentoo-commits@lists.gentoo.org> List-Help: <mailto:gentoo-commits+help@lists.gentoo.org> List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org> List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org> List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org> X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: a03ef8a2-7eb3-4ba6-8ccf-372d986ff2b1 X-Archives-Hash: a6c02df7fea38d1c3f1aaee0b3420c9b commit: fb4ba0ef70095bd6f17e198674e16d60284ab2dc Author: Sam James <sam <AT> gentoo <DOT> org> AuthorDate: Mon Feb 27 05:10:23 2023 +0000 Commit: Sam James <sam <AT> gentoo <DOT> org> CommitDate: Mon Feb 27 06:15:40 2023 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=fb4ba0ef portage: news: trivial type annotations Signed-off-by: Sam James <sam <AT> gentoo.org> lib/portage/news.py | 59 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/lib/portage/news.py b/lib/portage/news.py index f81debe97..68f0c72d3 100644 --- a/lib/portage/news.py +++ b/lib/portage/news.py @@ -14,11 +14,17 @@ __all__ = [ ] from collections import OrderedDict +from typing import TYPE_CHECKING, Any, Dict, List, Optional import fnmatch import logging import os as _os import re + +if TYPE_CHECKING: + import portage.dbapi.vartree + import portage.package.ebuild.config + from portage import os from portage import _encodings from portage import _unicode_decode @@ -58,7 +64,14 @@ class NewsManager: """ - def __init__(self, portdb, vardb, news_path, unread_path, language_id="en"): + def __init__( + self, + portdb: "portage.dbapi.porttree.portdbapi", + vardb: "portage.dbapi.vartree.vardbapi", + news_path: str, + unread_path: str, + language_id: str = "en", + ): self.news_path = news_path self.unread_path = unread_path self.language_id = language_id @@ -89,19 +102,19 @@ class NewsManager: profile_path = profile_path[len(profiles_base) :] self._profile_path = profile_path - def _unread_filename(self, repoid): + def _unread_filename(self, repoid: str) -> str: return os.path.join(self.unread_path, f"news-{repoid}.unread") - def _skip_filename(self, repoid): + def _skip_filename(self, repoid: str) -> str: return os.path.join(self.unread_path, f"news-{repoid}.skip") - def _news_dir(self, repoid): + def _news_dir(self, repoid: str) -> str: repo_path = self.portdb.getRepositoryPath(repoid) if repo_path is None: raise AssertionError(_(f"Invalid repoID: {repoid}")) return os.path.join(repo_path, self.news_path) - def updateItems(self, repoid): + def updateItems(self, repoid: str) -> None: """ Figure out which news items from NEWS_PATH are both unread and relevant to the user (according to the GLEP 42 standards of relevancy). Then add these @@ -199,7 +212,7 @@ class NewsManager: finally: unlockfile(unread_lock) - def getUnreadItems(self, repoid, update=False): + def getUnreadItems(self, repoid: str, update: bool = False) -> int: """ Determine if there are unread relevant items in news.repoid.unread. If there are unread items return their number. @@ -249,7 +262,7 @@ class NewsItem: Creation of a news item involves passing in the path to the particular news item. """ - def __init__(self, path, name): + def __init__(self, path: str, name: str): """ For a given news item we only want if it path is a file. """ @@ -258,7 +271,12 @@ class NewsItem: self._parsed = False self._valid = True - def isRelevant(self, vardb, config, profile): + def isRelevant( + self, + vardb: "portage.dbapi.vartree.vardbapi", + config: "portage.package.ebuild.config.config", + profile: Optional[str], + ) -> bool: """ This function takes a dict of keyword arguments; one should pass in any objects need to do to lookups (like what keywords we are on, what profile, @@ -292,12 +310,12 @@ class NewsItem: return all_match - def isValid(self): + def isValid(self) -> bool: if not self._parsed: self.parse() return self._valid - def parse(self): + def parse(self) -> None: with open( _unicode_encode(self.path, encoding=_encodings["fs"], errors="strict"), encoding=_encodings["content"], @@ -365,7 +383,7 @@ class DisplayRestriction: are met, then it is displayed """ - def isValid(self): + def isValid(self) -> bool: return True def checkRestriction(self, **kwargs): @@ -382,7 +400,7 @@ class DisplayProfileRestriction(DisplayRestriction): self.profile = profile self.format = news_format - def isValid(self): + def isValid(self) -> bool: return ( not fnmatch.fnmatch(self.format, "1.*") or "*" not in self.profile @@ -390,7 +408,7 @@ class DisplayProfileRestriction(DisplayRestriction): or _valid_profile_RE.match(self.profile) ) - def checkRestriction(self, **kwargs): + def checkRestriction(self, **kwargs) -> bool: if fnmatch.fnmatch(self.format, "2.*") and self.profile.endswith("/*"): return kwargs["profile"].startswith(self.profile[:-1]) return kwargs["profile"] == self.profile @@ -406,7 +424,7 @@ class DisplayKeywordRestriction(DisplayRestriction): self.keyword = keyword self.format = news_format - def checkRestriction(self, **kwargs): + def checkRestriction(self, **kwargs) -> bool: return kwargs["config"].get("ARCH", "") == self.keyword @@ -420,18 +438,23 @@ class DisplayInstalledRestriction(DisplayRestriction): self.atom = atom self.format = news_format - def isValid(self): + def isValid(self) -> bool: if fnmatch.fnmatch(self.format, "1.*"): return isvalidatom(self.atom, eapi="0") if fnmatch.fnmatch(self.format, "2.*"): return isvalidatom(self.atom, eapi="5") return isvalidatom(self.atom) - def checkRestriction(self, **kwargs): + def checkRestriction(self, **kwargs) -> bool: return kwargs["vardb"].match(self.atom) -def count_unread_news(portdb, vardb, repos=None, update=True): +def count_unread_news( + portdb: "portage.dbapi.porttree.portdbapi", + vardb: "portage.dbapi.vartree.vardbapi", + repos: Optional[List[Any]] = None, + update: bool = True, +) -> Dict[str, int]: """ Returns a dictionary mapping repos to integer counts of unread news items. By default, this will scan all repos and check for new items that have @@ -477,7 +500,7 @@ def count_unread_news(portdb, vardb, repos=None, update=True): return news_counts -def display_news_notifications(news_counts: dict): +def display_news_notifications(news_counts: Dict[Any, int]) -> None: """ Display a notification for unread news items, using a dictionary mapping repos to integer counts, like that returned from count_unread_news().