-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make example run from any directory (#377)
* Make it possible to run example scripts from anywhere. * Add a docstring for the test case * Add missing __init__.py * Import abcdefg...
- Loading branch information
1 parent
cca1569
commit 6d7d297
Showing
8 changed files
with
105 additions
and
39 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |