scripts: Adopted Attr class changes in all scripts

Mainly the addition of Attr.getall, Attr.get, and changing
Attr.__getitem__ to raise KeyError (just like a normal dict).
This commit is contained in:
Christopher Haster
2025-04-10 15:49:32 -05:00
parent b715e9a749
commit 06bb34fd99
9 changed files with 368 additions and 49 deletions
+47 -7
View File
@@ -3793,14 +3793,15 @@ class Attr:
if isinstance(key, tuple):
if len(key) > 0 and not isinstance(key[0], str):
i, key = key
if not isinstance(key, tuple):
key = (key,)
else:
i, key = 0, key
elif isinstance(key, str):
i, key = 0, (key,)
else:
i, key = key, ()
if not isinstance(key, tuple):
key = (key,)
# try to lookup by key
best = None
for ks, vs in self.keyed.items():
@@ -3824,10 +3825,49 @@ class Attr:
if self.defaults is not None:
return self.defaults[i, key]
return None
raise KeyError(i, key)
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
def __contains__(self, key):
return self.__getitem__(key) is not None
try:
self.__getitem__(key)
return True
except KeyError:
return False
# get all results for a given key
def getall(self, key, default=None):
if not isinstance(key, tuple):
key = (key,)
# try to lookup by key
best = None
for ks, vs in self.keyed.items():
prefix = []
for j, k in enumerate(ks):
if j < len(key) and fnmatch.fnmatchcase(key[j], k):
prefix.append(k)
else:
prefix = None
break
if prefix is not None and (
best is None or len(prefix) >= len(best[0])):
best = (prefix, vs)
if best is not None:
return best[1]
# fallback to defaults?
if self.defaults is not None:
return self.defaults.getall(key, default)
raise default
# a key function for sorting by key order
def key(self, key):
@@ -4597,7 +4637,7 @@ def main_(f, disk, mroots=None, *,
for b in bmap.values():
b.chars = {}
for type in [b.type] + (['unused'] if args.get('usage') else []):
char__ = chars_[b.block, (type, '0x%x' % b.block)]
char__ = chars_.get((b.block, (type, '0x%x' % b.block)))
if char__ is not None:
if isinstance(char__, str):
# don't punescape unless we have to
@@ -4610,7 +4650,7 @@ def main_(f, disk, mroots=None, *,
for b in bmap.values():
b.colors = {}
for type in [b.type] + (['unused'] if args.get('usage') else []):
color__ = colors_[b.block, (type, '0x%x' % b.block)]
color__ = colors_.get((b.block, (type, '0x%x' % b.block)))
if color__ is not None:
# don't punescape unless we have to
if '%' in color__: