Example #1
0
def find_toolkit(entry_point, toolkits=None, priorities=default_priorities):
    """ Find a toolkit that works.

    If ETSConfig is set, then attempt to find a matching toolkit.  Otherwise
    try every plugin for the entry_point until one works.  The ordering of the
    plugins is supplied via the priorities function which should be suitable
    for use as a sorting key function.  If all else fails, explicitly try to
    load the "null" toolkit backend.  If that fails, give up.

    Parameters
    ----------
    entry_point : str
        The name of the entry point that holds our toolkits.
    toolkits : collection of strings
        Only consider toolkits which match the given strings, ignore other
        ones.
    priorities : callable
        A callable function that returns an priority for each plugin.

    Returns
    -------
    toolkit : Toolkit instance
        A callable object that implements the Toolkit interface.

    Raises
    ------
    TraitError
        If no working toolkit is found.
    RuntimeError
        If no ETSConfig.toolkit is set but the toolkit cannot be loaded for
        some reason.
    """
    if ETSConfig.toolkit:
        return import_toolkit(ETSConfig.toolkit, entry_point)

    entry_points = [
        plugin for plugin in importlib_metadata.entry_points()[entry_point]
        if toolkits is None or plugin.name in toolkits
    ]
    for plugin in sorted(entry_points, key=priorities):
        try:
            with ETSConfig.provisional_toolkit(plugin.name):
                toolkit = plugin.load()
                return toolkit
        except (ImportError, AttributeError, RuntimeError) as exc:
            msg = "Could not load %s plugin %r from %r"
            module_name = plugin.value.split(":")[0]
            logger.info(msg, entry_point, plugin.name, module_name)
            logger.debug(exc, exc_info=True)

    # if all else fails, try to import the null toolkit.
    with ETSConfig.provisional_toolkit("null"):
        return import_toolkit("null", entry_point)

    raise TraitError("Could not import any {} toolkit.".format(entry_point))
Example #2
0
def find_toolkit(entry_point, toolkits=None, priorities=default_priorities):
    """ Find a toolkit that works.

    If ETSConfig is set, then attempt to find a matching toolkit.  Otherwise
    try every plugin for the entry_point until one works.  The ordering of the
    plugins is supplied via the priorities function which should be suitable
    for use as a sorting key function.  If all else fails, explicitly try to
    load the "null" toolkit backend.  If that fails, give up.

    Parameters
    ----------
    entry_point : str
        The name of the entry point that holds our toolkits.
    toolkits : collection of strings
        Only consider toolkits which match the given strings, ignore other
        ones.
    priorities : callable
        A callable function that returns an priority for each plugin.

    Returns
    -------
    toolkit : Toolkit instance
        A callable object that implements the Toolkit interface.

    Raises
    ------
    TraitError
        If no working toolkit is found.
    RuntimeError
        If no ETSConfig.toolkit is set but the toolkit cannot be loaded for
        some reason.
    """
    if ETSConfig.toolkit:
        return import_toolkit(ETSConfig.toolkit, entry_point)

    entry_points = [
        plugin for plugin in pkg_resources.iter_entry_points(entry_point)
        if toolkits is None or plugin.name in toolkits
    ]
    for plugin in sorted(entry_points, key=priorities):
        try:
            with ETSConfig.provisional_toolkit(plugin.name):
                toolkit = plugin.load()
                return toolkit
        except (ImportError, AttributeError, RuntimeError) as exc:
            msg = "Could not load %s plugin %r from %r"
            logger.info(msg, entry_point, plugin.name, plugin.module_name)
            logger.debug(exc, exc_info=True)

    # if all else fails, try to import the null toolkit.
    with ETSConfig.provisional_toolkit('null'):
        return import_toolkit('null', entry_point)

    raise TraitError("Could not import any {} toolkit.".format(entry_point))
Example #3
0
def main():
    usage = 'usage: %prog [options] enaml_file'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option('-c', '--component', default='Main',
                      help='The component to view')
    parser.add_option('-t', '--toolkit', default='default',
                      choices=['default', 'wx', 'qt'],
                      help='The toolkit backend to use')
    
    options, args = parser.parse_args()

    if len(args) == 0:
        print 'No .enaml file specified'
        sys.exit()
    elif len(args) > 1:
        print 'Too many files specified'
        sys.exit()
    else:
        enaml_file = args[0]

    pg = PersistGeometry(datadir=ETSConfig.get_application_home(create=True))
    with toolkits[options.toolkit]():
        try:
            factory, module = read_component(enaml_file, requested=options.component)
        except NameError, e:
            raise SystemExit('Error: ' + str(e))

        with imports():
            from enaml_debug.debug_ui import DebugLayoutUI, get_geometry

        window = DebugLayoutUI(root=factory().central_widget, persist_geometry=pg)
        window.show()
        pg.save(get_geometry(window))
    def _load_preferences(self):
        """Load the default preferences."""
        # Save current application_home.
        app_home = ETSConfig.get_application_home()
        # Set it to where the mayavi preferences are temporarily.
        path = join(ETSConfig.get_application_data(), ID)
        ETSConfig.application_home = path
        try:
            for pkg in ('mayavi.preferences', 'tvtk.plugins.scene'):
                pref = 'preferences.ini'
                pref_file = pkg_resources.resource_stream(pkg, pref)

                preferences = self.preferences
                default = preferences.node('default/')
                default.load(pref_file)
                pref_file.close()
        finally:
            # Set back the application home.
            ETSConfig.application_home = app_home
