diff --git a/envisage/examples/__init__.py b/envisage/examples/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/envisage/examples/_demo.py b/envisage/examples/_demo.py new file mode 100644 index 000000000..3259a618c --- /dev/null +++ b/envisage/examples/_demo.py @@ -0,0 +1,36 @@ +# (C) Copyright 2007-2020 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! +""" Utilities for supporting Envisage's demo examples. +""" + +import contextlib +import os +import sys + + +@contextlib.contextmanager +def demo_path(path): + """ Context manager to temporarily insert the directory containing + the demo script to sys.path such that demo examples can be run using + local packages. + + This function should only be used by Envisage example files. + + Parameters + ---------- + path : Path-like + Path to the demo script to be run. + """ + path = os.path.dirname(os.fspath(path)) + try: + sys.path.insert(0, path) + yield + finally: + sys.path.remove(path) diff --git a/envisage/examples/tests/__init__.py b/envisage/examples/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/envisage/examples/tests/test__demo.py b/envisage/examples/tests/test__demo.py new file mode 100644 index 000000000..64319d5b6 --- /dev/null +++ b/envisage/examples/tests/test__demo.py @@ -0,0 +1,24 @@ +# (C) Copyright 2007-2020 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! +import os +import sys +import unittest + +from envisage.examples._demo import demo_path + + +class TestDemoUtilities(unittest.TestCase): + """ Test utility functions in the _demo module.""" + + def test_sys_path_inserted(self): + path = os.path.join("dirname", "file.py") + with demo_path(path): + self.assertIn("dirname", sys.path) + self.assertNotIn("dirname", sys.path) diff --git a/examples/MOTD/run.py b/examples/MOTD/run.py index 0c78b6762..5b89ec19c 100644 --- a/examples/MOTD/run.py +++ b/examples/MOTD/run.py @@ -7,12 +7,6 @@ # Enthought library imports. from envisage.api import Application, CorePlugin -# Example plugins. -from acme.motd.motd_plugin import MOTDPlugin -from acme.motd.software_quotes.software_quotes_plugin import ( - SoftwareQuotesPlugin, -) - # Do whatever you want to do with log messages! Here we create a log file. logger = logging.getLogger() @@ -22,7 +16,12 @@ def main(): """ Run the application. """ - + # Import here so that this script can be run from anywhere without + # having to install the packages. + from acme.motd.motd_plugin import MOTDPlugin + from acme.motd.software_quotes.software_quotes_plugin import ( + SoftwareQuotesPlugin, + ) # Create an application containing the appropriate plugins. application = Application( id="acme.motd", @@ -34,4 +33,9 @@ def main(): if __name__ == "__main__": - main() + # This context manager is added so that one can run this example from any + # directory without necessarily having installed the examples as packages. + from envisage.examples._demo import demo_path + + with demo_path(__file__): + main() diff --git a/examples/plugins/tasks/attractors/run.py b/examples/plugins/tasks/attractors/run.py deleted file mode 100644 index 948fa6cd0..000000000 --- a/examples/plugins/tasks/attractors/run.py +++ /dev/null @@ -1,28 +0,0 @@ -# Standard library imports. -import logging - -# Plugin imports. -from envisage.api import CorePlugin -from envisage.ui.tasks.api import TasksPlugin -from attractors.attractors_plugin import AttractorsPlugin - -# Local imports. -from attractors.attractors_application import AttractorsApplication - - -def main(argv): - """ Run the application. - """ - logging.basicConfig(level=logging.WARNING) - - plugins = [CorePlugin(), TasksPlugin(), AttractorsPlugin()] - app = AttractorsApplication(plugins=plugins) - app.run() - - logging.shutdown() - - -if __name__ == "__main__": - import sys - - main(sys.argv) diff --git a/examples/plugins/tasks/index.rst b/examples/plugins/tasks/index.rst index 1929a7178..d1b1949f1 100644 --- a/examples/plugins/tasks/index.rst +++ b/examples/plugins/tasks/index.rst @@ -2,9 +2,7 @@ Welcome to the attractors example. To run the application:: - python -m attractors.run - -from this directory. + python run_attractor.py Note that this example application depends on the following additional packages:: diff --git a/examples/plugins/tasks/run_attractor.py b/examples/plugins/tasks/run_attractor.py new file mode 100644 index 000000000..6e15fe130 --- /dev/null +++ b/examples/plugins/tasks/run_attractor.py @@ -0,0 +1,32 @@ +# Standard library imports. +import logging + +# Plugin imports. +from envisage.api import CorePlugin +from envisage.ui.tasks.api import TasksPlugin + + +def main(argv): + """ Run the application. + """ + # Import here so that this script can be run from anywhere without + # having to install the packages. + from attractors.attractors_plugin import AttractorsPlugin + from attractors.attractors_application import AttractorsApplication + logging.basicConfig(level=logging.WARNING) + + plugins = [CorePlugin(), TasksPlugin(), AttractorsPlugin()] + app = AttractorsApplication(plugins=plugins) + app.run() + + logging.shutdown() + + +if __name__ == "__main__": + import sys + # This context manager is added so that one can run this example from any + # directory without necessarily having installed the examples as packages. + from envisage.examples._demo import demo_path + + with demo_path(__file__): + main(sys.argv)