Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make example run from any directory #377

Merged
merged 4 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions envisage/examples/_demo.py
Original file line number Diff line number Diff line change
@@ -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 sys
import os


@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)
24 changes: 24 additions & 0 deletions envisage/examples/tests/test__demo.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 12 additions & 8 deletions examples/MOTD/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
Expand All @@ -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()
28 changes: 0 additions & 28 deletions examples/plugins/tasks/attractors/run.py

This file was deleted.

4 changes: 1 addition & 3 deletions examples/plugins/tasks/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
32 changes: 32 additions & 0 deletions examples/plugins/tasks/run_attractor.py
Original file line number Diff line number Diff line change
@@ -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)