prez/cli.py

100 lines
2.2 KiB
Python
Raw Normal View History

2020-07-30 01:08:39 -04:00
import sys
import io
import argparse
import time
2020-08-01 23:00:19 -04:00
from open_rfork import open_rfork
2020-08-02 10:42:15 -04:00
import traceback
2020-07-30 01:08:39 -04:00
from base import rObject
# 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-01 20:39:39 -04:00
# import all the resource types and constants into
# a dictionary to be the exec() local scope
import base
import window
import control
import menu
import sound
import rect
2020-08-02 15:45:00 -04:00
import version
import tool_startup
2020-08-08 18:33:12 -04:00
import icon
2020-08-01 20:39:39 -04:00
import constants
2020-08-01 20:39:39 -04:00
# could do: mod = importlib.import_module("base"), etc.
scope = {}
2020-08-08 18:33:12 -04:00
for mod in (base, window, control, menu, sound, rect, version, tool_startup, icon, constants):
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 01:08:39 -04:00
2020-08-01 20:39:39 -04:00
def execute(filename, scope):
2020-07-30 01:08:39 -04: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 01:08:39 -04:00
return True
pass
except Exception as e:
print(e)
2020-08-02 10:42:15 -04:00
print(traceback.format_exc())
2020-07-30 01:08:39 -04:00
return False
if __name__ == '__main__':
2020-08-01 20:39:39 -04: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")
2020-08-02 13:40:48 -04:00
p.add_argument('-x', action="store_true", help="Generate REZ data")
2020-08-01 20:39:39 -04:00
p.add_argument('-D', type=str, nargs='+', help='define a variable')
p.add_argument('--df', action="store_true", help="Write to a regular file")
p.add_argument('-o', metavar='file', type=str, help="Specify output file")
2020-07-30 01:08:39 -04:00
2020-08-01 20:39:39 -04:00
opts = p.parse_args()
2020-08-01 23:00:19 -04:00
df = opts.df or not sys.platform in ("win32", "darwin")
2020-08-01 20:39:39 -04:00
scope = rez_scope()
2020-08-01 23:00:19 -04:00
errors = 0
2020-08-01 20:39:39 -04:00
for f in opts.files:
ok = execute(f, scope)
2020-08-01 23:00:19 -04:00
if not ok: errors += 1
if errors > 0 : sys.exit(1)
if not opts.o: opts.rez = True
2020-08-02 13:40:48 -04:00
if opts.x: opts.rez = True
2020-07-30 01:08:39 -04:00
2020-08-01 23:00:19 -04:00
if df or opts.rez:
open_rfork = io.open
2020-07-30 01:08:39 -04:00
2020-08-01 23:00:19 -04:00
if opts.rez:
print("/* Generated on {} */".format(time.ctime()))
print('#include "types.rez"\n')
2020-08-02 13:40:48 -04:00
if opts.x: rObject.dump_hex()
else: rObject.dump_rez()
2020-08-01 23:00:19 -04:00
else:
with open_rfork(opts.o, "wb") as io:
rObject.save_resources(io)
2020-08-01 15:33:19 -04:00
2020-08-01 20:39:39 -04:00
rObject.dump_exports()
sys.exit(0)