prez/prez/cli.py

104 lines
2.4 KiB
Python
Raw Normal View History

2020-07-30 05:08:39 +00:00
import sys
import io
import argparse
import time
2020-08-02 14:42:15 +00:00
import traceback
2023-12-31 23:21:22 +00:00
import importlib
from . open_rfork import open_rfork
from . base import rObject
2020-07-30 05:08:39 +00:00
# from base import *
# from window import *
# from control import *
# from menu import *
# from sound import *
# from rect import rect, point, size
def rez_scope():
2020-08-02 00:39:39 +00:00
# import all the resource types and constants into
# a dictionary to be the exec() local scope
2023-12-31 23:21:22 +00:00
# import prez.base
# import prez.window
# import prez.control
# import prez.menu
# import prez.sound
# import prez.rect
# import prez.version
# import prez.tool_startup
# import prez.icon
# import prez.constants
2020-08-02 00:39:39 +00:00
# could do: mod = importlib.import_module("base"), etc.
scope = {}
2023-12-31 23:21:22 +00:00
for m in ('base', 'window', 'control', 'menu', 'sound', 'rect', 'version', 'tool_startup', 'icon', 'constants'):
mod = importlib.import_module('.' + m, 'prez')
if hasattr(mod, '__all__'): keys = mod.__all__
else: keys = [x for x in dir(mod) if x[0] != '_']
for key in keys:
scope[key] = getattr(mod, key)
return scope
2020-07-30 05:08:39 +00:00
2020-08-02 00:39:39 +00:00
def execute(filename, scope):
2020-07-30 05:08:39 +00:00
try:
with open(filename, 'r', encoding="utf-8") as f:
src = f.read()
code = compile(src, filename, "exec")
exec(code, {}, scope)
2020-07-30 05:08:39 +00:00
return True
pass
except Exception as e:
print(e)
2020-08-02 14:42:15 +00:00
print(traceback.format_exc())
2020-07-30 05:08:39 +00:00
return False
2023-12-31 23:21:22 +00:00
def main():
2020-08-02 00:39:39 +00:00
p = argparse.ArgumentParser(prog='prez')
p.add_argument('files', metavar='file', type=str, nargs='+')
p.add_argument('--rez', action='store_true', help="Generate REZ code")
2023-12-31 23:21:22 +00:00
p.add_argument('--hex', action="store_true", help="Generate REZ data")
2020-08-02 17:40:48 +00:00
2020-08-02 00:39:39 +00:00
p.add_argument('-D', type=str, nargs='+', help='define a variable')
2023-12-31 23:21:22 +00:00
p.add_argument('--data-fork', action="store_true", help="Write to a regular file")
2020-08-02 00:39:39 +00:00
p.add_argument('-o', metavar='file', type=str, help="Specify output file")
2020-07-30 05:08:39 +00:00
2020-08-02 00:39:39 +00:00
opts = p.parse_args()
2023-12-31 23:21:22 +00:00
opts.data_fork = opts.data_fork or not sys.platform in ("win32", "darwin")
2020-08-02 03:00:19 +00:00
2020-08-02 00:39:39 +00:00
scope = rez_scope()
2020-08-02 03:00:19 +00:00
errors = 0
2020-08-02 00:39:39 +00:00
for f in opts.files:
ok = execute(f, scope)
2020-08-02 03:00:19 +00:00
if not ok: errors += 1
if errors > 0 : sys.exit(1)
if not opts.o: opts.rez = True
2020-07-30 05:08:39 +00:00
2023-12-31 23:21:22 +00:00
if opts.rez or opts.hex:
2020-08-02 03:00:19 +00:00
print("/* Generated on {} */".format(time.ctime()))
print('#include "types.rez"\n')
2023-12-31 23:21:22 +00:00
if opts.hex: rObject.dump_hex()
2020-08-02 17:40:48 +00:00
else: rObject.dump_rez()
2020-08-02 03:00:19 +00:00
else:
2023-12-31 23:21:22 +00:00
opener = open_rfork
if opts.data_fork: opener = io.open
with opener(opts.o, "wb") as f:
rObject.save_resources(f)
2020-08-01 19:33:19 +00:00
2020-08-02 00:39:39 +00:00
rObject.dump_exports()
sys.exit(0)
2023-12-31 23:21:22 +00:00
if __name__ == '__main__':
main()