File: test.py

package info (click to toggle)
mayavi2 4.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (151 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python

""" This is a simple test envisage3 application to demonstrate how one
can use TVTK's scene and browser plugins to create a simple application.
"""

# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
# Copyright (c) 2008, Enthought, Inc.
# License: BSD Style.

# Standard library imports.
import os.path
import logging

# Enthought library imports.
from apptools.logger.api import LogFileHandler, FORMATTER
from traits.etsconfig.api import ETSConfig
from traits.api import List
from envisage.api import Plugin
from envisage.ui.workbench.api import WorkbenchApplication
from envisage.core_plugin import CorePlugin
from envisage.ui.workbench.workbench_plugin import WorkbenchPlugin
from envisage.plugins.python_shell.python_shell_plugin import PythonShellPlugin
from tvtk.plugins.scene.scene_plugin import ScenePlugin
from tvtk.plugins.scene.ui.scene_ui_plugin import SceneUIPlugin
from tvtk.plugins.browser.browser_plugin import BrowserPlugin
from pyface.workbench.api import Perspective, PerspectiveItem


logger = logging.getLogger()

###############################################################################
# `TestPerspective` class.
###############################################################################
class TestPerspective(Perspective):
    """ An default perspective for the app. """

    # The perspective's name.
    name = 'Test'

    # Should this perspective be enabled or not?
    enabled = True

    # Should the editor area be shown in this perspective?
    show_editor_area = True

    # View IDs.
    BROWSER_VIEW = 'tvtk.plugins.browser.browser_view.BrowserView'
    SHELL_VIEW = 'envisage.plugins.python_shell.view.python_shell_view.PythonShellView'

    # The contents of the perspective.
    contents = [
        PerspectiveItem(id=BROWSER_VIEW, position='left'),
        PerspectiveItem(id=SHELL_VIEW, position='bottom')
    ]

###############################################################################
# `MyPlugin` class.
###############################################################################
class MyPlugin(Plugin):

    # Extension points we contribute to.
    PERSPECTIVES = 'envisage.ui.workbench.perspectives'

    # The plugin's unique identifier.
    id = 'tvtk_example_plugin'

    # The plugin's name (suitable for displaying to the user).
    name = 'TVTK example plugin'

    # Perspectives.
    perspectives = List(contributes_to=PERSPECTIVES)

    def _perspectives_default(self):
        """ Trait initializer. """
        return [TestPerspective]


###############################################################################
# Useful functions.
###############################################################################
def setup_logger(logger, fname, stream=True, mode=logging.ERROR):
    """Setup a log file and the logger in `ETSConfig.application_home`.

    Parameters:
    -----------

    fname -- file name the logger should use.

    stream -- Add a stream handler.

    mode -- the logging mode.

    """
    path = os.path.join(ETSConfig.application_home, fname)
    handler = LogFileHandler(path)
    logger.addHandler(handler)
    if stream:
        s = logging.StreamHandler()
        s.setFormatter(FORMATTER)
        s.setLevel(mode)
        logger.addHandler(s)
    logger.info("*"*80)
    logger.info("logfile is: '%s'", os.path.abspath(path))
    logger.info("*"*80)


def bind_object(value, app):
    """Binds the scene manager service to the Python shell."""
    if not value:
        # value is False when the GUI is stopped.
        return
    id = 'envisage.plugins.python_shell.view.python_shell_view.PythonShellView'
    py = app.workbench.active_window.get_view_by_id(id)

    id = 'tvtk.plugins.scene.i_scene_manager.ISceneManager'
    sm = app.workbench.active_window.get_service(id)
    if py is not None:
        py.bind('scene_manager', sm)


def main():
    """The main application is created and launched here."""
    # Setup the logger.

    plugins = [CorePlugin(),
               WorkbenchPlugin(),
               MyPlugin(),
               ScenePlugin(),
               SceneUIPlugin(),
               BrowserPlugin(),
               PythonShellPlugin(),
               ]
    # Create an Envisage application.
    id = 'tvtk.examples.plugins.test'
    application = WorkbenchApplication(id=id,
                                       plugins = plugins
                                       )
    # This needs to be done here since the ETSConfig.application_home is
    # not set correctly up to this point.
    setup_logger(logger, 'test.log', mode=logging.DEBUG)

    application.gui.on_trait_change(lambda value: bind_object(value, application),
                                    'started')
    # Start the application.
    application.run()


if __name__ == '__main__':
    main()