~ubuntu-branches/debian/experimental/spyder/experimental

« back to all changes in this revision

Viewing changes to spyderlib/plugins/variableexplorer.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2013-01-20 12:19:54 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20130120121954-1jt1xa924bshhvh0
Tags: 2.2.0~beta1+dfsg-2
fix typo ipython-qtconsol -> ipython-qtconsole

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Copyright © 2009-2010 Pierre Raybaut
4
 
# Licensed under the terms of the MIT License
5
 
# (see spyderlib/__init__.py for details)
6
 
 
7
 
"""Namespace Browser Plugin"""
8
 
 
9
 
from spyderlib.qt.QtGui import QStackedWidget, QGroupBox, QVBoxLayout
10
 
from spyderlib.qt.QtCore import Signal
11
 
 
12
 
# Local imports
13
 
from spyderlib.baseconfig import _
14
 
from spyderlib.config import CONF, get_icon
15
 
from spyderlib.utils import programs
16
 
from spyderlib.plugins import SpyderPluginMixin, PluginConfigPage
17
 
from spyderlib.widgets.externalshell.monitor import REMOTE_SETTINGS
18
 
from spyderlib.widgets.externalshell.namespacebrowser import NamespaceBrowser
19
 
 
20
 
 
21
 
class VariableExplorerConfigPage(PluginConfigPage):
22
 
    def setup_page(self):
23
 
        ar_group = QGroupBox(_("Autorefresh"))
24
 
        ar_box = self.create_checkbox(_("Enable autorefresh"),
25
 
                                      'autorefresh')
26
 
        ar_spin = self.create_spinbox(_("Refresh interval: "),
27
 
                                      _(" ms"), 'autorefresh/timeout',
28
 
                                      min_=100, max_=1000000, step=100)
29
 
        
30
 
        filter_group = QGroupBox(_("Filter"))
31
 
        filter_data = [
32
 
            ('exclude_private', _("Exclude private references")),
33
 
            ('exclude_capitalized', _("Exclude capitalized references")),
34
 
            ('exclude_uppercase', _("Exclude all-uppercase references")),
35
 
            ('exclude_unsupported', _("Exclude unsupported data types")),
36
 
                ]
37
 
        filter_boxes = [self.create_checkbox(text, option)
38
 
                        for option, text in filter_data]
39
 
 
40
 
        display_group = QGroupBox(_("Display"))
41
 
        display_data = [
42
 
                        ('truncate', _("Truncate values"), ''),
43
 
                        ('inplace', _("Always edit in-place"), ''),
44
 
                        ('collvalue', _("Show collection contents"), ''),
45
 
                        ]
46
 
        if programs.is_module_installed('numpy'):
47
 
            display_data.append(('minmax', _("Show arrays min/max"), ''))
48
 
        display_data.append(
49
 
            ('remote_editing', _("Edit data in the remote process"),
50
 
             _("Editors are opened in the remote process for NumPy "
51
 
                     "arrays, PIL images, lists, tuples and dictionaries.\n"
52
 
                     "This avoids transfering large amount of data between "
53
 
                     "the remote process and Spyder (through the socket)."))
54
 
                            )
55
 
        display_boxes = [self.create_checkbox(text, option, tip=tip)
56
 
                         for option, text, tip in display_data]
57
 
        
58
 
        ar_layout = QVBoxLayout()
59
 
        ar_layout.addWidget(ar_box)
60
 
        ar_layout.addWidget(ar_spin)
61
 
        ar_group.setLayout(ar_layout)
62
 
        
63
 
        filter_layout = QVBoxLayout()
64
 
        for box in filter_boxes:
65
 
            filter_layout.addWidget(box)
66
 
        filter_group.setLayout(filter_layout)
67
 
 
68
 
        display_layout = QVBoxLayout()
69
 
        for box in display_boxes:
70
 
            display_layout.addWidget(box)
71
 
        display_group.setLayout(display_layout)
72
 
 
73
 
        vlayout = QVBoxLayout()
74
 
        vlayout.addWidget(ar_group)
75
 
        vlayout.addWidget(filter_group)
76
 
        vlayout.addWidget(display_group)
77
 
        vlayout.addStretch(1)
78
 
        self.setLayout(vlayout)
79
 
 
80
 
 
81
 
class VariableExplorer(QStackedWidget, SpyderPluginMixin):
82
 
    """
83
 
    Variable Explorer Plugin
84
 
    """
85
 
    CONF_SECTION = 'variable_explorer'
86
 
    CONFIGWIDGET_CLASS = VariableExplorerConfigPage
87
 
    sig_option_changed = Signal(str, object)
88
 
    def __init__(self, parent):
89
 
        QStackedWidget.__init__(self, parent)
90
 
        SpyderPluginMixin.__init__(self, parent)
