1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" Command-line tool to run .enaml files.
"""
import optparse
import os
import signal
import sys
import types
from enaml import imports
from enaml.compat import read_source
from enaml.core.parser import parse
from enaml.core.enaml_compiler import EnamlCompiler
def main():
usage = 'usage: %prog [options] enaml_file [script arguments]'
parser = optparse.OptionParser(usage=usage, description=__doc__)
parser.allow_interspersed_args = False
parser.add_option(
'-c', '--component', default='Main', help='The component to view'
)
options, args = parser.parse_args()
if len(args) == 0:
print('No .enaml file specified')
sys.exit()
else:
enaml_file_path = args[0]
script_argv = args[1:]
enaml_code = read_source(enaml_file_path)
# Parse and compile the Enaml source into a code object
ast = parse(enaml_code, enaml_file_path)
code = EnamlCompiler.compile(ast, enaml_file_path)
# Create a proper module in which to execute the compiled code so
# that exceptions get reported with better meaning
module = types.ModuleType('__main__')
module.__file__ = os.path.abspath(enaml_file_path)
sys.modules['__main__'] = module
ns = module.__dict__
# Put the directory of the Enaml file first in the path so relative
# imports can work.
sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file_path)))
# Bung in the command line arguments.
sys.argv = [enaml_file_path] + script_argv
with imports():
exec(code, ns)
# Make sure ^C keeps working
signal.signal(signal.SIGINT, signal.SIG_DFL)
requested = options.component
if requested in ns:
from enaml.qt.qt_application import QtApplication
app = QtApplication("enaml-runner")
window = ns[requested]()
window.show()
window.send_to_front()
app.start()
elif 'main' in ns:
ns['main']()
else:
msg = "Could not find component '%s'" % options.component
print(msg)
if __name__ == '__main__':
main()
|