mirror of
https://github.com/dgelessus/python-rsrcfork.git
synced 2025-01-21 18:31:12 +00:00
Refactor filter_resources in __main__
With the new implementation, each filter is converted to a function, then all resources are checked if they match any of the filter functions. This is simpler than the old implementation, where the resource lookup code was slightly different for some filter forms.
This commit is contained in:
parent
c009e8f80f
commit
2b0bbb19ed
@ -83,32 +83,13 @@ def bytes_escape(bs: bytes, *, quote: typing.Optional[str]=None) -> str:
|
||||
|
||||
return "".join(out)
|
||||
|
||||
def filter_resources(rf: api.ResourceFile, filters: typing.Sequence[str]) -> typing.List[api.Resource]:
|
||||
if not filters:
|
||||
resources = []
|
||||
for reses in rf.values():
|
||||
resources.extend(reses.values())
|
||||
return resources
|
||||
|
||||
matching: typing.MutableMapping[typing.Tuple[bytes, int], api.Resource] = collections.OrderedDict()
|
||||
|
||||
for filter in filters:
|
||||
def filter_to_predicate(filter: str) -> typing.Callable[[api.Resource], bool]:
|
||||
if len(filter) == 4:
|
||||
try:
|
||||
resources = rf[filter.encode("ascii")]
|
||||
except KeyError:
|
||||
continue
|
||||
|
||||
for res in resources.values():
|
||||
matching[res.type, res.id] = res
|
||||
restype = filter.encode("ascii")
|
||||
return lambda res: res.type == restype
|
||||
elif filter[0] == filter[-1] == "'":
|
||||
try:
|
||||
resources = rf[bytes_unescape(filter[1:-1])]
|
||||
except KeyError:
|
||||
continue
|
||||
|
||||
for res in resources.values():
|
||||
matching[res.type, res.id] = res
|
||||
restype = bytes_unescape(filter[1:-1])
|
||||
return lambda res: res.type == restype
|
||||
else:
|
||||
pos = filter.find("'", 1)
|
||||
if pos == -1:
|
||||
@ -131,36 +112,31 @@ def filter_resources(rf: api.ResourceFile, filters: typing.Sequence[str]) -> typ
|
||||
raise ValueError(f"Invalid filter {filter!r}: Resource ID must be parenthesized")
|
||||
resid_str = resid_str[1:-1]
|
||||
|
||||
try:
|
||||
resources = rf[restype]
|
||||
except KeyError:
|
||||
continue
|
||||
|
||||
if resid_str[0] == resid_str[-1] == '"':
|
||||
name = bytes_unescape(resid_str[1:-1])
|
||||
|
||||
for res in resources.values():
|
||||
if res.name == name:
|
||||
matching[res.type, res.id] = res
|
||||
break
|
||||
return lambda res: res.type == restype and res.name == name
|
||||
elif ":" in resid_str:
|
||||
if resid_str.count(":") > 1:
|
||||
raise ValueError(f"Invalid filter {filter!r}: Too many colons in ID range expression: {resid_str!r}")
|
||||
start_str, end_str = resid_str.split(":")
|
||||
start, end = int(start_str), int(end_str)
|
||||
|
||||
for res in resources.values():
|
||||
if start <= res.id <= end:
|
||||
matching[res.type, res.id] = res
|
||||
return lambda res: res.type == restype and start <= res.id <= end
|
||||
else:
|
||||
resid = int(resid_str)
|
||||
try:
|
||||
res = resources[resid]
|
||||
except KeyError:
|
||||
continue
|
||||
matching[res.type, res.id] = res
|
||||
return lambda res: res.type == restype and res.id == resid
|
||||
|
||||
return list(matching.values())
|
||||
def filter_resources(rf: api.ResourceFile, filters: typing.Sequence[str]) -> typing.Iterable[api.Resource]:
|
||||
if not filters:
|
||||
# Special case: an empty list of filters matches all resources rather than none
|
||||
for reses in rf.values():
|
||||
yield from reses.values()
|
||||
else:
|
||||
preds = [filter_to_predicate(filter) for filter in filters]
|
||||
|
||||
for reses in rf.values():
|
||||
for res in reses.values():
|
||||
if any(pred(res) for pred in preds):
|
||||
yield res
|
||||
|
||||
def hexdump(data: bytes) -> None:
|
||||
last_line = None
|
||||
@ -556,7 +532,7 @@ or rewritten by the shell.
|
||||
ns = ap.parse_args(args)
|
||||
|
||||
with open_resource_file(ns.file, fork=ns.fork) as rf:
|
||||
resources = filter_resources(rf, ns.filter)
|
||||
resources = list(filter_resources(rf, ns.filter))
|
||||
|
||||
if ns.sort:
|
||||
resources.sort(key=lambda res: (res.type, res.id))
|
||||
|
Loading…
x
Reference in New Issue
Block a user