91
 
        self.shellwidgets = {}
92
 
 
93
 
        # Initialize plugin
94
 
        self.initialize_plugin()
95
 
 
96
 
    @staticmethod
97
 
    def get_settings():
98
 
        """
99
 
        Return Variable Explorer settings dictionary
100
 
        (i.e. namespace browser settings according to Spyder's configuration file)
101
 
        """
102
 
        settings = {}
103
 
#        CONF.load_from_ini() # necessary only when called from another process
104
 
        for name in REMOTE_SETTINGS:
105
 
            settings[name] = CONF.get(VariableExplorer.CONF_SECTION, name)
106
 
        return settings
107
 
        
108
 
    #------ Public API ---------------------------------------------------------
109
 
    def add_shellwidget(self, shellwidget):
110
 
        shellwidget_id = id(shellwidget)
111
 
        # Add shell only once: this method may be called two times in a row 
112
 
        # by the External console plugin (dev. convenience)
113
 
        from spyderlib.widgets.externalshell import systemshell
114
 
        if isinstance(shellwidget, systemshell.ExternalSystemShell):
115
 
            return
116
 
        if shellwidget_id not in self.shellwidgets:
117
 
            nsb = NamespaceBrowser(self)
118
 
            nsb.set_shellwidget(shellwidget)
119
 
            nsb.setup(**VariableExplorer.get_settings())
120
 
            nsb.sig_option_changed.connect(self.sig_option_changed.emit)
121
 
            self.addWidget(nsb)
122
 
            self.shellwidgets[shellwidget_id] = nsb
123
 
            self.set_shellwidget_from_id(shellwidget_id)
124
 
            return nsb
125
 
        
126
 
    def remove_shellwidget(self, shellwidget_id):
127
 
        # If shellwidget_id is not in self.shellwidgets, it simply means
128
 
        # that shell was not a Python/IPython-based console (it was a terminal)
129
 
        if shellwidget_id in self.shellwidgets:
130
 
            nsb = self.shellwidgets.pop(shellwidget_id)
131
 
            self.removeWidget(nsb)
132
 
            nsb.close()
133
 
    
134
 
    def set_shellwidget_from_id(self, shellwidget_id):
135
 
        if shellwidget_id in self.shellwidgets:
136
 
            nsb = self.shellwidgets[shellwidget_id]
137
 
            self.setCurrentWidget(nsb)
138
 
            if self.isvisible:
139
 
                nsb.visibility_changed(True)
140
 
            
141
 
    def import_data(self, fname):
142
 
        """Import data in current namespace"""
143
 
        if self.count():
144
 
            nsb = self.currentWidget()
145
 
            nsb.refresh_table()
146
 
            nsb.import_data(fname)
147
 
            if self.dockwidget and not self.ismaximized:
148
 
                self.dockwidget.setVisible(True)
149
 
                self.dockwidget.raise_()
150
 
 
151
 
    #------ SpyderPluginMixin API ---------------------------------------------
152
 
    def visibility_changed(self, enable):
153
 
        """DockWidget visibility has changed"""
154
 
        SpyderPluginMixin.visibility_changed(self, enable)
155
 
        for nsb in self.shellwidgets.values():
156
 
            nsb.visibility_changed(enable and nsb is self.currentWidget())
157
 
    
158
 
    #------ SpyderPluginWidget API ---------------------------------------------
159
 
    def get_plugin_title(self):
160
 
        """Return widget title"""
161
 
        return _('Variable explorer')
162
 
 
163
 
    def get_plugin_icon(self):
164
 
        """Return plugin icon"""
165
 
        return get_icon('dictedit.png')
166
 
    
167
 
    def get_focus_widget(self):
168
 
        """
169
 
        Return the widget to give focus to when
170
 
        this plugin's dockwidget is raised on top-level
171
 
        """
172
 
        return self.currentWidget()
173
 
        
174
 
    def closing_plugin(self, cancelable=False):
175
 
        """Perform actions before parent main window is closed"""
176
 
        return True
177
 
        
178
 
    def refresh_plugin(self):
179
 
        """Refresh widget"""
180
 
        pass
181
 
    
182
 
    def get_plugin_actions(self):
183
 
        """Return a list of actions related to plugin"""
184
 
        return []
185
 
    
186
 
    def register_plugin(self):
187
 
        """Register plugin in Spyder's main window"""
188
 
        self.main.extconsole.set_variableexplorer(self)
189
 
        self.main.add_dockwidget(self)
190
 
        
191
 
    def apply_plugin_settings(self, options):
192
 
        """Apply configuration file's plugin settings"""
193
 
        for nsb in self.shellwidgets.values():
194
 
            nsb.setup(**VariableExplorer.get_settings())
195
 
        ar_timeout = self.get_option('autorefresh/timeout')
