mirror of
https://github.com/elliotnunn/tbxi.git
synced 2024-05-31 18:41:32 +00:00
f2353d63c7
Conceived in a dream, this feature turned out to be more of a bug. The whole idea of tbxi is to work with easily-editable text files and naked binaries. Patching is best accomplished with a separate script.
68 lines
1.3 KiB
Python
68 lines
1.3 KiB
Python
import importlib
|
|
|
|
import os
|
|
from os import path
|
|
|
|
|
|
FORMATS = '''
|
|
bootinfo
|
|
parcels
|
|
powerpc
|
|
supermario
|
|
'''.split()
|
|
|
|
|
|
class WrongFormat(Exception):
|
|
pass
|
|
|
|
|
|
def build_dir(p):
|
|
for fmt in FORMATS:
|
|
mod = importlib.import_module('..%s_build' % fmt, __name__)
|
|
try:
|
|
data = mod.build(p)
|
|
except WrongFormat:
|
|
continue
|
|
|
|
print(fmt)
|
|
return data
|
|
|
|
raise WrongFormat
|
|
|
|
|
|
def build(p):
|
|
parent = path.dirname(path.abspath(p))
|
|
name = path.basename(path.abspath(p))
|
|
|
|
# try building the file from a directory
|
|
for np in [p + '.src', p]:
|
|
try:
|
|
data = build_dir(np)
|
|
except WrongFormat:
|
|
pass
|
|
else:
|
|
break
|
|
else:
|
|
# fall back on just reading the file (boring, I know!)
|
|
with open(p, 'rb') as f:
|
|
data = f.read()
|
|
|
|
return data
|
|
|
|
|
|
def dump(binary, dest_path, toplevel=False):
|
|
if not toplevel:
|
|
with open(dest_path, 'wb') as f:
|
|
f.write(binary)
|
|
|
|
dest_path += '.src'
|
|
|
|
for fmt in FORMATS:
|
|
mod = importlib.import_module('..%s_dump' % fmt, __name__)
|
|
try:
|
|
mod.dump(binary, dest_path)
|
|
print(fmt)
|
|
break
|
|
except WrongFormat:
|
|
pass
|