Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def select_directory(self):
"""Select directory"""
self.__redirect_stdio_emit(False)
directory = getexistingdirectory(
self, _("Select directory"), self.path)
if directory:
directory = to_unicode_from_fs(osp.abspath(directory))
self.__redirect_stdio_emit(True)
return directory
fname= --> create file
"""
# If no text is provided, create default content
if text is None:
default_content = True
text, enc = encoding.read(self.TEMPLATE_PATH)
enc_match = re.search('-*- coding: ?([a-z0-9A-Z\-]*) -*-', text)
if enc_match:
enc = enc_match.group(1)
# Initialize template variables
# Windows
username = encoding.to_unicode_from_fs(os.environ.get('USERNAME',
''))
# Linux, Mac OS X
if not username:
username = encoding.to_unicode_from_fs(os.environ.get('USER',
'-'))
VARS = {
'date': time.ctime(),
'username': username,
}
try:
text = text % VARS
except:
pass
else:
default_content = False
enc = encoding.read(self.TEMPLATE_PATH)[1]
create_fname = lambda n: to_text_string(_("untitled")) + ("%d.py" % n)
# Creating editor widget
if editorstack is None:
def _convert(fname):
fname = osp.abspath(encoding.to_unicode_from_fs(fname))
if os.name == 'nt' and len(fname) >= 2 and fname[1] == ':':
fname = fname[0].upper()+fname[1:]
return fname
empty = False
try:
if text is None:
default_content = True
text, enc = encoding.read(self.TEMPLATE_PATH)
enc_match = re.search(r'-*- coding: ?([a-z0-9A-Z\-]*) -*-',
text)
if enc_match:
enc = enc_match.group(1)
# Initialize template variables
# Windows
username = encoding.to_unicode_from_fs(
os.environ.get('USERNAME', ''))
# Linux, Mac OS X
if not username:
username = encoding.to_unicode_from_fs(
os.environ.get('USER', '-'))
VARS = {
'date': time.ctime(),
'username': username,
}
try:
text = text % VARS
except Exception:
pass
else:
default_content = False
enc = encoding.read(self.TEMPLATE_PATH)[1]
except (IOError, OSError):
text = ''
enc = 'utf-8'
default_content = True
def get_home_dir():
"""Return user home directory."""
try:
# expanduser() returns a raw byte string which needs to be
# decoded with the codec that the OS is using to represent
# file paths.
path = encoding.to_unicode_from_fs(osp.expanduser('~'))
except Exception:
path = ''
if osp.isdir(path):
return path
else:
# Get home from alternative locations
for env_var in ('HOME', 'USERPROFILE', 'TMP'):
# os.environ.get() returns a raw byte string which needs to be
# decoded with the codec that the OS is using to represent
# environment variables.
path = encoding.to_unicode_from_fs(os.environ.get(env_var, ''))
if osp.isdir(path):
return path
else:
path = ''
"""Open the project located in `path`"""
self.notify_project_open(path)
self.switch_to_plugin()
if path is None:
basedir = get_home_dir()
path = getexistingdirectory(parent=self,
caption=_("Open project"),
basedir=basedir)
path = encoding.to_unicode_from_fs(path)
if not self.is_valid_project(path):
if path:
QMessageBox.critical(self, _('Error'),
_("<b>%s</b> is not a Spyder project!") % path)
return
else:
path = encoding.to_unicode_from_fs(path)
self.add_to_recent(path)
# A project was not open before
if self.current_active_project is None:
if save_previous_files and self.main.editor is not None:
self.main.editor.save_open_files()
if self.main.editor is not None:
self.main.editor.set_option('last_working_dir',
getcwd_or_home())
if self.get_option('visible_if_project_open'):
self.show_explorer()
else:
# We are switching projects
if self.main.editor is not None:
self.set_project_filenames(
def set_current_shell_working_directory(self, directory):
"""Set current shell working directory"""
shellwidget = self.__find_python_shell()
if shellwidget is not None:
directory = encoding.to_unicode_from_fs(directory)
shellwidget.shell.set_cwd(directory)
# expanduser() returns a raw byte string which needs to be
# decoded with the codec that the OS is using to represent
# file paths.
path = encoding.to_unicode_from_fs(osp.expanduser('~'))
except Exception:
path = ''
if osp.isdir(path):
return path
else:
# Get home from alternative locations
for env_var in ('HOME', 'USERPROFILE', 'TMP'):
# os.environ.get() returns a raw byte string which needs to be
# decoded with the codec that the OS is using to represent
# environment variables.
path = encoding.to_unicode_from_fs(os.environ.get(env_var, ''))
if osp.isdir(path):
return path
else:
path = ''
if not path:
raise RuntimeError('Please set the environment variable HOME to '
'your user/home directory path so Spyder can '
fname=None --> fname will be 'untitledXX.py' but do not create file
fname= --> create file
"""
# If no text is provided, create default content
empty = False
try:
if text is None:
default_content = True
text, enc = encoding.read(self.TEMPLATE_PATH)
enc_match = re.search(r'-*- coding: ?([a-z0-9A-Z\-]*) -*-',
text)
if enc_match:
enc = enc_match.group(1)
# Initialize template variables
# Windows
username = encoding.to_unicode_from_fs(
os.environ.get('USERNAME', ''))
# Linux, Mac OS X
if not username:
username = encoding.to_unicode_from_fs(
os.environ.get('USER', '-'))
VARS = {
'date': time.ctime(),
'username': username,
}
try:
text = text % VARS
except Exception:
pass
else:
default_content = False
enc = encoding.read(self.TEMPLATE_PATH)[1]
# don't interfere with the rest of html present in the page
argspec = escape(context['argspec'])
for char in ['=', ',', '(', ')', '*', '**']:
argspec = argspec.replace(char,
'<span class="argspec-highlight">' + char + '</span>')
context['argspec'] = argspec
doc_file = codecs.open(rst_name, 'w', encoding='utf-8')
doc_file.write(docstring)
doc_file.close()
temp_confdir = False
if temp_confdir:
# TODO: This may be inefficient. Find a faster way to do it.
confdir = mkdtemp()
confdir = encoding.to_unicode_from_fs(confdir)
generate_configuration(confdir)
else:
confdir = osp.join(get_module_source_path('spyder.plugins.help.utils'))
confoverrides = {'html_context': context}
doctreedir = osp.join(srcdir, 'doctrees')
sphinx_app = Sphinx(srcdir, confdir, destdir, doctreedir, buildername,
confoverrides, status=None, warning=None,
freshenv=True, warningiserror=False, tags=None)
try:
sphinx_app.build(None, [rst_name])
except SystemMessage:
output = _("It was not possible to generate rich text help for this "
"object.<br>"