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

« back to all changes in this revision

Viewing changes to spyderlib/widgets/explorer.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel, Ghislain Antony Vaillant, Picca Frédéric-Emmanuel
  • Date: 2015-02-26 13:31:59 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20150226133159-zvom6ax6mah3irhv
Tags: 2.3.3+dfsg-1~exp1
[Ghislain Antony Vaillant]
* New upstream release.

[Picca Frédéric-Emmanuel]
* Upstream moved to github (watch, control and copyright file updated)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
# Local imports
31
31
from spyderlib.utils.qthelpers import (get_icon, create_action, add_actions,
32
 
                                       file_uri)
 
32
                                       file_uri, get_std_icon)
33
33
from spyderlib.utils import misc, encoding, programs, vcs
34
34
from spyderlib.baseconfig import _
35
 
from spyderlib.py3compat import to_text_string, getcwd, str_lower
 
35
from spyderlib.py3compat import (to_text_string, to_binary_string, getcwd,
 
36
                                 str_lower)
36
37
 
37
 
if programs.is_module_installed('IPython'):
38
 
    import IPython.nbformat as nbformat
39
 
    import IPython.nbformat.current  # in IPython 0.13.2, current is not loaded
40
 
                                     # with nbformat.
41
 
    try:
42
 
        from IPython.nbconvert import PythonExporter as nbexporter  # >= 1.0
43
 
    except:
44
 
        nbexporter = None
45
 
else:
46
 
    nbformat = None
 
38
try:
 
39
    from IPython.nbconvert import PythonExporter as nbexporter
 
40
except:
 
41
    nbexporter = None      # analysis:ignore
47
42
 
48
43
 
49
44
def fixpath(path):
92
87
        super(DirView, self).__init__(parent)
93
88
        self.name_filters = None
94
89
        self.parent_widget = parent
95
 
        self.valid_types = None
96
90
        self.show_all = None
97
91
        self.menu = None
98
92
        self.common_actions = None
161
155
                return osp.dirname(fname)
162
156
        
163
157
    #---- Tree view widget
164
 
    def setup(self, name_filters=['*.py', '*.pyw'],
165
 
              valid_types= ('.py', '.pyw'), show_all=False):
 
158
    def setup(self, name_filters=['*.py', '*.pyw'], show_all=False):
166
159
        """Setup tree widget"""
167
160
        self.setup_view()
168
161
        
169
162
        self.set_name_filters(name_filters)
170
 
        self.valid_types = valid_types
171
163
        self.show_all = show_all
172
164
        
173
165
        # Setup context menu
238
230
                            for _fn in fnames])
239
231
        only_notebooks = all([osp.splitext(_fn)[1] == '.ipynb'
240
232
                              for _fn in fnames])
241
 
        only_valid = all([osp.splitext(_fn)[1] in self.valid_types
242
 
                          for _fn in fnames])
 
233
        only_valid = all([encoding.is_text_file(_fn) for _fn in fnames])
243
234
        run_action = create_action(self, _("Run"), icon="run_small.png",
244
235
                                   triggered=self.run)