Example #5
0
    def _load_preferences(self):
        """Load the default preferences."""
        # Save current application_home.
        app_home = ETSConfig.get_application_home()
        # Set it to where the mayavi preferences are temporarily.
        path = join(ETSConfig.get_application_data(), ID)
        ETSConfig.application_home = path
        try:
            for pkg in ('mayavi.preferences',
                        'tvtk.plugins.scene'):
                pref = 'preferences.ini'
                pref_file = pkg_resources.resource_stream(pkg, pref)

                preferences = self.preferences
                default = preferences.node('default/')
                default.load(pref_file)
                pref_file.close()
        finally:
            # Set back the application home.
            ETSConfig.application_home = app_home
    def _load_preferences(self):
        """Load the default preferences."""
#        print 'debugging mayavi.preferences.preferences_manager frozen with bbfreeze.sh start'    
#        import sys
#        print 'sys.executable', sys.executable
#        print 'sys.path', sys.path
        # Save current application_home.
        app_home = ETSConfig.get_application_home()
#        print 'app_home', app_home
        # Set it to where the mayavi preferences are temporarily.
#       print 'ID', ID
        path = join(ETSConfig.get_application_data(), ID)
#        print 'path', path
        ETSConfig.application_home = path
        try:
            for pkg in ('mayavi.preferences',
                        'tvtk.plugins.scene'):
                pref = 'preferences.ini'
#                print 'pkg', pkg
#                print 'pref', pref

#                pref_file = pkg_resources.resource_stream(pkg, pref)
                import sys
                if getattr(sys, "frozen", False):
#                    print join(sys.path[1], pkg.replace('.', '/'), pref)
                    pref_file = open(join(sys.path[1], pkg.replace('.', '/'), pref), 'rb')
                else:
                    pref_file = pkg_resources.resource_stream(pkg, pref)

            preferences = self.preferences
            default = preferences.node('default/')
            default.load(pref_file)
            pref_file.close()
        finally:
            # Set back the application home.
            ETSConfig.application_home = app_home
Example #7
0
                # Remove the option:
                del sys.argv[ opt_idx: opt_idx + 1 ]
            else:
                opt_toolkit = None

            if self._toolkit is not None:
                toolkit = self._toolkit
            elif opt_toolkit is not None:
                toolkit = opt_toolkit
            else:
                toolkit = os.environ.get( 'ETS_TOOLKIT', '' )

            return toolkit

    ETSConfig = ETSConfig()

#-------------------------------------------------------------------------------
#  Provide Python 2.3+ compatible definitions (if necessary):
#-------------------------------------------------------------------------------

try:
    from types import BooleanType
except ImportError:
    BooleanType = IntType

def _enumerate ( seq ):
    for i in xrange( len( seq) ):
        yield i, seq[i]
try:
    enumerate = enumerate
    def _id_default(self):
        """ Use the application's directory as the id """
        from traits.etsconfig.api import ETSConfig

        return ETSConfig._get_application_dirname()
Example #9
0
from traits.etsconfig.api import ETSConfig
from apptools.preferences.api import ScopedPreferences, set_default_preferences, get_default_preferences 
import os
import sys

ETSConfig.company = 'Infobiotics'

preferences = ScopedPreferences(filename=os.path.join(ETSConfig.get_application_data(create=True), 'preferences.ini'))
set_default_preferences(preferences) # allows use of Preferences, PreferencesHelper and bind_preference without explicitly passing preferences
assert preferences == get_default_preferences()

from infobiotics.mcss.mcss_preferences import PREFERENCES_PATH as MCSS_PREFERENCES_PATH
from infobiotics.mcsscmaes.mcsscmaes_preferences import PREFERENCES_PATH as MCSSCMAES_PREFERENCES_PATH
from infobiotics.pmodelchecker.pmodelchecker_preferences import PREFERENCES_PATH as PMODELCHECKER_PREFERENCES_PATH
from infobiotics.pmodelchecker.prism.prism_preferences import PREFERENCES_PATH as PRISM_PREFERENCES_PATH
from infobiotics.pmodelchecker.mc2.mc2_preferences import PREFERENCES_PATH as MC2_PREFERENCES_PATH
from infobiotics.pmodelchecker.mc2.mc2_preferences import MC2_MCSS_PREFERENCES_PATH
from infobiotics.poptimizer.poptimizer_preferences import PREFERENCES_PATH as POPTIMIZER_PREFERENCES_PATH

DEFAULT_MCSS_EXECUTABLE = 'default/'+MCSS_PREFERENCES_PATH+'.executable'
DEFAULT_MCSSCMAES_EXECUTABLE = 'default/'+MCSSCMAES_PREFERENCES_PATH+'.executable'
DEFAULT_PMODELCHECKER_EXECUTABLE = 'default/'+PMODELCHECKER_PREFERENCES_PATH+'.executable'
DEFAULT_PRISM_EXECUTABLE = 'default/'+PRISM_PREFERENCES_PATH+'.executable'
DEFAULT_MC2_EXECUTABLE = 'default/'+MC2_PREFERENCES_PATH+'.executable'
DEFAULT_MC2_MCSS_EXECUTABLE = 'default/'+MC2_MCSS_PREFERENCES_PATH+'.executable'
DEFAULT_POPTIMIZER_EXECUTABLE = 'default/'+POPTIMIZER_PREFERENCES_PATH+'.executable'

if sys.platform.startswith('win'):
    preferences.set(DEFAULT_MCSS_EXECUTABLE, 'mcss.exe'),
    preferences.set(DEFAULT_MCSSCMAES_EXECUTABLE, 'mcss-cmaes.exe'),
    preferences.set(DEFAULT_PMODELCHECKER_EXECUTABLE, 'pmodelchecker.exe'),