1 |
commit: 92c63ef74039e2361a4912ead5416b08cdfa8ac8 |
2 |
Author: David Palao <david.palao <AT> gmail <DOT> com> |
3 |
AuthorDate: Sun May 22 09:40:45 2022 +0000 |
4 |
Commit: Mike Gilbert <floppym <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Jun 7 23:47:54 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=92c63ef7 |
7 |
|
8 |
refactor(mtimedb): some little changes: |
9 |
|
10 |
- interpolated strings (%...) replaced by f-strings |
11 |
- some little simplifications |
12 |
- factor out some constants to prepare for unittesting |
13 |
|
14 |
Signed-off-by: David Palao <david.palao <AT> gmail.com> |
15 |
Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org> |
16 |
|
17 |
lib/portage/util/mtimedb.py | 44 ++++++++++++++++++++++---------------------- |
18 |
1 file changed, 22 insertions(+), 22 deletions(-) |
19 |
|
20 |
diff --git a/lib/portage/util/mtimedb.py b/lib/portage/util/mtimedb.py |
21 |
index 7a7fe6784..df9bd494c 100644 |
22 |
--- a/lib/portage/util/mtimedb.py |
23 |
+++ b/lib/portage/util/mtimedb.py |
24 |
@@ -23,7 +23,23 @@ from portage.localization import _ |
25 |
from portage.util import apply_secpass_permissions, atomic_ofstream, writemsg |
26 |
|
27 |
|
28 |
+_MTIMEDBKEYS = { |
29 |
+ "info", |
30 |
+ "ldpath", |
31 |
+ "resume", |
32 |
+ "resume_backup", |
33 |
+ "starttime", |
34 |
+ "updates", |
35 |
+ "version", |
36 |
+} |
37 |
+ |
38 |
+ |
39 |
class MtimeDB(dict): |
40 |
+ """The MtimeDB class is used to interact with a file storing the |
41 |
+ current resume lists. |
42 |
+ It is a subclass of ``dict`` and it reads from/writes to JSON, by |
43 |
+ default, althouth it can be configured to use ``pickle``. |
44 |
+ """ |
45 |
|
46 |
# JSON read support has been available since portage-2.1.10.49. |
47 |
_json_write = True |
48 |
@@ -46,13 +62,13 @@ class MtimeDB(dict): |
49 |
pass |
50 |
else: |
51 |
writemsg( |
52 |
- _("!!! Error loading '%s': %s\n") % (filename, e), noiselevel=-1 |
53 |
+ _(f"!!! Error loading '{filename}': {e}\n"), noiselevel=-1 |
54 |
) |
55 |
finally: |
56 |
if f is not None: |
57 |
f.close() |
58 |
|
59 |
- d = None |
60 |
+ d = {} |
61 |
if content: |
62 |
try: |
63 |
d = json.loads( |
64 |
@@ -75,12 +91,9 @@ class MtimeDB(dict): |
65 |
raise |
66 |
except Exception: |
67 |
writemsg( |
68 |
- _("!!! Error loading '%s': %s\n") % (filename, e), noiselevel=-1 |
69 |
+ _(f"!!! Error loading '{filename}': {e}\n"), noiselevel=-1 |
70 |
) |
71 |
|
72 |
- if d is None: |
73 |
- d = {} |
74 |
- |
75 |
if "old" in d: |
76 |
d["updates"] = d["old"] |
77 |
del d["old"] |
78 |
@@ -92,22 +105,9 @@ class MtimeDB(dict): |
79 |
for k in ("info", "ldpath", "updates"): |
80 |
d.setdefault(k, {}) |
81 |
|
82 |
- mtimedbkeys = set( |
83 |
- ( |
84 |
- "info", |
85 |
- "ldpath", |
86 |
- "resume", |
87 |
- "resume_backup", |
88 |
- "starttime", |
89 |
- "updates", |
90 |
- "version", |
91 |
- ) |
92 |
- ) |
93 |
- |
94 |
- for k in list(d): |
95 |
- if k not in mtimedbkeys: |
96 |
- writemsg(_("Deleting invalid mtimedb key: %s\n") % str(k)) |
97 |
- del d[k] |
98 |
+ for k in (d.keys()-_MTIMEDBKEYS): |
99 |
+ writemsg(_(f"Deleting invalid mtimedb key: {k}\n")) |
100 |
+ del d[k] |
101 |
self.update(d) |
102 |
self._clean_data = copy.deepcopy(d) |