31
31
from spyderlib.utils.qthelpers import (get_icon, create_action, add_actions,
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,
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
42
from IPython.nbconvert import PythonExporter as nbexporter # >= 1.0
39
from IPython.nbconvert import PythonExporter as nbexporter
41
nbexporter = None # analysis:ignore
161
155
return osp.dirname(fname)
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()
169
162
self.set_name_filters(name_filters)
170
self.valid_types = valid_types
171
163
self.show_all = show_all
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
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)
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)
430
420
self.open_outside_spyder([fname])
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'))
519
script = nbexporter().from_filename(fname)[0]
520
self.parent_widget.sig_new_file.emit(script)
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") + \
511
self.parent_widget.sig_new_file.emit(script)
522
513
def convert(self, fnames=None):
523
514
"""Convert IPython notebooks to Python scripts in editor"""
612
603
fname = osp.join(dirname, '__init__.py')
614
open(fname, 'wb').write('#')
605
with open(fname, 'wb') as f:
606
f.write(to_binary_string('#'))
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)
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)
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)
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())
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]
1005
993
self.toolbar = QToolBar(self)
1006
994
self.toolbar.setIconSize(QSize(16, 16))
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)
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)
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()
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)
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)
1044
1031
vlayout.addWidget(self.toolbar)
1045
1032
vlayout.addWidget(self.treewidget)
1046
1033
self.setLayout(vlayout)
1048
def toggle_toolbar(self, state):
1049
"""Toggle toolbar"""
1050
self.sig_option_changed.emit('show_toolbar', state)
1051
self.toolbar.setVisible(state)
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)
1059
widget.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
1061
widget.setToolButtonStyle(Qt.ToolButtonIconOnly)
1039
if not action.isSeparator():
1040
widget = self.toolbar.widgetForAction(action)
1042
widget.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
1044
widget.setToolButtonStyle(Qt.ToolButtonIconOnly)
1064
1047
class FileExplorerTest(QWidget):