diff options
author | Siddhanth Rathod <xsiddhanthrathod@gmail.com> | 2023-03-01 23:25:48 +0530 |
---|---|---|
committer | Sam James <sam@gentoo.org> | 2023-03-04 02:56:34 +0000 |
commit | e42940471a16159802bc99a542a5624d9d46251e (patch) | |
tree | b1273b9ee4ccd1c286a4bd9fb53debe52331804a | |
parent | install-qa-check.d/90config-impl-decl: prefix warning with 'QA Notice' (diff) | |
download | portage-e4294047.tar.gz portage-e4294047.tar.bz2 portage-e4294047.zip |
portage: news: further type annotations
Signed-off-by: Siddhanth Rathod <xsiddhanthrathod@gmail.com>
Closes: https://github.com/gentoo/portage/pull/1002
Signed-off-by: Sam James <sam@gentoo.org>
-rw-r--r-- | lib/portage/news.py | 52 | ||||
-rw-r--r-- | lib/portage/util/__init__.py | 42 |
2 files changed, 47 insertions, 47 deletions
diff --git a/lib/portage/news.py b/lib/portage/news.py index 68f0c72d3..b43d81fb7 100644 --- a/lib/portage/news.py +++ b/lib/portage/news.py @@ -15,7 +15,7 @@ __all__ = [ from collections import OrderedDict from typing import TYPE_CHECKING, Any, Dict, List, Optional - +from typing import Pattern, Match import fnmatch import logging import os as _os @@ -71,7 +71,7 @@ class NewsManager: news_path: str, unread_path: str, language_id: str = "en", - ): + ) -> None: self.news_path = news_path self.unread_path = unread_path self.language_id = language_id @@ -89,11 +89,11 @@ class NewsManager: self._dir_mode = 0o0074 self._mode_mask = 0o0000 - portdir = portdb.repositories.mainRepoLocation() - profiles_base = None + portdir: Optional[str] = portdb.repositories.mainRepoLocation() + profiles_base: Optional[str] = None if portdir is not None: profiles_base = os.path.join(portdir, ("profiles" + os.path.sep)) - profile_path = None + profile_path: Optional[str] = None if profiles_base is not None and portdb.settings.profile_path: profile_path = normalize_path( os.path.realpath(portdb.settings.profile_path) @@ -109,7 +109,7 @@ class NewsManager: return os.path.join(self.unread_path, f"news-{repoid}.skip") def _news_dir(self, repoid: str) -> str: - repo_path = self.portdb.getRepositoryPath(repoid) + repo_path: Optional[str] = self.portdb.getRepositoryPath(repoid) if repo_path is None: raise AssertionError(_(f"Invalid repoID: {repoid}")) return os.path.join(repo_path, self.news_path) @@ -137,23 +137,23 @@ class NewsManager: if not os.access(self.unread_path, os.W_OK): return - news_dir = self._news_dir(repoid) + news_dir: str = self._news_dir(repoid) try: - news = _os.listdir( + news: list[str] = _os.listdir( _unicode_encode(news_dir, encoding=_encodings["fs"], errors="strict") ) except OSError: return - skip_filename = self._skip_filename(repoid) - unread_filename = self._unread_filename(repoid) - unread_lock = lockfile(unread_filename, wantnewlockfile=1) + skip_filename: str = self._skip_filename(repoid) + unread_filename: str = self._unread_filename(repoid) + unread_lock: Optional[bool] = lockfile(unread_filename, wantnewlockfile=1) try: try: - unread = set(grabfile(unread_filename)) - unread_orig = unread.copy() - skip = set(grabfile(skip_filename)) - skip_orig = skip.copy() + unread: set[str | tuple[str, str]] = set(grabfile(unread_filename)) + unread_orig: set[str | tuple[str, str]] = unread.copy() + skip: set[str | tuple[str, str]] = set(grabfile(skip_filename)) + skip_orig: set[str | tuple[str, str]] = skip.copy() except PermissionDenied: return @@ -224,7 +224,7 @@ class NewsManager: self.updateItems(repoid) unread_filename = self._unread_filename(repoid) - unread_lock = None + unread_lock: Optional[bool] = None try: unread_lock = lockfile(unread_filename, wantnewlockfile=1) except ( @@ -244,11 +244,11 @@ class NewsManager: unlockfile(unread_lock) -_formatRE = re.compile(r"News-Item-Format:\s*([^\s]*)\s*$") -_installedRE = re.compile("Display-If-Installed:(.*)\n") -_profileRE = re.compile("Display-If-Profile:(.*)\n") -_keywordRE = re.compile("Display-If-Keyword:(.*)\n") -_valid_profile_RE = re.compile(r"^[^*]+(/\*)?$") +_formatRE: Pattern[str] = re.compile(r"News-Item-Format:\s*([^\s]*)\s*$") +_installedRE: Pattern[str] = re.compile("Display-If-Installed:(.*)\n") +_profileRE: Pattern[str] = re.compile("Display-If-Profile:(.*)\n") +_keywordRE: Pattern[str] = re.compile("Display-If-Keyword:(.*)\n") +_valid_profile_RE: Pattern[str] = re.compile(r"^[^*]+(/\*)?$") class NewsItem: @@ -295,9 +295,9 @@ class NewsItem: if not len(self.restrictions): return True - kwargs = {"vardb": vardb, "config": config, "profile": profile} + kwargs: dict[str, Any] = {"vardb": vardb, "config": config, "profile": profile} - all_match = True + all_match: bool = True for values in self.restrictions.values(): matches = [restriction.checkRestriction(**kwargs) for restriction in values] any_match = any(matches) @@ -324,11 +324,11 @@ class NewsItem: lines = f.readlines() self.restrictions = {} invalids = [] - news_format = None + news_format: Optional[str] = None # Look for News-Item-Format for i, line in enumerate(lines): - format_match = _formatRE.match(line) + format_match: Optional[Match[str]] = _formatRE.match(line) if format_match is not None: news_format = format_match.group(1) if fnmatch.fnmatch(news_format, "[12].*"): @@ -445,7 +445,7 @@ class DisplayInstalledRestriction(DisplayRestriction): return isvalidatom(self.atom, eapi="5") return isvalidatom(self.atom) - def checkRestriction(self, **kwargs) -> bool: + def checkRestriction(self, **kwargs) -> Optional[Match[str]]: return kwargs["vardb"].match(self.atom) diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py index 59db742d7..4d50088e7 100644 --- a/lib/portage/util/__init__.py +++ b/lib/portage/util/__init__.py @@ -1,6 +1,26 @@ # Copyright 2004-2020 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 +from portage.cache.mappings import UserDict +from portage.proxy.objectproxy import ObjectProxy +from portage.localization import _ +from portage.exception import ( + InvalidAtom, + PortageException, + FileNotFound, + IsADirectory, + OperationNotPermitted, + ParseError, + PermissionDenied, + ReadOnlyFileSystem, +) +from portage.const import VCS_DIRS +from portage import _unicode_decode +from portage import _unicode_encode +from portage import _os_merge +from portage import _encodings +from portage import os + __all__ = [ "apply_permissions", "apply_recursive_permissions", @@ -61,26 +81,6 @@ portage.proxy.lazyimport.lazyimport( "subprocess", ) -from portage import os -from portage import _encodings -from portage import _os_merge -from portage import _unicode_encode -from portage import _unicode_decode -from portage.const import VCS_DIRS -from portage.exception import ( - InvalidAtom, - PortageException, - FileNotFound, - IsADirectory, - OperationNotPermitted, - ParseError, - PermissionDenied, - ReadOnlyFileSystem, -) -from portage.localization import _ -from portage.proxy.objectproxy import ObjectProxy -from portage.cache.mappings import UserDict - noiselimit = 0 @@ -142,7 +142,7 @@ def writemsg_level(msg, level=0, noiselevel=0): writemsg(msg, noiselevel=noiselevel, fd=fd) -def normalize_path(mypath): +def normalize_path(mypath) -> str: """ os.path.normpath("//foo") returns "//foo" instead of "/foo" We dislike this behavior so we create our own normpath func |