Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 133 lines (97 sloc) 4.075 kb
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#-----------------------------------------------------------------------------
# Copyright (c) 2011, Enthought, Inc.
# All rights reserved.
#------------------------------------------------------------------------------
import os
import sys


#------------------------------------------------------------------------------
# Import Helper
#------------------------------------------------------------------------------
def imports():
    """ Lazily imports and returns an enaml imports context.

"""
    from .core.import_hooks import imports
    return imports()


#------------------------------------------------------------------------------
# Toolkit Factory Functions
#------------------------------------------------------------------------------
#: The private storage for the optional default toolkit function which
#: overrides that which is computed from environment variables.
_default_toolkit_func = None


def set_default_toolkit_func(func):
    """ Set the default toolkit function to the given callable.

Parameters
----------
func : callable
A callable object which takes no arguments and returns an
instance of Toolkit.
"""
    global _default_toolkit_func
    _default_toolkit_func = func


def reset_default_toolkit_func():
    """ Reset the default toolkit func such that the default toolkit
is computed from environment variables.

"""
    global _default_toolkit_func
    _default_toolkit_func = None


def default_toolkit():
    """ Creates an returns the default toolkit object based on the user's
current ETS_TOOLKIT environment variables, or the default toolkit
function supplied via the set_default_toolkit_func function.

"""
    tk_func = _default_toolkit_func
    if tk_func is not None:
        return tk_func()

    # Accepts forms such as 'qt4' and 'qt4.agg' to allow for kiva backends
    toolkit = os.environ.get('ETS_TOOLKIT', 'qt').lower().split('.')[0]

    if toolkit == 'qt' or toolkit == 'qt4':
        return qt_toolkit()

    if toolkit == 'wx':
        return wx_toolkit()

    raise ValueError('Invalid Toolkit: %s' % toolkit)


def qt_toolkit():
    """ Creates and return a toolkit object for the Qt backend.

"""
    from .core.operators import OPERATORS
    from .core.toolkit import Toolkit
    from .components.constructors import CONSTRUCTORS
    from .layout.layout_helpers import LAYOUT_HELPERS
    from .backends.qt.constructors import QT_CONSTRUCTORS
    from .backends.qt.qt_application import QtApplication
    from .backends.qt.noncomponents.toolkit_items import TOOLKIT_ITEMS

    toolkit = Toolkit(QT_CONSTRUCTORS)
    toolkit.update(TOOLKIT_ITEMS)
    toolkit.update(CONSTRUCTORS)
    toolkit.update(OPERATORS)
    toolkit.update(LAYOUT_HELPERS)
    toolkit.app = QtApplication()

    return toolkit


def wx_toolkit():
    """ Creates and return a toolkit object for the Wx backend.

"""
    from .core.operators import OPERATORS
    from .core.toolkit import Toolkit
    from .components.constructors import CONSTRUCTORS
    from .layout.layout_helpers import LAYOUT_HELPERS
    from .backends.wx.constructors import WX_CONSTRUCTORS
    from .backends.wx.wx_application import WXApplication
    from .backends.wx.noncomponents.toolkit_items import TOOLKIT_ITEMS

    toolkit = Toolkit(WX_CONSTRUCTORS)
    toolkit.update(TOOLKIT_ITEMS)
    toolkit.update(CONSTRUCTORS)
    toolkit.update(OPERATORS)
    toolkit.update(LAYOUT_HELPERS)
    toolkit.app = WXApplication()

    return toolkit


#------------------------------------------------------------------------------
# Test Helpers
#------------------------------------------------------------------------------
def test_collector():
    """ Discover and collect tests for the Enaml Package.

.. note :: addapted from the unittest2
"""
    from unittest import TestLoader

    # import __main__ triggers code re-execution
    __main__ = sys.modules['__main__']
    setupDir = os.path.abspath(os.path.dirname(__main__.__file__))

    return TestLoader().discover(setupDir)

Something went wrong with that request. Please try again.