Exemple #1
0
def test_import_cache_only(enaml_module):
    """Test importing a module for which we have no sources.

    """
    name, _, path = enaml_module
    with imports():
        importlib.import_module(name)

    assert name in sys.modules
    del sys.modules[name]
    os.remove(path)

    with imports():
        importlib.import_module(name)

    assert name in sys.modules
Exemple #2
0
def test_handling_importing_a_bugged_module(enaml_module):
    """Test that when importing a bugged module it does not stay in sys.modules

    """
    name, _, path = enaml_module
    with open(path, 'a') as f:
        f.write('\nraise RuntimeError()')

    with imports():
        with pytest.raises(RuntimeError):
            importlib.import_module(name)

    assert name not in sys.modules
Exemple #3
0
def test_import_when_cache_exists(enaml_module):
    """Test importing a module when the cache exists.

    """
    name, folder, _ = enaml_module
    assert name not in sys.modules
    with imports():
        importlib.import_module(name)

    assert name in sys.modules
    del sys.modules[name]

    cache_folder = os.path.join(folder, '__enamlcache__')
    assert os.path.isdir(cache_folder)
    cache_name = os.listdir(cache_folder)[0]
    cache_path = os.path.join(cache_folder, cache_name)
    cache_time = os.path.getmtime(cache_path)

    with imports():
        importlib.import_module(name)

    assert os.path.getmtime(cache_path) == cache_time
    assert name in sys.modules
Exemple #4
0
def test_import_and_cache_generation(enaml_module):
    """Test importing a module and checking that the cache was generated.

    """
    name, folder, _ = enaml_module
    with imports():
        importlib.import_module(name)

    assert name in sys.modules

    cache_folder = os.path.join(folder, '__enamlcache__')
    assert os.path.isdir(cache_folder)
    cache_name = os.listdir(cache_folder)[0]
    assert name in cache_name
    assert '.enamlc' in cache_name
Exemple #5
0
def imports(operators=None, union=True):
    """ Lazily imports and returns an enaml imports context.

    Parameters
    ----------
    operators : dict, optional
        An optional dictionary of operators to push onto the operator
        stack for the duration of the import context. If this is not
        provided, the default Enaml operators will be used. Unless a
        custom model framework is being used (i.e. not Atom), custom
        operators will typically not be needed.

    union : bool, optional
        Whether to union the operators with the operators on the top
        of the operator stack. The default is True and is typically
        the correct choice to allow overriding a subset of the default
        Enaml operators.

    Returns
    -------
    result : context manager
        A context manager which will install the Enaml import hook
        (and optional operators) for the duration of the context.

    """
    from enaml.core.import_hooks import imports
    if operators is None:
        return imports()

    from contextlib import contextmanager
    from enaml.core.operators import operator_context

    @contextmanager
    def imports_context():
        with imports():
            with operator_context(operators, union):
                yield

    return imports_context()
Exemple #6
0
def imports(operators=None, union=True):
    """ Lazily imports and returns an enaml imports context.

    Parameters
    ----------
    operators : dict, optional
        An optional dictionary of operators to push onto the operator
        stack for the duration of the import context. If this is not
        provided, the default Enaml operators will be used. Unless a
        custom model framework is being used (i.e. not Atom), custom
        operators will typically not be needed.

    union : bool, optional
        Whether to union the operators with the operators on the top
        of the operator stack. The default is True and is typically
        the correct choice to allow overriding a subset of the default
        Enaml operators.

    Returns
    -------
    result : context manager
        A context manager which will install the Enaml import hook
        (and optional operators) for the duration of the context.

    """
    from enaml.core.import_hooks import imports
    if operators is None:
        return imports()

    from contextlib import contextmanager
    from enaml.core.operators import operator_context

    @contextmanager
    def imports_context():
        with imports():
            with operator_context(operators, union):
                yield

    return imports_context()
Exemple #7
0
def test_import_and_cache_generation(enaml_module):
    """Test importing a module and checking that the cache was generated.

    """
    name, folder, _ = enaml_module
    with imports():
        importlib.import_module(name)

    assert name in sys.modules

    # Check that the module attributes are properly populated
    mod = sys.modules[name]
    assert mod.__name__ == name
    assert mod.__file__ == os.path.join(folder, name + ".enaml")
    assert os.path.join(folder, "__enamlcache__") in mod.__cached__
    assert isinstance(mod.__loader__, EnamlImporter)
    assert isinstance(mod.__spec__, ModuleSpec)

    cache_folder = os.path.join(folder, '__enamlcache__')
    assert os.path.isdir(cache_folder)
    cache_name = os.listdir(cache_folder)[0]
    assert name in cache_name
    assert '.enamlc' in cache_name
Exemple #8
0
def imports():
    """ Lazily imports and returns an enaml imports context.

    """
    from enaml.core.import_hooks import imports
    return imports()
Exemple #9
0
 def imports_context():
     with imports():
         with operator_context(operators, union):
             yield
Exemple #10
0
 def imports_context():
     with imports():
         with operator_context(operators, union):
             yield