245
236
        edit_action = create_action(self, _("Edit"), icon="edit.png",
270
261
        if all([fixpath(osp.dirname(_fn)) == basedir for _fn in fnames]):
271
262
            actions.append(move_action)
272
263
        actions += [None]
273
 
        if only_notebooks and nbformat is not None:
 
264
        if only_notebooks and nbexporter is not None:
274
265
            actions.append(ipynb_convert_action)
275
266
        
276
267
        # VCS support is quite limited for now, so we are enabling the VCS
423
414
        if fnames is None:
424
415
            fnames = self.get_selected_filenames()
425
416
        for fname in fnames:
426
 
            ext = osp.splitext(fname)[1]
427
 
            if osp.isfile(fname) and ext in self.valid_types:
 
417
            if osp.isfile(fname) and encoding.is_text_file(fname):
428
418
                self.parent_widget.sig_open_file.emit(fname)
429
419
            else:
430
420
                self.open_outside_spyder([fname])
510
500
 
511
501
    def convert_notebook(self, fname):
512
502
        """Convert an IPython notebook to a Python script in editor"""
513
 
        if nbformat is not None:
514
 
            # Use writes_py if nbconvert is not available.
515
 
            if nbexporter is None:
516
 
                script = nbformat.current.writes_py(nbformat.current.read(
517
 
                                                    open(fname, 'r'), 'ipynb'))
518
 
            else:
519
 
                script = nbexporter().from_filename(fname)[0]
520
 
            self.parent_widget.sig_new_file.emit(script)
 
503
        try: 
 
504
            script = nbexporter().from_filename(fname)[0]
 
505
        except Exception as e:
 
506
            QMessageBox.critical(self, _('Conversion error'), 
 
507
                                 _("It was not possible to convert this "
 
508
                                 "notebook. The error is:\n\n") + \
 
509
                                 to_text_string(e))
 
510
            return
 
511
        self.parent_widget.sig_new_file.emit(script)
521
512
    
522
513
    def convert(self, fnames=None):
523
514
        """Convert IPython notebooks to Python scripts in editor"""
611
602
                if is_package:
612
603
                    fname = osp.join(dirname, '__init__.py')
613
604
                    try:
614
 
                        open(fname, 'wb').write('#')
 
605
                        with open(fname, 'wb') as f:
 
606
                            f.write(to_binary_string('#'))
615
607
                        return dirname
616
608
                    except EnvironmentError as error:
617
609
                        QMessageBox.critical(self, title,
662
654
            if osp.splitext(fname)[1] in ('.py', '.pyw', '.ipy'):
663
655
                create_script(fname)
664
656
            else:
665
 
                open(fname, 'wb').write('')
 
657
                with open(fname, 'wb') as f:
 
658
                    f.write(to_binary_string(''))
666
659
        fname = self.create_new_file(basedir, title, filters, create_func)
667
660
        if fname is not None:
668
661
            self.open([fname])
985
978
    sig_new_file = Signal(str)
986
979
    
987
980
    def __init__(self, parent=None, name_filters=['*.py', '*.pyw'],
988
 
                 valid_types=('.py', '.pyw'), show_all=False,
989
 
                 show_cd_only=None, show_toolbar=True, show_icontext=True):
 
981
                 show_all=False, show_cd_only=None, show_icontext=True):
990
982
        QWidget.__init__(self, parent)
991
983
        
992
984
        self.treewidget = ExplorerTreeWidget(self, show_cd_only=show_cd_only)
993
 
        self.treewidget.setup(name_filters=name_filters,
994
 
                              valid_types=valid_types, show_all=show_all)
 
985
        self.treewidget.setup(name_filters=name_filters, show_all=show_all)
995
986
        self.treewidget.chdir(getcwd())
996
987
        
997
 
        toolbar_action = create_action(self, _("Show toolbar"),
998
 
                                       toggled=self.toggle_toolbar)
999
988
        icontext_action = create_action(self, _("Show icons and text"),
1000
989
                                        toggled=self.toggle_icontext)
1001
 
        self.treewidget.common_actions += [None,
1002
 
                                           toolbar_action, icontext_action]
 
990
        self.treewidget.common_actions += [None, icontext_action]
1003
991
        
1004
992
        # Setup toolbar
1005
993
        self.toolbar = QToolBar(self)
1006
994
        self.toolbar.setIconSize(QSize(16, 16))
1007
995
        
1008
996
        self.previous_action = create_action(self, text=_("Previous"),
1009
 
                            icon=get_icon('previous.png'),
 
997
                            icon=get_std_icon("ArrowBack"),
1010
998
                            triggered=self.treewidget.go_to_previous_directory)
1011
999
        self.toolbar.addAction(self.previous_action)
1012
1000
        self.previous_action.setEnabled(False)
1014
1002
                     self.previous_action.setEnabled)
1015
1003
        
1016
1004
        self.next_action = create_action(self, text=_("Next"),
1017
 
                            icon=get_icon('next.png'),
 
1005
                            icon=get_std_icon("ArrowForward"),
1018
1006
                            triggered=self.treewidget.go_to_next_directory)
1019
1007
        self.toolbar.addAction(self.next_action)
1020
1008
        self.next_action.setEnabled(False)
1022
1010
                     self.next_action.setEnabled)
1023
1011
        
1024
1012
        parent_action = create_action(self, text=_("Parent"),
1025
 
                            icon=get_icon('up.png'),
 
1013
                            icon=get_std_icon("ArrowUp"),
1026
1014
                            triggered=self.treewidget.go_to_parent_directory)
1027
1015
        self.toolbar.addAction(parent_action)
 
1016
        self.toolbar.addSeparator()
1028
1017
 
1029
1018
        options_action = create_action(self, text='', tip=_("Options"),
1030
1019
                                       icon=get_icon('tooloptions.png'))
1035
1024
        add_actions(menu, self.treewidget.common_actions)
1036
1025
        options_action.setMenu(menu)
1037
1026
            
1038
 
        toolbar_action.setChecked(show_toolbar)
1039
 
        self.toggle_toolbar(show_toolbar)   
1040
1027
        icontext_action.setChecked(show_icontext)
1041
1028
        self.toggle_icontext(show_icontext)     
1042
1029
        
1044
1031
        vlayout.addWidget(self.toolbar)
1045
1032
        vlayout.addWidget(self.treewidget)
1046
1033
        self.setLayout(vlayout)
1047
 
        
1048
 
    def toggle_toolbar(self, state):
1049
 
        """Toggle toolbar"""
1050
 
        self.sig_option_changed.emit('show_toolbar', state)
1051
 
        self.toolbar.setVisible(state)
1052
 
            
 
1034
 
1053
1035
    def toggle_icontext(self, state):
1054
1036
        """Toggle icon text"""
1055
1037
        self.sig_option_changed.emit('show_icontext', state)
1056
1038
        for action in self.toolbar.actions():
1057
 
            widget = self.toolbar.widgetForAction(action)
1058
 
            if state:
1059
 
                widget.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
1060
 
            else:
1061
 
                widget.setToolButtonStyle(Qt.ToolButtonIconOnly)
 
1039
            if not action.isSeparator():
 
1040
                widget = self.toolbar.widgetForAction(action)
 
1041
                if state:
 
1042
                    widget.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
 
1043
                else:
 
1044
                    widget.setToolButtonStyle(Qt.ToolButtonIconOnly)
1062
1045
 
1063
1046
 
1064
1047
class FileExplorerTest(QWidget):