Skip to content

Commit

Permalink
Merge pull request #2973 from ccordoba12/change-all-filters
Browse files Browse the repository at this point in the history
PR: Show all supported text files when opening files with "File > Open"
  • Loading branch information
ccordoba12 committed Feb 10, 2016
2 parents 59d848c + e977a71 commit 00e40a5
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 129 deletions.
8 changes: 3 additions & 5 deletions spyderlib/app/spyder.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
get_module_source_path, STDERR, DEBUG,
debug_print, TEST, SUBFOLDER, MAC_APP_NAME,
running_in_mac_app, get_module_path)
from spyderlib.config.main import (CONF, EDIT_EXT, IMPORT_EXT, OPEN_FILES_PORT,
is_gtk_desktop)
from spyderlib.config.main import CONF, OPEN_FILES_PORT
from spyderlib.config.utils import IMPORT_EXT, is_gtk_desktop
from spyderlib.app.cli_options import get_options
from spyderlib import dependencies
from spyderlib.config.ipython import QTCONSOLE_INSTALLED
Expand Down Expand Up @@ -2488,9 +2488,7 @@ def open_file(self, fname, external=False):
"""
fname = to_text_string(fname)
ext = osp.splitext(fname)[1]
if ext in EDIT_EXT:
self.editor.load(fname)
elif self.variableexplorer is not None and ext in IMPORT_EXT:
if self.variableexplorer is not None and ext in IMPORT_EXT:
self.variableexplorer.import_data(fname)
elif encoding.is_text_file(fname):
self.editor.load(fname)
Expand Down
130 changes: 21 additions & 109 deletions spyderlib/config/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2009-2011 Pierre Raybaut
# Copyright © 2009- The Spyder Development Team
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)

Expand All @@ -18,136 +18,43 @@
# Local import
from spyderlib.config.user import UserConfig
from spyderlib.config.base import (CHECK_ALL, EXCLUDED_NAMES, SUBFOLDER,
get_home_dir, _)
from spyderlib.utils import iofuncs, codeanalysis
get_home_dir)
from spyderlib.config.utils import IMPORT_EXT, is_ubuntu
from spyderlib.utils import codeanalysis


#==============================================================================
# Extensions supported by Spyder's Editor
# Main constants
#==============================================================================
EDIT_FILETYPES = (
(_("Python files"), ('.py', '.pyw', '.ipy')),
(_("Cython/Pyrex files"), ('.pyx', '.pxd', '.pxi')),
(_("C files"), ('.c', '.h')),
(_("C++ files"), ('.cc', '.cpp', '.cxx', '.h', '.hh', '.hpp', '.hxx')),
(_("OpenCL files"), ('.cl', )),
(_("Fortran files"), ('.f', '.for', '.f77', '.f90', '.f95', '.f2k')),
(_("IDL files"), ('.pro', )),
(_("MATLAB files"), ('.m', )),
(_("Julia files"), ('.jl',)),
(_("Yaml files"), ('.yaml','.yml',)),
(_("Patch and diff files"), ('.patch', '.diff', '.rej')),
(_("Batch files"), ('.bat', '.cmd')),
(_("Text files"), ('.txt',)),
(_("reStructuredText files"), ('.txt', '.rst')),
(_("gettext files"), ('.po', '.pot')),
(_("NSIS files"), ('.nsi', '.nsh')),
(_("Web page files"), ('.scss', '.css', '.htm', '.html',)),
(_("XML files"), ('.xml',)),
(_("Javascript files"), ('.js',)),
(_("Json files"), ('.json',)),
(_("IPython notebooks"), ('.ipynb',)),
(_("Enaml files"), ('.enaml',)),
(_("Configuration files"), ('.properties', '.session', '.ini', '.inf',
'.reg', '.cfg', '.desktop')),
)

def _create_filter(title, ftypes):
return "%s (*%s)" % (title, " *".join(ftypes))

ALL_FILTER = "%s (*)" % _("All files")

def _get_filters(filetypes):
filters = []
for title, ftypes in filetypes:
filters.append(_create_filter(title, ftypes))
filters.append(ALL_FILTER)
return ";;".join(filters)

def _get_extensions(filetypes):
ftype_list = []
for _title, ftypes in filetypes:
ftype_list += list(ftypes)
return ftype_list

def get_filter(filetypes, ext):
"""Return filter associated to file extension"""
if not ext:
return ALL_FILTER
for title, ftypes in filetypes:
if ext in ftypes:
return _create_filter(title, ftypes)
else:
return ''

EDIT_FILTERS = _get_filters(EDIT_FILETYPES)
EDIT_EXT = _get_extensions(EDIT_FILETYPES)+['']

# Extensions supported by Spyder's Variable explorer
IMPORT_EXT = list(iofuncs.iofunctions.load_extensions.values())
# Find in files exclude patterns
EXCLUDE_PATTERNS = [r'\.pyc$|\.pyo$|\.orig$|\.hg|\.svn|\bbuild\b',
r'\.pyc$|\.pyo$|\.orig$|\.hg|\.svn']

# Extensions that should be visible in Spyder's file/project explorers
SHOW_EXT = ['.png', '.ico', '.svg']

# Extensions supported by Spyder (Editor or Variable explorer)
VALID_EXT = EDIT_EXT+IMPORT_EXT
SHOW_EXT = ['.py', '.ipynb', '.txt', '.dat', '.pdf', '.png', '.svg']


# Find in files include/exclude patterns
INCLUDE_PATTERNS = [r'|'.join(['\\'+_ext+r'$' for _ext in EDIT_EXT if _ext])+\
r'|README|INSTALL',
r'\.pyw?$|\.ipy$|\.txt$|\.rst$',
'.']
EXCLUDE_PATTERNS = [r'\.pyc$|\.pyo$|\.orig$|\.hg|\.svn|\bbuild\b',
r'\.pyc$|\.pyo$|\.orig$|\.hg|\.svn']
# Extensions supported by Spyder (Editor or Variable explorer)
USEFUL_EXT = IMPORT_EXT + SHOW_EXT


# Name filters for file/project explorers (excluding files without extension)
NAME_FILTERS = ['*' + _ext for _ext in VALID_EXT + SHOW_EXT if _ext]+\
['README', 'INSTALL', 'LICENSE', 'CHANGELOG']
NAME_FILTERS = ['README', 'INSTALL', 'LICENSE', 'CHANGELOG'] + \
['*' + _ext for _ext in USEFUL_EXT if _ext]


# Port used to detect if there is a running instance and to communicate with
# it to open external files
OPEN_FILES_PORT = 21128


# OS Specific
WIN = os.name == 'nt'
MAC = sys.platform == 'darwin'
CTRL = "Meta" if MAC else "Ctrl"


#==============================================================================
# Fonts
#==============================================================================
def is_ubuntu():
"Detect if we are running in an Ubuntu-based distribution"
if sys.platform.startswith('linux') and osp.isfile('/etc/lsb-release'):
release_info = open('/etc/lsb-release').read()
if 'Ubuntu' in release_info:
return True
else:
return False
else:
return False


def is_gtk_desktop():
"Detect if we are running in a Gtk-based desktop"
if sys.platform.startswith('linux'):
xdg_desktop = os.environ.get('XDG_CURRENT_DESKTOP', '')
if xdg_desktop:
gtk_desktops = ['Unity', 'GNOME', 'XFCE']
if any([xdg_desktop.startswith(d) for d in gtk_desktops]):
return True
else:
return False
else:
return False
else:
return False


SANS_SERIF = ['Sans Serif', 'DejaVu Sans', 'Bitstream Vera Sans',
'Bitstream Charter', 'Lucida Grande', 'MS Shell Dlg 2',
'Calibri', 'Verdana', 'Geneva', 'Lucid', 'Arial',
Expand All @@ -158,6 +65,9 @@ def is_gtk_desktop():
'Courier New', 'Courier', 'monospace', 'Fixed', 'Terminal']


#==============================================================================
# Adjust font size per OS
#==============================================================================
if sys.platform == 'darwin':
MONOSPACE = ['Menlo'] + MONOSPACE
BIG = MEDIUM = SMALL = 12
Expand Down Expand Up @@ -448,7 +358,7 @@ def is_gtk_desktop():
{
'enable': True,
'supported_encodings': ["utf-8", "iso-8859-1", "cp1252"],
'include': INCLUDE_PATTERNS,
'include': '',
'include_regexp': True,
'exclude': EXCLUDE_PATTERNS,
'exclude_regexp': True,
Expand Down Expand Up @@ -746,13 +656,15 @@ def is_gtk_desktop():
# or if you want to *rename* options, then you need to do a MAJOR update in
# version, e.g. from 3.0.0 to 4.0.0
# 3. You don't need to touch this value if you're just adding a new option
CONF_VERSION = '24.0.0'
CONF_VERSION = '24.1.0'


# XXX: Previously we had load=(not DEV) here but DEV was set to *False*.
# Check if it *really* needs to be updated or not
CONF = UserConfig('spyder', defaults=DEFAULTS, load=True, version=CONF_VERSION,
subfolder=SUBFOLDER, backup=True, raw_mode=True)


# Removing old .spyder.ini location:
old_location = osp.join(get_home_dir(), '.spyder.ini')
if osp.isfile(old_location):
Expand Down
161 changes: 161 additions & 0 deletions spyderlib/config/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2009- The Spyder Development Team
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)

"""
Utilities to define configuration values
"""

import os
import os.path as osp
import sys

from spyderlib.config.base import _
from spyderlib.utils import iofuncs


#==============================================================================
# Constants
#==============================================================================
# File types supported by the Editor up to Spyder 2.3
EDIT_FILETYPES = [
(_("Python files"), ('.py', '.pyw', '.ipy')),
(_("Cython/Pyrex files"), ('.pyx', '.pxd', '.pxi')),
(_("C files"), ('.c', '.h')),
(_("C++ files"), ('.cc', '.cpp', '.cxx', '.h', '.hh', '.hpp', '.hxx')),
(_("OpenCL files"), ('.cl', )),
(_("Fortran files"), ('.f', '.for', '.f77', '.f90', '.f95', '.f2k')),
(_("IDL files"), ('.pro', )),
(_("MATLAB files"), ('.m', )),
(_("Julia files"), ('.jl',)),
(_("Yaml files"), ('.yaml','.yml',)),
(_("Patch and diff files"), ('.patch', '.diff', '.rej')),
(_("Batch files"), ('.bat', '.cmd')),
(_("Text files"), ('.txt',)),
(_("reStructuredText files"), ('.txt', '.rst')),
(_("gettext files"), ('.po', '.pot')),
(_("NSIS files"), ('.nsi', '.nsh')),
(_("Web page files"), ('.scss', '.css', '.htm', '.html',)),
(_("XML files"), ('.xml',)),
(_("Javascript files"), ('.js',)),
(_("Json files"), ('.json',)),
(_("IPython notebooks"), ('.ipynb',)),
(_("Enaml files"), ('.enaml',)),
(_("Configuration files"), ('.properties', '.session', '.ini', '.inf',
'.reg', '.cfg', '.desktop')),
]

# Filter for all files
ALL_FILTER = "%s (*)" % _("All files")

# Extensions supported by Spyder's Variable explorer
IMPORT_EXT = list(iofuncs.iofunctions.load_extensions.values())


#==============================================================================
# Auxiliary functions
#==============================================================================
def _create_filter(title, ftypes):
return "%s (*%s)" % (title, " *".join(ftypes))


def _get_filters(filetypes):
filters = []
for title, ftypes in filetypes:
filters.append(_create_filter(title, ftypes))
filters.append(ALL_FILTER)
return ";;".join(filters)


def _get_extensions(filetypes):
ftype_list = []
for _title, ftypes in filetypes:
ftype_list += list(ftypes)
return ftype_list


def _get_pygments_extensions():
"""Return all file type extensions supported by Pygments"""
# NOTE: Leave this import here to keep startup process fast!
import pygments.lexers as lexers
extensions = []
all_lexers = lexers.get_all_lexers()
for lx in all_lexers:
lexer_exts = lx[2]
if lexer_exts:
extensions = extensions + list(lexer_exts)
return extensions


#==============================================================================
# Main functions
#==============================================================================
def get_filter(filetypes, ext):
"""Return filter associated to file extension"""
if not ext:
return ALL_FILTER
for title, ftypes in filetypes:
if ext in ftypes:
return _create_filter(title, ftypes)
else:
return ''


def get_edit_filetypes():
"""Get all file types supported by the Editor"""
pygments_exts = _get_pygments_extensions()
other_exts = ['*.ipynb', '*.md']
all_exts = tuple(pygments_exts + other_exts)
text_filetypes = (_("Supported text files"), all_exts)
return [text_filetypes] + EDIT_FILETYPES


def get_edit_filters():
"""
Return filters associated with the file types
supported by the Editor
"""
edit_filetypes = get_edit_filetypes()
return _get_filters(edit_filetypes)


def get_edit_extensions():
"""
Return extensions associated with the file types
supported by the Editor
"""
edit_filetypes = get_edit_filetypes()
return _get_extensions(edit_filetypes)+['']


#==============================================================================
# Detection of OS specific versions
#==============================================================================
def is_ubuntu():
"Detect if we are running in an Ubuntu-based distribution"
if sys.platform.startswith('linux') and osp.isfile('/etc/lsb-release'):
release_info = open('/etc/lsb-release').read()
if 'Ubuntu' in release_info:
return True
else:
return False
else:
return False


def is_gtk_desktop():
"Detect if we are running in a Gtk-based desktop"
if sys.platform.startswith('linux'):
xdg_desktop = os.environ.get('XDG_CURRENT_DESKTOP', '')
if xdg_desktop:
gtk_desktops = ['Unity', 'GNOME', 'XFCE']
if any([xdg_desktop.startswith(d) for d in gtk_desktops]):
return True
else:
return False
else:
return False
else:
return False
3 changes: 2 additions & 1 deletion spyderlib/plugins/configdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

from spyderlib.config.base import (_, running_in_mac_app, LANGUAGE_CODES,
save_lang_conf, load_lang_conf)
from spyderlib.config.main import CONF, is_gtk_desktop
from spyderlib.config.main import CONF
from spyderlib.config.utils import is_gtk_desktop
from spyderlib.config.gui import (CUSTOM_COLOR_SCHEME_NAME,
set_default_color_scheme)
from spyderlib.config.user import NoDefault
Expand Down
Loading

0 comments on commit 00e40a5

Please sign in to comment.