File: namespace_view.py

package info (click to toggle)
python-envisage 7.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,880 kB
  • sloc: python: 8,696; makefile: 76; sh: 5
file content (168 lines) | stat: -rw-r--r-- 4,776 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# (C) Copyright 2007-2023 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!
""" A view containing the contents of a Python shell namespace. """

# Enthought library imports.

from pyface.workbench.api import View
from traits.api import (
    cached_property,
    DelegatesTo,
    HasTraits,
    Instance,
    List,
    Property,
    Str,
)
from traitsui.api import Item, TableEditor, VGroup
from traitsui.api import View as TraitsView
from traitsui.table_column import ObjectColumn
from traitsui.table_filter import (
    EvalFilterTemplate,
    MenuFilterTemplate,
    RuleFilterTemplate,
    RuleTableFilter,
)

from envisage.plugins.python_shell.api import IPythonShell
from envisage.plugins.python_shell.view.python_shell_view import (
    PythonShellView,
)

# Table editor definition:
filters = [EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate]

table_editor = TableEditor(
    columns=[
        ObjectColumn(name="name"),
        ObjectColumn(name="type"),
        ObjectColumn(name="module"),
    ],
    editable=False,
    deletable=False,
    sortable=True,
    sort_model=False,
    filters=filters,
    search=RuleTableFilter(),
)


def type_to_str(obj):
    """
    Make a string out `obj`'s type robustly.
    """
    typ = type(obj)
    if typ.__name__ == "vtkobject":
        typ = obj.__class__
    if type.__module__ == "__builtin__":
        # Make things like int and str easier to read.
        return typ.__name__
    else:
        name = "%s.%s" % (typ.__module__, typ.__name__)
        return name


def module_to_str(obj):
    """
    Return the string representation of *obj*'s ``__module__`` attribute, or
    an empty string if there is no such attribute.
    """
    if hasattr(obj, "__module__"):
        return str(obj.__module__)
    else:
        return ""


class NamespaceView(View):
    """A view containing the contents of the Python shell namespace."""

    #### 'IView' interface ####################################################

    # The part's globally unique identifier.
    id = "enthought.plugins.python_shell.view.namespace_view"

    # The view's name.
    name = "Namespace"

    # The default position of the view relative to the item specified in the
    # 'relative_to' trait.
    position = "left"

    #### 'NamespaceView' interface ############################################

    # The bindings in the namespace.  This is a list of HasTraits objects with
    # 'name', 'type' and 'module' string attributes.
    bindings = Property(List, observe="namespace")

    shell_view = Instance(PythonShellView)

    namespace = DelegatesTo("shell_view")

    # The default traits UI view.
    traits_view = TraitsView(
        VGroup(
            Item(
                "bindings",
                id="table",
                editor=table_editor,
                springy=True,
                resizable=True,
            ),
            show_border=True,
            show_labels=False,
        ),
        resizable=True,
    )

    ###########################################################################
    # 'View' interface.
    ###########################################################################

    def create_control(self, parent):
        """Creates the toolkit-specific control that represents the view.

        'parent' is the toolkit-specific control that is the view's parent.

        """

        self.ui = self.edit_traits(parent=parent, kind="subpanel")

        self.shell_view = self.window.application.get_service(IPythonShell)
        # 'shell_view' is an instance of the class PythonShellView from the
        # module envisage.plugins.python_shell.view.python_shell_view.

        return self.ui.control

    ###########################################################################
    # 'NamespaceView' interface.
    ###########################################################################

    #### Properties ###########################################################

    @cached_property
    def _get_bindings(self):
        """Property getter."""

        if self.shell_view is None:
            return []

        class item(HasTraits):
            name = Str
            type = Str
            module = Str

        data = [
            item(
                name=name, type=type_to_str(value), module=module_to_str(value)
            )
            for name, value in self.shell_view.namespace.items()
        ]

        return data