Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #230 from enthought/sync-enaml-with-chaco
Browse files Browse the repository at this point in the history
Sync enaml with chaco
  • Loading branch information
sccolbert committed Jan 7, 2013
2 parents 3e7d596 + e945f49 commit f864975
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions enaml/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,72 @@
import os
import sys
import types
import warnings

from enaml import imports
from enaml.stdlib.sessions import show_simple_view
from enaml.core.parser import parse
from enaml.core.enaml_compiler import EnamlCompiler


def prepare_toolkit(toolkit_option):
""" Prepare the toolkit to be used by Enaml.
This function determines the Enaml toolkit based on the values of
the toolkit option and the ETS_TOOLKIT environment variable. ETS
gui components default to Wx when ETS_TOOLKIT is not defined, but
Enaml defaults to Qt. Under this condition, ETS_TOOLKIT is updated
to be consistent with Enaml. If ETS_TOOLKIT is already set and it
is incompatibile with the -t option, a warning is raised.
Parameters
----------
toolkit_option : str
The toolkit option provided to the enaml-run script.
Returns
-------
result : str
The toolkit to be used by enaml.
"""
if 'ETS_TOOLKIT' in os.environ:
ets_toolkit = os.environ['ETS_TOOLKIT'].lower().split('.')[0][:2]
if toolkit_option == 'default' and ets_toolkit in ('wx', 'qt'):
enaml_toolkit = ets_toolkit
else:
enaml_toolkit = 'wx' if toolkit_option == 'wx' else 'qt'
if ets_toolkit != enaml_toolkit:
msg = (
'The --toolkit option is different from the ETS_TOOLKIT '
'environment variable, which can cause incompatibility '
'issues when using enable or chaco components.'
)
warnings.warn(msg)
else:
if toolkit_option == 'wx':
enaml_toolkit = 'wx'
os.environ['ETS_TOOLKIT'] = 'wx'
else:
enaml_toolkit = 'qt'
os.environ['ETS_TOOLKIT'] = 'qt4'
return enaml_toolkit


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')
parser.add_option('-t', '--toolkit', default='qt',
help='The GUI toolikit to use')
parser.add_option(
'-c', '--component', default='Main', help='The component to view'
)
parser.add_option(
'-t', '--toolkit', default='default',
help='The GUI toolkit to use [default: qt or ETS_TOOLKIT].'
)

options, args = parser.parse_args()
toolkit = prepare_toolkit(options.toolkit)

if len(args) == 0:
print 'No .enaml file specified'
Expand All @@ -47,8 +96,8 @@ def main():
module.__file__ = enaml_file
ns = module.__dict__

# Put the directory of the Enaml file first in the path so relative imports
# can work.
# 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)))
# Bung in the command line arguments.
sys.argv = [enaml_file] + script_argv
Expand All @@ -59,7 +108,7 @@ def main():
if requested in ns:
component = ns[requested]
descr = 'Enaml-run "%s" view' % requested
show_simple_view(component(), options.toolkit, descr)
show_simple_view(component(), toolkit, descr)
elif 'main' in ns:
ns['main']()
else:
Expand Down

0 comments on commit f864975

Please sign in to comment.