mirror of
https://github.com/elliotnunn/macresources.git
synced 2024-12-12 18:30:08 +00:00
Bidirectional Rhqx
This commit is contained in:
parent
7b25e03e64
commit
123cd3294e
50
bin/Rhqx
50
bin/Rhqx
@ -1,22 +1,58 @@
|
||||
import os
|
||||
from os import path
|
||||
import argparse
|
||||
import macresources
|
||||
from macresources import binhex
|
||||
|
||||
parser = argparse.ArgumentParser(description='''
|
||||
Convert a "rdump" file (provide base name) to hqx
|
||||
Supply base name to convert datafork+rdump+idump to HQX.
|
||||
Supply base.hqx name to convert HQX to datafork+rdump+idump.
|
||||
''')
|
||||
|
||||
parser.add_argument('srcfile', help='base name (.rdump/.idump should be alongside)')
|
||||
parser.add_argument('srcfile', nargs='*', help='base or base.hqx')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
for srcfile in args.srcfile:
|
||||
# Case 1: from BinHex
|
||||
if path.splitext(path.basename(srcfile))[1].lower() == '.hqx':
|
||||
hb = binhex.HexBin(srcfile)
|
||||
|
||||
base = path.splitext(srcfile)[0]
|
||||
|
||||
if hb.FInfo.Type == hb.FInfo.Creator == b'????':
|
||||
try:
|
||||
os.remove(base + '.idump')
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
with open(base + '.idump', 'wb') as f:
|
||||
f.write(hb.FInfo.Type + hb.FInfo.Creator)
|
||||
|
||||
data = hb.read()
|
||||
if hb.FInfo.Type in [b'TEXT', b'ttro']:
|
||||
data = data.replace(b'\r', b'\n').decode('mac_roman').encode('utf-8')
|
||||
with open(base, 'wb') as f:
|
||||
f.write(data)
|
||||
|
||||
rsrc = hb.read_rsrc()
|
||||
if rsrc:
|
||||
with open(base + '.rdump', 'wb') as f:
|
||||
f.write(macresources.make_rez_code(macresources.parse_file(rsrc), ascii_clean=True))
|
||||
else:
|
||||
try:
|
||||
os.remove(base + '.rdump')
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
# Case 2: to BinHex
|
||||
else:
|
||||
finfo = binhex.FInfo()
|
||||
finfo.Flags = 0
|
||||
|
||||
try:
|
||||
info = open(args.srcfile + '.idump', 'rb').read(8)
|
||||
info = open(srcfile + '.idump', 'rb').read(8)
|
||||
assert len(info) == 8
|
||||
finfo.Type = info[:4]
|
||||
finfo.Creator = info[4:]
|
||||
@ -24,19 +60,19 @@ except:
|
||||
pass
|
||||
|
||||
try:
|
||||
data = open(args.srcfile, 'rb').read()
|
||||
data = open(srcfile, 'rb').read()
|
||||
if finfo.Type in [b'TEXT', b'ttro']:
|
||||
data = data.decode('utf-8').encode('mac_roman')
|
||||
data = data.replace(b'\n', b'\r').decode('utf-8').encode('mac_roman')
|
||||
except:
|
||||
data = b''
|
||||
|
||||
try:
|
||||
rsrc = open(args.srcfile + '.rdump', 'rb').read()
|
||||
rsrc = open(srcfile + '.rdump', 'rb').read()
|
||||
rsrc = macresources.make_file(macresources.parse_rez_code(rsrc))
|
||||
except:
|
||||
rsrc = b''
|
||||
|
||||
bh = binhex.BinHex((path.basename(args.srcfile), finfo, len(data), len(rsrc)), args.srcfile + '.hqx')
|
||||
bh = binhex.BinHex((path.basename(srcfile), finfo, len(data), len(rsrc)), srcfile + '.hqx')
|
||||
|
||||
bh.write(data)
|
||||
bh.write_rsrc(rsrc)
|
||||
|
Loading…
Reference in New Issue
Block a user