Ejemplo n.º 1
0
def execute_notebook(nb, src, cell_separator):
    """
    Run the notebook in a python3 kernel. The ESPResSo visualizers are
    disabled to prevent the kernel from crashing and to allow running
    the notebook in a CI environment.
    """
    import nbconvert.preprocessors
    notebook_dirname = os.path.dirname(notebook_filepath)
    # disable OpenGL/Mayavi GUI
    src_no_gui = iw.mock_es_visualization(src)
    # update notebook with new code
    set_code_cells(nb, src_no_gui.split(cell_separator))
    # execute notebook
    ep = nbconvert.preprocessors.ExecutePreprocessor(timeout=20 * 60,
                                                     kernel_name='python3')
    ep.preprocess(nb, {'metadata': {'path': notebook_dirname}})
    # restore notebook with code before the GUI removal step
    set_code_cells(nb, src.split(cell_separator))
Ejemplo n.º 2
0
    def test_mock_es_visualization(self):
        statement = "import espressomd.visualization"
        expected = """
try:
    import espressomd.visualization
    if hasattr(espressomd.visualization.mayaviLive, 'deferred_ImportError') or \\
       hasattr(espressomd.visualization.openGLLive, 'deferred_ImportError'):
        raise ImportError()
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    espressomd.visualization = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "import espressomd.visualization as test"
        expected = """
try:
    import espressomd.visualization as test
    if hasattr(test.mayaviLive, 'deferred_ImportError') or \\
       hasattr(test.openGLLive, 'deferred_ImportError'):
        raise ImportError()
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    test = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization"
        expected = """
try:
    from espressomd import visualization
    if hasattr(visualization.mayaviLive, 'deferred_ImportError') or \\
       hasattr(visualization.openGLLive, 'deferred_ImportError'):
        raise ImportError()
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    visualization = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization as test"
        expected = """
try:
    from espressomd import visualization as test
    if hasattr(test.mayaviLive, 'deferred_ImportError') or \\
       hasattr(test.openGLLive, 'deferred_ImportError'):
        raise ImportError()
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    test = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization_mayavi"
        expected = """
try:
    from espressomd import visualization_mayavi
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    visualization_mayavi = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization_mayavi as test"
        expected = """
try:
    from espressomd import visualization_mayavi as test
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    test = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization_mayavi import mayaviLive"
        expected = """
try:
    from espressomd.visualization_mayavi import mayaviLive
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    mayaviLive = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization_mayavi import mayaviLive as test"
        expected = """
try:
    from espressomd.visualization_mayavi import mayaviLive as test
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    test = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization_mayavi import a as b, c"
        expected = """
try:
    from espressomd.visualization_mayavi import a as b
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    b = MagicMock()

try:
    from espressomd.visualization_mayavi import c
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    c = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization import openGLLive"
        expected = """
try:
    from espressomd.visualization import openGLLive
    if hasattr(openGLLive, 'deferred_ImportError'):
        raise openGLLive.deferred_ImportError
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    openGLLive = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization import openGLLive as test"
        expected = """
try:
    from espressomd.visualization import openGLLive as test
    if hasattr(test, 'deferred_ImportError'):
        raise test.deferred_ImportError
except ImportError:
    from unittest.mock import MagicMock
    import espressomd
    test = MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        # test exceptions
        statements_without_namespace = [
            "from espressomd.visualization import *",
            "from espressomd.visualization_opengl import *",
            "from espressomd.visualization_mayavi import *"
        ]
        for s in statements_without_namespace:
            self.assertRaises(AssertionError, iw.mock_es_visualization, s)
Ejemplo n.º 3
0
            lineno_end = mapping[visitor.matplotlib_first]
            split_code = '\n'.join(lines[lineno_end:]).lstrip('\n')
            if split_code:
                new_cell = nbformat.v4.new_code_cell(source=split_code)
                nb['cells'].insert(i + 1, new_cell)
            lines = lines[:lineno_end]
            nb['cells'][i]['source'] = '\n'.join(lines).rstrip('\n')
            break

# substitute global variables and disable OpenGL/Mayavi GUI
cell_separator = '\n##{}\n'.format(uuid.uuid4().hex)
src = cell_separator.join(get_code_cells(nb))
parameters = dict(x.split('=', 1) for x in new_values)
src = iw.substitute_variable_values(src, strings_as_is=True,
                                    keep_original=False, **parameters)
src_no_gui = iw.mock_es_visualization(src)

# update notebook with new code
set_code_cells(nb, src_no_gui.split(cell_separator))

# execute notebook
ep = ExecutePreprocessor(timeout=20 * 60, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': notebook_dirname}})

# restore notebook with code before the GUI removal step
set_code_cells(nb, src.split(cell_separator))

# write edited notebook
with open(notebook_filepath_edited, 'w', encoding='utf-8') as f:
    nbformat.write(nb, f)
Ejemplo n.º 4
0
    def test_mock_es_visualization(self):
        statement = "import espressomd.visualization"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    espressomd.visualization = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "import espressomd.visualization as test"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    test = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "import espressomd.visualization, espressomd.visualization as test"
        expected = """
try:
    import espressomd.visualization
except ImportError:
    import unittest.mock
    import espressomd
    espressomd.visualization = unittest.mock.MagicMock()
try:
    import espressomd.visualization as test
except ImportError:
    import unittest.mock
    import espressomd
    test = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    visualization = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd import visualization as test"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    test = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization import openGLLive"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    openGLLive = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization import openGLLive as test"
        expected = f"""
try:
    {statement}
except ImportError:
    import unittest.mock
    import espressomd
    test = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        statement = "from espressomd.visualization import a as b, openGLLive"
        expected = """
try:
    from espressomd.visualization import a as b
except ImportError:
    import unittest.mock
    import espressomd
    b = unittest.mock.MagicMock()
try:
    from espressomd.visualization import openGLLive
except ImportError:
    import unittest.mock
    import espressomd
    openGLLive = unittest.mock.MagicMock()
"""
        self.assertEqual(iw.mock_es_visualization(statement), expected[1:])

        # test exceptions
        self.assertRaises(ValueError, iw.mock_es_visualization,
                          "from espressomd.visualization import *")