def test_symlinks(tmpdir): """ Check that modifying symlinks files changes source file and keeps symlinks. """ base_dir = tmpdir.mkdir("symlinks") base_file = base_dir.join("symlinks_text.txt") base_file_path = to_text_string(base_file) # Write base file write("Some text for symlink", base_file_path) # Create symlink symlink_file = pathlib.Path(base_dir.join('link-to-symlinks_text.txt')) symlink_file.symlink_to(base_file_path) symlink_file_path = to_text_string(symlink_file) # Assert the symlink was created assert os.path.islink(symlink_file_path) # Write using the symlink encoding = write("New text for symlink", symlink_file_path) # Assert symlink is valid and contents of the file assert os.path.islink(symlink_file_path) assert base_file.read_text(encoding) == symlink_file.read_text(encoding) assert symlink_file.read_text(encoding) == 'New text for symlink'
def add_to_history(self, command): """Add command to history""" command = to_text_string(command) if command in ['', '\n'] or command.startswith('Traceback'): return if command.endswith('\n'): command = command[:-1] self.histidx = None if len(self.history) > 0 and self.history[-1] == command: return self.history.append(command) text = os.linesep + command # When the first entry will be written in history file, # the separator will be append first: if self.history_filename not in self.HISTORY_FILENAMES: self.HISTORY_FILENAMES.append(self.history_filename) text = self.SEPARATOR + text # Needed to prevent errors when writing history to disk # See issue 6431 try: encoding.write(text, self.history_filename, mode='ab') except EnvironmentError: pass if self.append_to_history is not None: self.append_to_history.emit(self.history_filename, text)
def get_filename_text(self, filename): """ Read and return content from filename. Parameters ---------- filename: str The file path to read. Returns ------- str Content of the filename. """ # Avoid a possible error when reading the history file try: text, _ = encoding.read(filename) except (IOError, OSError): text = "# Previous history could not be read from disk, sorry\n\n" text = normalize_eols(text) linebreaks = [m.start() for m in re.finditer('\n', text)] if len(linebreaks) > MAX_LINES: text = text[linebreaks[-MAX_LINES - 1] + 1:] # Avoid an error when trying to write the trimmed text to disk. # See spyder-ide/spyder#9093. try: encoding.write(text, filename) except (IOError, OSError): pass return text
def create_script(fname): """Create a new Python script""" text = os.linesep.join(["# -*- coding: utf-8 -*-", "", ""]) try: encoding.write(str(text), fname, 'utf-8') except EnvironmentError as error: QMessageBox.critical( _("Save Error"), _("<b>Unable to save file '%s'</b>" "<br><br>Error message:<br>%s") % (osp.basename(fname), str(error)))
def test_timestamp(tmpdir): """Check that the modification timestamp is preserved.""" tmp_file = tmpdir.mkdir("timestamp").join('test_file.txt') tmp_file = to_text_string(tmp_file) # Write a file write("Test text", tmp_file) st = os.stat(tmp_file) actual_creation_time = st.st_atime # Write the file and check that creation time is preserved. write('New text', tmp_file) creation_time = os.stat(tmp_file).st_atime assert actual_creation_time == creation_time
def add_history(self, filename): """ Add new history tab Slot for add_history signal emitted by shell instance """ filename = encoding.to_unicode_from_fs(filename) if filename in self.filenames: return editor = codeeditor.CodeEditor(self) if osp.splitext(filename)[1] == '.py': language = 'py' else: language = 'bat' editor.setup_editor(linenumbers=self.get_option('line_numbers'), language=language, scrollflagarea=False) editor.focus_changed.connect(lambda: self.focus_changed.emit()) editor.setReadOnly(True) color_scheme = self.get_color_scheme() editor.set_font( self.get_plugin_font(), color_scheme ) editor.toggle_wrap_mode( self.get_option('wrap') ) # Avoid a possible error when reading the history file try: text, _ = encoding.read(filename) except (IOError, OSError): text = "# Previous history could not be read from disk, sorry\n\n" text = normalize_eols(text) linebreaks = [m.start() for m in re.finditer('\n', text)] maxNline = self.get_option('max_entries') if len(linebreaks) > maxNline: text = text[linebreaks[-maxNline - 1] + 1:] # Avoid an error when trying to write the trimmed text to # disk. # See issue 9093 try: encoding.write(text, filename) except (IOError, OSError): pass editor.set_text(text) editor.set_cursor_position('eof') self.editors.append(editor) self.filenames.append(filename) index = self.tabwidget.addTab(editor, osp.basename(filename)) self.find_widget.set_editor(editor) self.tabwidget.setTabToolTip(index, filename) self.tabwidget.setCurrentIndex(index)
def add_history(self, filename): """ Add new history tab Slot for add_history signal emitted by shell instance """ filename = encoding.to_unicode_from_fs(filename) if filename in self.filenames: return editor = codeeditor.CodeEditor(self) if osp.splitext(filename)[1] == '.py': language = 'py' else: language = 'bat' editor.setup_editor(linenumbers=self.get_option('line_numbers'), language=language, scrollflagarea=False) editor.focus_changed.connect(lambda: self.focus_changed.emit()) editor.setReadOnly(True) color_scheme = self.get_color_scheme() editor.set_font(self.get_font(), color_scheme) editor.toggle_wrap_mode(self.get_option('wrap')) # Avoid a possible error when reading the history file try: text, _ = encoding.read(filename) except (IOError, OSError): text = "# Previous history could not be read from disk, sorry\n\n" text = normalize_eols(text) linebreaks = [m.start() for m in re.finditer('\n', text)] maxNline = self.get_option('max_entries') if len(linebreaks) > maxNline: text = text[linebreaks[-maxNline - 1] + 1:] # Avoid an error when trying to write the trimmed text to # disk. # See issue 9093 try: encoding.write(text, filename) except (IOError, OSError): pass editor.set_text(text) editor.set_cursor_position('eof') self.editors.append(editor) self.filenames.append(filename) index = self.tabwidget.addTab(editor, osp.basename(filename)) self.find_widget.set_editor(editor) self.tabwidget.setTabToolTip(index, filename) self.tabwidget.setCurrentIndex(index)
def save_historylog(self): """Save current history log (all text in console)""" title = _("Save history log") self.sig_redirect_stdio_requested.emit(False) filename, _selfilter = getsavefilename(self, title, self.historylog_filename, "%s (*.log)" % _("History logs")) self.sig_redirect_stdio_requested.emit(True) if filename: filename = osp.normpath(filename) try: encoding.write(to_text_string(self.get_text_with_eol()), filename) self.historylog_filename = filename CONF.set('main', 'historylog_filename', filename) except EnvironmentError: pass
def save_historylog(self): """Save current history log (all text in console)""" title = _("Save history log") self.redirect_stdio.emit(False) filename, _selfilter = getsavefilename(self, title, self.historylog_filename, "%s (*.log)" % _("History logs")) self.redirect_stdio.emit(True) if filename: filename = osp.normpath(filename) try: encoding.write(to_text_string(self.get_text_with_eol()), filename) self.historylog_filename = filename CONF.set('main', 'historylog_filename', filename) except EnvironmentError: pass
def test_permissions(tmpdir): """Check that file permissions are preserved.""" p_file = tmpdir.mkdir("permissions").join("permissions_text.txt") p_file = to_text_string(p_file) # Write file and define execution permissions write("Some text", p_file) st = os.stat(p_file) mode = st.st_mode | stat.S_IEXEC os.chmod(p_file, mode) old_mode = os.stat(p_file).st_mode # Write the file and check permissions write("Some text and more", p_file) new_mode = os.stat(p_file).st_mode assert old_mode == new_mode
def save_historylog(self): """Save current history log (all text in console)""" title = _("Save history log") self.redirect_stdio.emit(False) filename, _selfilter = getsavefilename(self, title, self.historylog_filename, "%s (*.log)" % _("History logs")) self.redirect_stdio.emit(True) if filename: filename = osp.normpath(filename) try: encoding.write(to_text_string(self.get_text_with_eol()), filename) self.historylog_filename = filename CONF.set('main', 'historylog_filename', filename) except EnvironmentError as error: QMessageBox.critical(self, title, _("<b>Unable to save file '%s'</b>" "<br><br>Error message:<br>%s" ) % (osp.basename(filename), to_text_string(error)))
def add_to_history(self, command): """Add command to history""" command = to_text_string(command) if command in ['', '\n'] or command.startswith('Traceback'): return if command.endswith('\n'): command = command[:-1] self.histidx = None if len(self.history) > 0 and self.history[-1] == command: return self.history.append(command) text = os.linesep + command # When the first entry will be written in history file, # the separator will be append first: if self.history_filename not in HISTORY_FILENAMES: HISTORY_FILENAMES.append(self.history_filename) text = self.SEPARATOR + text encoding.write(text, self.history_filename, mode='ab') if self.append_to_history is not None: self.append_to_history.emit(self.history_filename, text)
def add_to_history(self, command): """Add command to history""" command = to_text_string(command) if command in ['', '\n'] or command.startswith('Traceback'): return if command.endswith('\n'): command = command[:-1] self.histidx = None if len(self.history)>0 and self.history[-1] == command: return self.history.append(command) text = os.linesep + command # When the first entry will be written in history file, # the separator will be append first: if self.history_filename not in HISTORY_FILENAMES: HISTORY_FILENAMES.append(self.history_filename) text = self.SEPARATOR + text encoding.write(text, self.history_filename, mode='ab') if self.append_to_history is not None: self.append_to_history.emit(self.history_filename, text)
def add_history(self, filename): """ Add new history tab Slot for add_history signal emitted by shell instance """ filename = encoding.to_unicode_from_fs(filename) if filename in self.filenames: return editor = codeeditor.CodeEditor(self) if osp.splitext(filename)[1] == '.py': language = 'py' else: language = 'bat' editor.setup_editor(linenumbers=self.get_option('line_numbers'), language=language, scrollflagarea=False) editor.focus_changed.connect(lambda: self.focus_changed.emit()) editor.setReadOnly(True) color_scheme = self.get_color_scheme() editor.set_font( self.get_plugin_font(), color_scheme ) editor.toggle_wrap_mode( self.get_option('wrap') ) text, _ = encoding.read(filename) text = normalize_eols(text) linebreaks = [m.start() for m in re.finditer('\n', text)] maxNline = self.get_option('max_entries') if len(linebreaks) > maxNline: text = text[linebreaks[-maxNline - 1] + 1:] encoding.write(text, filename) editor.set_text(text) editor.set_cursor_position('eof') self.editors.append(editor) self.filenames.append(filename) index = self.tabwidget.addTab(editor, osp.basename(filename)) self.find_widget.set_editor(editor) self.tabwidget.setTabToolTip(index, filename) self.tabwidget.setCurrentIndex(index)
def create_script(fname): """Create a new Python script""" text = os.linesep.join(["# -*- coding: utf-8 -*-", "", ""]) encoding.write(to_text_string(text), fname, 'utf-8')