From 9ab7a7846161bb122ae3a2442363b475a384ad3e Mon Sep 17 00:00:00 2001 From: Elliot Nunn Date: Fri, 4 Jan 2019 23:20:49 +0800 Subject: [PATCH] Add resource-stdout tool --- bin/Rget | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 bin/Rget diff --git a/bin/Rget b/bin/Rget new file mode 100755 index 0000000..e5bfe9c --- /dev/null +++ b/bin/Rget @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import argparse +import macresources +import sys + +def fourcc(s): + b = s.encode('mac_roman').ljust(4, b' ') + if len(b) != 4: + raise ValueError('wrong length') + return b + +def seemshex(s): + s = s.lower() + if not s: return False + return all(c in '0123456789abcdef' for c in s) + +def seemsdec(s): + if not s: return False + return all(c in '0123456789' for c in s) + +def resid(s): + s = s.lower() + if s.startswith('0x') and seemshex(s[2:]): + thenum = int(s[2:], 16) + if thenum > 0x7fff: thenum -= 0x8000 + elif s.startswith('$') and seemshex(s[1:]): + thenum = int(s[1:], 16) + if thenum > 0x7fff: thenum -= 0x8000 + else: + thenum = int(s) + if not (-0x8000 <= thenum <= 0x7fff): raise ValueError + return thenum + +parser = argparse.ArgumentParser(description=''' + Copy a single MacOS resource from a source file to the standard + output. If the source filename ends with `.r' or `.rdump', then it + is parsed using the `SimpleRez' subset of the Rez language. TODO: + find a way to specify null characters so that desk accessories can + be looked up by name. +''') + +parser.add_argument('srcfile', help='resource file or Rez file') +parser.add_argument('type', type=fourcc, help='four-byte type of resource (converted to Mac Roman pre-lookup)') +parser.add_argument('id', type=resid, help='ID number of resource (-32768 to 32767)') + +args = parser.parse_args() + +with open(args.srcfile, 'rb') as f: + raw = f.read() + +if args.srcfile.endswith('.r') or args.srcfile.endswith('.rdump'): + resources = macresources.parse_rez_code(raw) +else: + resources = macresources.parse_file(raw) + +for r in resources: + if r.type == args.type and r.id == args.id: + try: + sys.stdout.buffer.write(r.data) + except BrokenPipeError: + pass + break +else: + raise ValueError(args.type, args.id) diff --git a/setup.py b/setup.py index 17436a0..b74d038 100644 --- a/setup.py +++ b/setup.py @@ -18,5 +18,5 @@ setup( 'Development Status :: 3 - Alpha', ], packages=['macresources'], - scripts=['bin/SimpleRez', 'bin/SimpleDeRez'], + scripts=['bin/SimpleRez', 'bin/SimpleDeRez', 'bin/Rget'], )