1 |
commit: fb4ba0ef70095bd6f17e198674e16d60284ab2dc |
2 |
Author: Sam James <sam <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Feb 27 05:10:23 2023 +0000 |
4 |
Commit: Sam James <sam <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Feb 27 06:15:40 2023 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=fb4ba0ef |
7 |
|
8 |
portage: news: trivial type annotations |
9 |
|
10 |
Signed-off-by: Sam James <sam <AT> gentoo.org> |
11 |
|
12 |
lib/portage/news.py | 59 +++++++++++++++++++++++++++++++++++++---------------- |
13 |
1 file changed, 41 insertions(+), 18 deletions(-) |
14 |
|
15 |
diff --git a/lib/portage/news.py b/lib/portage/news.py |
16 |
index f81debe97..68f0c72d3 100644 |
17 |
--- a/lib/portage/news.py |
18 |
+++ b/lib/portage/news.py |
19 |
@@ -14,11 +14,17 @@ __all__ = [ |
20 |
] |
21 |
|
22 |
from collections import OrderedDict |
23 |
+from typing import TYPE_CHECKING, Any, Dict, List, Optional |
24 |
|
25 |
import fnmatch |
26 |
import logging |
27 |
import os as _os |
28 |
import re |
29 |
+ |
30 |
+if TYPE_CHECKING: |
31 |
+ import portage.dbapi.vartree |
32 |
+ import portage.package.ebuild.config |
33 |
+ |
34 |
from portage import os |
35 |
from portage import _encodings |
36 |
from portage import _unicode_decode |
37 |
@@ -58,7 +64,14 @@ class NewsManager: |
38 |
|
39 |
""" |
40 |
|
41 |
- def __init__(self, portdb, vardb, news_path, unread_path, language_id="en"): |
42 |
+ def __init__( |
43 |
+ self, |
44 |
+ portdb: "portage.dbapi.porttree.portdbapi", |
45 |
+ vardb: "portage.dbapi.vartree.vardbapi", |
46 |
+ news_path: str, |
47 |
+ unread_path: str, |
48 |
+ language_id: str = "en", |
49 |
+ ): |
50 |
self.news_path = news_path |
51 |
self.unread_path = unread_path |
52 |
self.language_id = language_id |
53 |
@@ -89,19 +102,19 @@ class NewsManager: |
54 |
profile_path = profile_path[len(profiles_base) :] |
55 |
self._profile_path = profile_path |
56 |
|
57 |
- def _unread_filename(self, repoid): |
58 |
+ def _unread_filename(self, repoid: str) -> str: |
59 |
return os.path.join(self.unread_path, f"news-{repoid}.unread") |
60 |
|
61 |
- def _skip_filename(self, repoid): |
62 |
+ def _skip_filename(self, repoid: str) -> str: |
63 |
return os.path.join(self.unread_path, f"news-{repoid}.skip") |
64 |
|
65 |
- def _news_dir(self, repoid): |
66 |
+ def _news_dir(self, repoid: str) -> str: |
67 |
repo_path = self.portdb.getRepositoryPath(repoid) |
68 |
if repo_path is None: |
69 |
raise AssertionError(_(f"Invalid repoID: {repoid}")) |
70 |
return os.path.join(repo_path, self.news_path) |
71 |
|
72 |
- def updateItems(self, repoid): |
73 |
+ def updateItems(self, repoid: str) -> None: |
74 |
""" |
75 |
Figure out which news items from NEWS_PATH are both unread and relevant to |
76 |
the user (according to the GLEP 42 standards of relevancy). Then add these |
77 |
@@ -199,7 +212,7 @@ class NewsManager: |
78 |
finally: |
79 |
unlockfile(unread_lock) |
80 |
|
81 |
- def getUnreadItems(self, repoid, update=False): |
82 |
+ def getUnreadItems(self, repoid: str, update: bool = False) -> int: |
83 |
""" |
84 |
Determine if there are unread relevant items in news.repoid.unread. |
85 |
If there are unread items return their number. |
86 |
@@ -249,7 +262,7 @@ class NewsItem: |
87 |
Creation of a news item involves passing in the path to the particular news item. |
88 |
""" |
89 |
|
90 |
- def __init__(self, path, name): |
91 |
+ def __init__(self, path: str, name: str): |
92 |
""" |
93 |
For a given news item we only want if it path is a file. |
94 |
""" |
95 |
@@ -258,7 +271,12 @@ class NewsItem: |
96 |
self._parsed = False |
97 |
self._valid = True |
98 |
|
99 |
- def isRelevant(self, vardb, config, profile): |
100 |
+ def isRelevant( |
101 |
+ self, |
102 |
+ vardb: "portage.dbapi.vartree.vardbapi", |
103 |
+ config: "portage.package.ebuild.config.config", |
104 |
+ profile: Optional[str], |
105 |
+ ) -> bool: |
106 |
""" |
107 |
This function takes a dict of keyword arguments; one should pass in any |
108 |
objects need to do to lookups (like what keywords we are on, what profile, |
109 |
@@ -292,12 +310,12 @@ class NewsItem: |
110 |
|
111 |
return all_match |
112 |
|
113 |
- def isValid(self): |
114 |
+ def isValid(self) -> bool: |
115 |
if not self._parsed: |
116 |
self.parse() |
117 |
return self._valid |
118 |
|
119 |
- def parse(self): |
120 |
+ def parse(self) -> None: |
121 |
with open( |
122 |
_unicode_encode(self.path, encoding=_encodings["fs"], errors="strict"), |
123 |
encoding=_encodings["content"], |
124 |
@@ -365,7 +383,7 @@ class DisplayRestriction: |
125 |
are met, then it is displayed |
126 |
""" |
127 |
|
128 |
- def isValid(self): |
129 |
+ def isValid(self) -> bool: |
130 |
return True |
131 |
|
132 |
def checkRestriction(self, **kwargs): |
133 |
@@ -382,7 +400,7 @@ class DisplayProfileRestriction(DisplayRestriction): |
134 |
self.profile = profile |
135 |
self.format = news_format |
136 |
|
137 |
- def isValid(self): |
138 |
+ def isValid(self) -> bool: |
139 |
return ( |
140 |
not fnmatch.fnmatch(self.format, "1.*") |
141 |
or "*" not in self.profile |
142 |
@@ -390,7 +408,7 @@ class DisplayProfileRestriction(DisplayRestriction): |
143 |
or _valid_profile_RE.match(self.profile) |
144 |
) |
145 |
|
146 |
- def checkRestriction(self, **kwargs): |
147 |
+ def checkRestriction(self, **kwargs) -> bool: |
148 |
if fnmatch.fnmatch(self.format, "2.*") and self.profile.endswith("/*"): |
149 |
return kwargs["profile"].startswith(self.profile[:-1]) |
150 |
return kwargs["profile"] == self.profile |
151 |
@@ -406,7 +424,7 @@ class DisplayKeywordRestriction(DisplayRestriction): |
152 |
self.keyword = keyword |
153 |
self.format = news_format |
154 |
|
155 |
- def checkRestriction(self, **kwargs): |
156 |
+ def checkRestriction(self, **kwargs) -> bool: |
157 |
return kwargs["config"].get("ARCH", "") == self.keyword |
158 |
|
159 |
|
160 |
@@ -420,18 +438,23 @@ class DisplayInstalledRestriction(DisplayRestriction): |
161 |
self.atom = atom |
162 |
self.format = news_format |
163 |
|
164 |
- def isValid(self): |
165 |
+ def isValid(self) -> bool: |
166 |
if fnmatch.fnmatch(self.format, "1.*"): |
167 |
return isvalidatom(self.atom, eapi="0") |
168 |
if fnmatch.fnmatch(self.format, "2.*"): |
169 |
return isvalidatom(self.atom, eapi="5") |
170 |
return isvalidatom(self.atom) |
171 |
|
172 |
- def checkRestriction(self, **kwargs): |
173 |
+ def checkRestriction(self, **kwargs) -> bool: |
174 |
return kwargs["vardb"].match(self.atom) |
175 |
|
176 |
|
177 |
-def count_unread_news(portdb, vardb, repos=None, update=True): |
178 |
+def count_unread_news( |
179 |
+ portdb: "portage.dbapi.porttree.portdbapi", |
180 |
+ vardb: "portage.dbapi.vartree.vardbapi", |
181 |
+ repos: Optional[List[Any]] = None, |
182 |
+ update: bool = True, |
183 |
+) -> Dict[str, int]: |
184 |
""" |
185 |
Returns a dictionary mapping repos to integer counts of unread news items. |
186 |
By default, this will scan all repos and check for new items that have |
187 |
@@ -477,7 +500,7 @@ def count_unread_news(portdb, vardb, repos=None, update=True): |
188 |
return news_counts |
189 |
|
190 |
|
191 |
-def display_news_notifications(news_counts: dict): |
192 |
+def display_news_notifications(news_counts: Dict[Any, int]) -> None: |
193 |
""" |
194 |
Display a notification for unread news items, using a dictionary mapping |
195 |
repos to integer counts, like that returned from count_unread_news(). |