Debian Codegraph @

envisage/plugins/python_shell/view/namespace_view.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ A view containing the contents of a Python shell namespace. """
11
12 # Enthought library imports.
13
14 from pyface.workbench.api import View
15 from traits.api import (
16 cached_property,
17 DelegatesTo,
18 HasTraits,
19 Instance,
20 List,
21 Property,
22 Str,
23 )
24 from traitsui.api import Item, TableEditor, VGroup
25 from traitsui.api import View as TraitsView
26 from traitsui.table_column import ObjectColumn
27 from traitsui.table_filter import (
28 EvalFilterTemplate,
29 MenuFilterTemplate,
30 RuleFilterTemplate,
31 RuleTableFilter,
32 )
33
34 from envisage.plugins.python_shell.api import IPythonShell
35 from envisage.plugins.python_shell.view.python_shell_view import (
36 PythonShellView,
37 )
38
39 # Table editor definition:
40 filters = [EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate]
41
42 table_editor = TableEditor(
43 columns=[
44 ObjectColumn(name="name"),
45 ObjectColumn(name="type"),
46 ObjectColumn(name="module"),
47 ],
48 editable=False,
49 deletable=False,
50 sortable=True,
51 sort_model=False,
52 filters=filters,
53 search=RuleTableFilter(),
54 )
55
56
57 def type_to_str(obj):
58 """
59 Make a string out `obj`'s type robustly.
60 """
61 typ = type(obj)
62 if typ.__name__ == "vtkobject":
63 typ = obj.__class__
64 if type.__module__ == "__builtin__":
65 # Make things like int and str easier to read.
66 return typ.__name__
67 else:
68 name = "%s.%s" % (typ.__module__, typ.__name__)
69 return name
70
71
72 def module_to_str(obj):
73 """
74 Return the string representation of *obj*'s ``__module__`` attribute, or
75 an empty string if there is no such attribute.
76 """
77 if hasattr(obj, "__module__"):
78 return str(obj.__module__)
79 else:
80 return ""
81
82
83 class NamespaceView(View):
84 """A view containing the contents of the Python shell namespace."""
85
86 #### 'IView' interface ####################################################
87
88 # The part's globally unique identifier.
89 id = "enthought.plugins.python_shell.view.namespace_view"
90
91 # The view's name.
92 name = "Namespace"
93
94 # The default position of the view relative to the item specified in the
95 # 'relative_to' trait.
96 position = "left"
97
98 #### 'NamespaceView' interface ############################################
99
100 # The bindings in the namespace. This is a list of HasTraits objects with
101 # 'name', 'type' and 'module' string attributes.
102 bindings = Property(List, observe="namespace")
103
104 shell_view = Instance(PythonShellView)
105
106 namespace = DelegatesTo("shell_view")
107
108 # The default traits UI view.
109 traits_view = TraitsView(
110 VGroup(
111 Item(
112 "bindings",
113 id="table",
114 editor=table_editor,
115 springy=True,
116 resizable=True,
117 ),
118 show_border=True,
119 show_labels=False,
120 ),
121 resizable=True,
122 )
123
124 ###########################################################################
125 # 'View' interface.
126 ###########################################################################
127
128 def create_control(self, parent):
129 """Creates the toolkit-specific control that represents the view.
130
131 'parent' is the toolkit-specific control that is the view's parent.
132
133 """
134
135 self.ui = self.edit_traits(parent=parent, kind="subpanel")
136
137 self.shell_view = self.window.application.get_service(IPythonShell)
138 # 'shell_view' is an instance of the class PythonShellView from the
139 # module envisage.plugins.python_shell.view.python_shell_view.
140
141 return self.ui.control
142
143 ###########################################################################
144 # 'NamespaceView' interface.
145 ###########################################################################
146
147 #### Properties ###########################################################
148
149 @cached_property
150 def _get_bindings(self):
151 """Property getter."""
152
153 if self.shell_view is None:
154 return []
155
156 class item(HasTraits):
157 name = Str
158 type = Str
159 module = Str
160
161 data = [
162 item(
163 name=name, type=type_to_str(value), module=module_to_str(value)
164 )
165 for name, value in self.shell_view.namespace.items()
166 ]
167
168 return data