196
 
        for shellwidget in self.main.extconsole.shellwidgets:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright © 2009-2010 Pierre Raybaut
 
4
# Licensed under the terms of the MIT License
 
5
# (see spyderlib/__init__.py for details)
 
6
 
 
7
"""Namespace Browser Plugin"""
 
8
 
 
9
from spyderlib.qt.QtGui import QStackedWidget, QGroupBox, QVBoxLayout
 
10
from spyderlib.qt.QtCore import Signal
 
11
 
 
12
# Local imports
 
13
from spyderlib.baseconfig import _
 
14
from spyderlib.config import CONF
 
15
from spyderlib.guiconfig import get_icon
 
16
from spyderlib.utils import programs
 
17
from spyderlib.plugins import SpyderPluginMixin, PluginConfigPage
 
18
from spyderlib.widgets.externalshell.monitor import REMOTE_SETTINGS
 
19
from spyderlib.widgets.externalshell.namespacebrowser import NamespaceBrowser
 
20
 
 
21
 
 
22
class VariableExplorerConfigPage(PluginConfigPage):
 
23
    def setup_page(self):
 
24
        ar_group = QGroupBox(_("Autorefresh"))
 
25
        ar_box = self.create_checkbox(_("Enable autorefresh"),
 
26
                                      'autorefresh')
 
27
        ar_spin = self.create_spinbox(_("Refresh interval: "),
 
28
                                      _(" ms"), 'autorefresh/timeout',
 
29
                                      min_=100, max_=1000000, step=100)
 
30
        
 
31
        filter_group = QGroupBox(_("Filter"))
 
32
        filter_data = [
 
33
            ('exclude_private', _("Exclude private references")),
 
34
            ('exclude_capitalized', _("Exclude capitalized references")),
 
35
            ('exclude_uppercase', _("Exclude all-uppercase references")),
 
36
            ('exclude_unsupported', _("Exclude unsupported data types")),
 
37
                ]
 
38
        filter_boxes = [self.create_checkbox(text, option)
 
39
                        for option, text in filter_data]
 
40
 
 
41
        display_group = QGroupBox(_("Display"))
 
42
        display_data = [
 
43
                        ('truncate', _("Truncate values"), ''),
 
44
                        ('inplace', _("Always edit in-place"), ''),
 
45
                        ('collvalue', _("Show collection contents"), ''),
 
46
                        ]
 
47
        if programs.is_module_installed('numpy'):
 
48
            display_data.append(('minmax', _("Show arrays min/max"), ''))
 
49
        display_data.append(
 
50
            ('remote_editing', _("Edit data in the remote process"),
 
51
             _("Editors are opened in the remote process for NumPy "
 
52
                     "arrays, PIL images, lists, tuples and dictionaries.\n"
 
53
                     "This avoids transfering large amount of data between "
 
54
                     "the remote process and Spyder (through the socket)."))
 
55
                            )
 
56
        display_boxes = [self.create_checkbox(text, option, tip=tip)
 
57
                         for option, text, tip in display_data]
 
58
        
 
59
        ar_layout = QVBoxLayout()
 
60
        ar_layout.addWidget(ar_box)
 
61
        ar_layout.addWidget(ar_spin)
 
62
        ar_group.setLayout(ar_layout)
 
63
        
 
64
        filter_layout = QVBoxLayout()
 
65
        for box in filter_boxes:
 
66
            filter_layout.addWidget(box)
 
67
        filter_group.setLayout(filter_layout)
 
68
 
 
69
        display_layout = QVBoxLayout()
 
70
        for box in display_boxes:
 
71
            display_layout.addWidget(box)
 
72
        display_group.setLayout(display_layout)
 
73
 
 
74
        vlayout = QVBoxLayout()
 
75
        vlayout.addWidget(ar_group)
 
76
        vlayout.addWidget(filter_group)
 
77
        vlayout.addWidget(display_group)
 
78
        vlayout.addStretch(1)
 
79
        self.setLayout(vlayout)
 
80
 
 
81
 
 
82
class VariableExplorer(QStackedWidget, SpyderPluginMixin):
 
83
    """
 
84
    Variable Explorer Plugin
 
85
    """
 
86
    CONF_SECTION = 'variable_explorer'
 
87
    CONFIGWIDGET_CLASS = VariableExplorerConfigPage
 
88
    sig_option_changed = Signal(str, object)
 
89
    def __init__(self, parent):
 
90
        QStackedWidget.__init__(self, parent)
 
91
        SpyderPluginMixin.__init__(self, parent)
 
92
        self.shellwidgets = {}
 
93
 
 
94
        # Initialize plugin
 
95
        self.initialize_plugin()
 
96
 
 
97
    @staticmethod
 
98
    def get_settings():
 
