Skip to content

Commit

Permalink
Determinate supported files using edit files and is_text_files
Browse files Browse the repository at this point in the history
Fixes spyder-ide#3631, files with suported extension could be open from project
explorer even if they aren't uft-8
  • Loading branch information
rlaverde committed Nov 2, 2016
1 parent 6fbed96 commit ea58beb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
from spyder.config.ipython import QTCONSOLE_INSTALLED
from spyder.py3compat import (getcwd, is_text_string, to_text_string,
PY3, qbytearray_to_str, configparser as cp)
from spyder.utils import encoding, programs
from spyder.utils import encoding, programs, misc
from spyder.utils import icon_manager as ima
from spyder.utils.introspection import module_completion
from spyder.utils.programs import is_module_installed
Expand Down Expand Up @@ -2435,7 +2435,7 @@ def open_file(self, fname, external=False):
"""
fname = to_text_string(fname)
ext = osp.splitext(fname)[1]
if encoding.is_text_file(fname):
if misc.is_supported_file(fname):
self.editor.load(fname)
elif self.variableexplorer is not None and ext in IMPORT_EXT:
self.variableexplorer.import_data(fname)
Expand Down
12 changes: 12 additions & 0 deletions spyder/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import stat

from spyder.py3compat import is_text_string
from spyder.config.utils import get_edit_filetypes
from spyder.utils import encoding


def __remove_pyc_pyo(fname):
Expand Down Expand Up @@ -214,6 +216,16 @@ def is_python_script(fname):
return osp.isfile(fname) and fname.endswith(('.py', '.pyw', '.ipy'))


def is_supported_file(fname):
"""
Return True if fname is a valid utf-8 file or ends with a supported
extension.
"""
supported_extensions = get_edit_filetypes()[0][1]
return encoding.is_text_file(fname) or any(fname.endswith(ext)
for ext in supported_extensions)


def abspardir(path):
"""Return absolute parent dir"""
return osp.abspath(osp.join(path, os.pardir))
Expand Down
6 changes: 3 additions & 3 deletions spyder/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
get_filter)
from spyder.py3compat import qbytearray_to_str, to_text_string, u
from spyder.utils import icon_manager as ima
from spyder.utils import (codeanalysis, encoding, sourcecode,
from spyder.utils import (codeanalysis, encoding, sourcecode, misc,
syntaxhighlighters)
from spyder.utils.qthelpers import (add_actions, create_action,
create_toolbutton, mimedata2url)
Expand Down Expand Up @@ -1871,7 +1871,7 @@ def dragEnterEvent(self, event):
# can return True but source.urls() is []
if source.hasUrls() and source.urls():
all_urls = mimedata2url(source)
text = [encoding.is_text_file(url) for url in all_urls]
text = [misc.is_supported_file(url) for url in all_urls]
if any(text):
event.acceptProposedAction()
else:
Expand All @@ -1893,7 +1893,7 @@ def dropEvent(self, event):
source = event.mimeData()
if source.hasUrls():
files = mimedata2url(source)
files = [f for f in files if encoding.is_text_file(f)]
files = [f for f in files if misc.is_supported_file(f)]
files = set(files or [])
for fname in files:
self.plugin_load.emit(fname)
Expand Down
4 changes: 2 additions & 2 deletions spyder/widgets/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def create_file_manage_actions(self, fnames):
for _fn in fnames])
only_notebooks = all([osp.splitext(_fn)[1] == '.ipynb'
for _fn in fnames])
only_valid = all([encoding.is_text_file(_fn) for _fn in fnames])
only_valid = all([misc.is_supported_file(_fn) for _fn in fnames])
run_action = create_action(self, _("Run"), icon=ima.icon('run'),
triggered=self.run)
edit_action = create_action(self, _("Edit"), icon=ima.icon('edit'),
Expand Down Expand Up @@ -462,7 +462,7 @@ def open(self, fnames=None):
if fnames is None:
fnames = self.get_selected_filenames()
for fname in fnames:
if osp.isfile(fname) and encoding.is_text_file(fname):
if osp.isfile(fname) and misc.is_supported_file(fname):
self.parent_widget.sig_open_file.emit(fname)
else:
self.open_outside_spyder([fname])
Expand Down

0 comments on commit ea58beb

Please sign in to comment.