/
utils.py
214 lines (176 loc) · 6.83 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
Utilities to define configuration values
"""
import os
import os.path as osp
import sys
from spyder.config.base import _
from spyder_kernels.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',
'.f03', '.f08')),
(_("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')),
(_("Markdown files"), ('.md', )),
]
# 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 = []
for lx in lexers.get_all_lexers():
lexer_exts = lx[2]
if lexer_exts:
# Reference: This line was included for leaving untrimmed the
# extensions not starting with `*`
other_exts = [le for le in lexer_exts if not le.startswith('*')]
# Reference: This commented line was replaced by the following one
# to trim only extensions that start with '*'
# lexer_exts = [le[1:] for le in lexer_exts]
lexer_exts = [le[1:] for le in lexer_exts if le.startswith('*')]
lexer_exts = [le for le in lexer_exts if not le.endswith('_*')]
extensions = extensions + list(lexer_exts) + list(other_exts)
return sorted(list(set(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"""
# The filter details are not hidden on Windows, so we can't use
# all Pygments extensions on that platform
if os.name == 'nt':
supported_exts = []
else:
try:
supported_exts = _get_pygments_extensions()
except Exception:
supported_exts = []
# NOTE: Try to not add too much extensions to this list to not
# make the filter look too big on Windows
favorite_exts = ['.py', '.R', '.jl', '.ipynb', '.md', '.pyw', '.pyx',
'.c', '.cpp', '.json', '.dat', '.csv', '.tsv', '.txt',
'.ini', '.html', '.js', '.h', '.bat']
other_exts = [ext for ext in supported_exts if ext not in favorite_exts]
all_exts = tuple(favorite_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
def is_kde_desktop():
"""Detect if we are running in a KDE desktop"""
if sys.platform.startswith('linux'):
xdg_desktop = os.environ.get('XDG_CURRENT_DESKTOP', '')
if xdg_desktop:
if 'KDE' in xdg_desktop:
return True
else:
return False
else:
return False
else:
return False
def is_anaconda():
"""
Detect if we are running under Anaconda.
Taken from https://stackoverflow.com/a/47610844/438386
"""
is_conda = osp.exists(osp.join(sys.prefix, 'conda-meta'))
return is_conda