99
        """
 
100
        Return Variable Explorer settings dictionary
 
101
        (i.e. namespace browser settings according to Spyder's configuration file)
 
102
        """
 
103
        settings = {}
 
104
#        CONF.load_from_ini() # necessary only when called from another process
 
105
        for name in REMOTE_SETTINGS:
 
106
            settings[name] = CONF.get(VariableExplorer.CONF_SECTION, name)
 
107
        return settings
 
108
        
 
109
    #------ Public API ---------------------------------------------------------
 
110
    def add_shellwidget(self, shellwidget):
 
111
        shellwidget_id = id(shellwidget)
 
112
        # Add shell only once: this method may be called two times in a row 
 
113
        # by the External console plugin (dev. convenience)
 
114
        from spyderlib.widgets.externalshell import systemshell
 
115
        if isinstance(shellwidget, systemshell.ExternalSystemShell):
 
116
            return
 
117
        if shellwidget_id not in self.shellwidgets:
 
118
            nsb = NamespaceBrowser(self)
 
119
            nsb.set_shellwidget(shellwidget)
 
120
            nsb.setup(**VariableExplorer.get_settings())
 
121
            nsb.sig_option_changed.connect(self.sig_option_changed.emit)
 
122
            self.addWidget(nsb)
 
123
            self.shellwidgets[shellwidget_id] = nsb
 
124
            self.set_shellwidget_from_id(shellwidget_id)
 
125
            return nsb
 
126
        
 
127
    def remove_shellwidget(self, shellwidget_id):
 
128
        # If shellwidget_id is not in self.shellwidgets, it simply means
 
129
        # that shell was not a Python-based console (it was a terminal)
 
130
        if shellwidget_id in self.shellwidgets:
 
131
            nsb = self.shellwidgets.pop(shellwidget_id)
 
132
            self.removeWidget(nsb)
 
133
            nsb.close()
 
134
    
 
135
    def set_shellwidget_from_id(self, shellwidget_id):
 
136
        if shellwidget_id in self.shellwidgets:
 
137
            nsb = self.shellwidgets[shellwidget_id]
 
138
            self.setCurrentWidget(nsb)
 
139
            if self.isvisible:
 
140
                nsb.visibility_changed(True)
 
141
            
 
142
    def import_data(self, fname):
 
143
        """Import data in current namespace"""
 
144
        if self.count():
 
145
            nsb = self.currentWidget()
 
146
            nsb.refresh_table()
 
147
            nsb.import_data(fname)
 
148
            if self.dockwidget and not self.ismaximized:
 
149
                self.dockwidget.setVisible(True)
 
150
                self.dockwidget.raise_()
 
151
 
 
152
    #------ SpyderPluginMixin API ---------------------------------------------
 
153
    def visibility_changed(self, enable):
 
154
        """DockWidget visibility has changed"""
 
155
        SpyderPluginMixin.visibility_changed(self, enable)
 
156
        for nsb in self.shellwidgets.values():
 
157
            nsb.visibility_changed(enable and nsb is self.currentWidget())
 
158
    
 
159
    #------ SpyderPluginWidget API ---------------------------------------------
 
160
    def get_plugin_title(self):
 
161
        """Return widget title"""
 
162
        return _('Variable explorer')
 
163
 
 
164
    def get_plugin_icon(self):
 
165
        """Return plugin icon"""
 
166
        return get_icon('dictedit.png')
 
167
    
 
168
    def get_focus_widget(self):
 
169
        """
 
170
        Return the widget to give focus to when
 
171
        this plugin's dockwidget is raised on top-level
 
172
        """
 
173
        return self.currentWidget()
 
174
        
 
175
    def closing_plugin(self, cancelable=False):
 
176
        """Perform actions before parent main window is closed"""
 
177
        return True
 
178
        
 
179
    def refresh_plugin(self):
 
180
        """Refresh widget"""
 
181
        pass
 
182
    
 
183
    def get_plugin_actions(self):
 
184
        """Return a list of actions related to plugin"""
 
185
        return []
 
186
    
 
187
    def register_plugin(self):
 
188
        """Register plugin in Spyder's main window"""
 
189
        self.main.extconsole.set_variableexplorer(self)
 
190
        self.main.add_dockwidget(self)
 
191
        
 
192
    def apply_plugin_settings(self, options):
 
193
        """Apply configuration file's plugin settings"""
 
194
        for nsb in self.shellwidgets.values():
 
195
            nsb.setup(**VariableExplorer.get_settings())
 
196
        ar_timeout = self.get_option('autorefresh/timeout')
 
197
        for shellwidget in self.main.extconsole.shellwidgets:
197
198
            shellwidget.set_autorefresh_timeout(ar_timeout)
 
 
b'\\ No newline at end of file'