mirror of
https://github.com/dgelessus/python-rsrcfork.git
synced 2024-09-28 00:54:49 +00:00
Add command-line --sort option
This commit is contained in:
parent
7612322c43
commit
8ad0234633
@ -130,6 +130,7 @@ Changelog
|
|||||||
Version 1.2.1 (next version)
|
Version 1.2.1 (next version)
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
* Added a ``--sort`` command line option to output resources sorted by type and ID, instead of using the order stored in the file.
|
||||||
* Added a ``dump-text`` output format to the command line tool. This format is identical to ``dump``, but instead of a hex dump, it outputs the resource data as text. The data is decoded as MacRoman and classic Mac newlines (``\r``) are translated. This is useful for examining resources that contain mostly plain text.
|
* Added a ``dump-text`` output format to the command line tool. This format is identical to ``dump``, but instead of a hex dump, it outputs the resource data as text. The data is decoded as MacRoman and classic Mac newlines (``\r``) are translated. This is useful for examining resources that contain mostly plain text.
|
||||||
* Changed ``--format=dump`` output to match ``hexdump -C``'s format - spacing has been adjusted, and multiple subsequent identical lines are collapsed into a single ``*``.
|
* Changed ``--format=dump`` output to match ``hexdump -C``'s format - spacing has been adjusted, and multiple subsequent identical lines are collapsed into a single ``*``.
|
||||||
|
|
||||||
|
@ -245,6 +245,7 @@ def _parse_args() -> argparse.Namespace:
|
|||||||
ap.add_argument("-f", "--fork", choices=["auto", "data", "rsrc"], default="auto", help="The fork from which to read the resource data, or auto to guess (default: %(default)s)")
|
ap.add_argument("-f", "--fork", choices=["auto", "data", "rsrc"], default="auto", help="The fork from which to read the resource data, or auto to guess (default: %(default)s)")
|
||||||
ap.add_argument("--no-decompress", action="store_false", dest="decompress", help="Do not decompress compressed resources, output compressed resource data as-is")
|
ap.add_argument("--no-decompress", action="store_false", dest="decompress", help="Do not decompress compressed resources, output compressed resource data as-is")
|
||||||
ap.add_argument("--format", choices=["dump", "dump-text", "hex", "raw", "derez"], default="dump", help="How to output the resources - human-readable info with hex dump (dump) (default), human-readable info with newline-translated data (dump-text), data only as hex (hex), data only as raw bytes (raw), or like DeRez with no resource definitions (derez)")
|
ap.add_argument("--format", choices=["dump", "dump-text", "hex", "raw", "derez"], default="dump", help="How to output the resources - human-readable info with hex dump (dump) (default), human-readable info with newline-translated data (dump-text), data only as hex (hex), data only as raw bytes (raw), or like DeRez with no resource definitions (derez)")
|
||||||
|
ap.add_argument("--sort", action="store_true", help="Output resources sorted by type and ID, instead of the order in which they are stored in the file")
|
||||||
ap.add_argument("--header-system", action="store_true", help="Output system-reserved header data and nothing else")
|
ap.add_argument("--header-system", action="store_true", help="Output system-reserved header data and nothing else")
|
||||||
ap.add_argument("--header-application", action="store_true", help="Output application-specific header data and nothing else")
|
ap.add_argument("--header-application", action="store_true", help="Output application-specific header data and nothing else")
|
||||||
|
|
||||||
@ -355,7 +356,7 @@ def _show_filtered_resources(resources: typing.Sequence[api.Resource], format: s
|
|||||||
else:
|
else:
|
||||||
raise ValueError(f"Unhandled output format: {format}")
|
raise ValueError(f"Unhandled output format: {format}")
|
||||||
|
|
||||||
def _list_resource_file(rf: api.ResourceFile, *, decompress: bool) -> None:
|
def _list_resource_file(rf: api.ResourceFile, *, sort: bool, decompress: bool) -> None:
|
||||||
if rf.header_system_data != bytes(len(rf.header_system_data)):
|
if rf.header_system_data != bytes(len(rf.header_system_data)):
|
||||||
print("Header system data:")
|
print("Header system data:")
|
||||||
_hexdump(rf.header_system_data)
|
_hexdump(rf.header_system_data)
|
||||||
@ -370,10 +371,16 @@ def _list_resource_file(rf: api.ResourceFile, *, decompress: bool) -> None:
|
|||||||
|
|
||||||
if len(rf) > 0:
|
if len(rf) > 0:
|
||||||
print(f"{len(rf)} resource types:")
|
print(f"{len(rf)} resource types:")
|
||||||
for typecode, resources in rf.items():
|
restype_items = rf.items()
|
||||||
|
if sort:
|
||||||
|
restype_items = sorted(restype_items, key=lambda item: item[0])
|
||||||
|
for typecode, resources in restype_items:
|
||||||
restype = _bytes_escape(typecode, quote="'")
|
restype = _bytes_escape(typecode, quote="'")
|
||||||
print(f"'{restype}': {len(resources)} resources:")
|
print(f"'{restype}': {len(resources)} resources:")
|
||||||
for resid, res in rf[typecode].items():
|
resources_items = resources.items()
|
||||||
|
if sort:
|
||||||
|
resources_items = sorted(resources_items, key=lambda item: item[0])
|
||||||
|
for resid, res in resources_items:
|
||||||
print(_describe_resource(res, include_type=False, decompress=decompress))
|
print(_describe_resource(res, include_type=False, decompress=decompress))
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
@ -407,9 +414,12 @@ def main():
|
|||||||
for reses in rf.values():
|
for reses in rf.values():
|
||||||
resources.extend(reses.values())
|
resources.extend(reses.values())
|
||||||
|
|
||||||
|
if ns.sort:
|
||||||
|
resources.sort(key=lambda res: (res.resource_type, res.resource_id))
|
||||||
|
|
||||||
_show_filtered_resources(resources, format=ns.format, decompress=ns.decompress)
|
_show_filtered_resources(resources, format=ns.format, decompress=ns.decompress)
|
||||||
else:
|
else:
|
||||||
_list_resource_file(rf, decompress=ns.decompress)
|
_list_resource_file(rf, sort=ns.sort, decompress=ns.decompress)
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user