def show_kernel_error(self, error): """Show kernel initialization errors in infowidget.""" self.error_text = error # Filter out benign errors if "http://bugs.python.org/issue1666807" in error: # See spyder-ide/spyder#16828 return InstallerIPythonKernelError(error) # Replace end of line chars with <br> eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') # Don't break lines in hyphens # From https://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') # Create error page message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) self.info_page = kernel_error_template.substitute( css_path=self.css_path, message=message, error=error) # Show error self.set_info_page() self.shellwidget.hide() self.infowidget.show() # Tell the client we're in error mode self.is_error_shown = True
def show_kernel_error(self, error): """Show kernel initialization errors in infowidget.""" # Replace end of line chars with <br> eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') # Don't break lines in hyphens # From https://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') # Create error page message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) page = kernel_error_template.substitute(css_path=self.css_path, message=message, error=error) # Show error self.infowidget.setHtml(page, QUrl.fromLocalFile(self.css_path)) self.shellwidget.hide() self.infowidget.show() # Tell the client we're in error mode self.is_error_shown = True
def show_kernel_error(self, error): """Show kernel initialization errors in infowidget""" # Remove explanation about how to kill the kernel (doesn't apply to us) error = error.split('issues/2049')[-1] # Remove unneeded blank lines at the beginning eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') while error.startswith('<br>'): error = error[4:] # Remove connection message if error.startswith('To connect another client') or \ error.startswith('[IPKernelApp] To connect another client'): error = error.split('<br>') error = '<br>'.join(error[2:]) # Don't break lines in hyphens # From http://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) page = kernel_error_template.substitute(css_path=CSS_PATH, message=message, error=error) self.infowidget.setHtml(page)
def show_kernel_error(self, error): """Show kernel initialization errors in infowidget.""" # Replace end of line chars with <br> eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') # Don't break lines in hyphens # From https://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') # Create error page message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) self.info_page = kernel_error_template.substitute( css_path=self.css_path, message=message, error=error) # Show error self.set_info_page() self.shellwidget.hide() self.infowidget.show() # Tell the client we're in error mode self.is_error_shown = True
def test_save_with_preferred_eol_chars(editor_plugin, python_files, qtbot, os_name): """Check that saving files with preferred eol chars works as expected.""" filenames, tmpdir = python_files editorstack = editor_plugin.get_current_editorstack() eol_lookup = {'posix': 'LF', 'nt': 'CRLF', 'mac': 'CR'} # Set options editor_plugin.set_option('convert_eol_on_save', True) editor_plugin.set_option('convert_eol_on_save_to', eol_lookup[os_name]) editor_plugin.apply_plugin_settings( {'convert_eol_on_save', 'convert_eol_on_save_to'}) # Load a test file fname = filenames[0] editor_plugin.load(fname) qtbot.wait(500) codeeditor = editor_plugin.get_current_editor() # Set file as dirty, save it and check that it has the right eol. codeeditor.document().setModified(True) editorstack.save() with open(fname, mode='r', newline='') as f: text = f.read() assert get_eol_chars(text) == get_eol_chars_from_os_name(os_name)
def set_eol_chars(self, text): """Set widget end-of-line (EOL) characters from text (analyzes text)""" if not is_text_string(text): # testing for QString (PyQt API#1) text = to_text_string(text) eol_chars = sourcecode.get_eol_chars(text) if eol_chars is not None and self.eol_chars is not None: self.document().setModified(True) self.eol_chars = eol_chars
def test_get_eol_chars_no_eol(): eol_chars = sourcecode.get_eol_chars('foo') if os.name == 'nt': assert eol_chars == "\r\n" elif sys.platform.startswith('linux'): assert eol_chars == "\n" elif sys.platform == 'darwin': assert eol_chars == "\r"
def show_kernel_error(self, error): """Show kernel initialization errors.""" # Remove unneeded blank lines at the beginning eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') # Don't break lines in hyphens # From http://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') message = _("An error occurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) page = kernel_error_template.substitute(css_path=CSS_PATH, message=message, error=error) self.setHtml(page)
def test_toggle_eol_chars(editor_plugin, python_files, qtbot, os_name): """ Check that changing eol chars from the 'Convert end-of-line characters' menu works as expected. """ filenames, tmpdir = python_files editorstack = editor_plugin.get_current_editorstack() # Load a test file fname = filenames[0] editor_plugin.load(fname) qtbot.wait(500) codeeditor = editor_plugin.get_current_editor() # Change to a different eol, save and check that file has the right eol. editor_plugin.toggle_eol_chars(os_name, True) assert codeeditor.document().isModified() editorstack.save() with open(fname, mode='r', newline='') as f: text = f.read() assert get_eol_chars(text) == get_eol_chars_from_os_name(os_name)
def show_kernel_error(self, error): """Show kernel initialization errors in infowidget.""" self.error_text = error if self.is_benign_error(error): return InstallerIPythonKernelError(error) # Replace end of line chars with <br> eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') # Don't break lines in hyphens # From https://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') # Create error page message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) self.info_page = kernel_error_template.substitute( css_path=self.css_path, message=message, error=error) # Show error if self.infowidget is not None: self.set_info_page() self.shellwidget.hide() self.infowidget.show() # Tell the client we're in error mode self.is_error_shown = True # Stop shellwidget self.shellwidget.shutdown() self.remove_std_files(is_last_client=False)
def test_save_with_os_eol_chars(editor_plugin, mocker, qtbot, tmpdir): """Check that saving new files uses eol chars according to OS.""" editorstack = editor_plugin.get_current_editorstack() # Mock output of save file dialog. fname = osp.join(tmpdir, 'test_eol_chars.py') mocker.patch.object(editor_module, 'getsavefilename') editor_module.getsavefilename.return_value = (fname, '') # Load new, empty file editor_plugin.new() qtbot.wait(500) codeeditor = editor_plugin.get_current_editor() # Write some blank lines on it. for __ in range(3): qtbot.keyClick(codeeditor, Qt.Key_Return) # Save file and check that it has the right eol. editorstack.save() with open(fname, mode='r', newline='') as f: text = f.read() assert get_eol_chars(text) == os.linesep
def replace_find(self, focus_replace_text=False, replace_all=False): """Replace and find.""" if self.editor is None: return replace_text = to_text_string(self.replace_text.currentText()) search_text = to_text_string(self.search_text.currentText()) re_pattern = None case = self.case_button.isChecked() re_flags = re.MULTILINE if case else re.IGNORECASE | re.MULTILINE # Check regexp before proceeding if self.re_button.isChecked(): try: re_pattern = re.compile(search_text, flags=re_flags) # Check if replace_text can be substituted in re_pattern # Fixes spyder-ide/spyder#7177. re_pattern.sub(replace_text, '') except re.error: # Do nothing with an invalid regexp return first = True cursor = None while True: if first: # First found seltxt = to_text_string(self.editor.get_selected_text()) cmptxt1 = search_text if case else search_text.lower() cmptxt2 = seltxt if case else seltxt.lower() if re_pattern is None: has_selected = self.editor.has_selected_text() if has_selected and cmptxt1 == cmptxt2: # Text was already found, do nothing pass else: if not self.find(changed=False, forward=True, rehighlight=False): break else: if len(re_pattern.findall(cmptxt2)) > 0: pass else: if not self.find(changed=False, forward=True, rehighlight=False): break first = False wrapped = False position = self.editor.get_position('cursor') position0 = position cursor = self.editor.textCursor() cursor.beginEditBlock() else: position1 = self.editor.get_position('cursor') if is_position_inf( position1, position0 + len(replace_text) - len(search_text) + 1): # Identify wrapping even when the replace string # includes part of the search string wrapped = True if wrapped: if (position1 == position or is_position_sup(position1, position)): # Avoid infinite loop: replace string includes # part of the search string break if position1 == position0: # Avoid infinite loop: single found occurrence break position0 = position1 if re_pattern is None: cursor.removeSelectedText() cursor.insertText(replace_text) else: seltxt = to_text_string(cursor.selectedText()) # Note: If the selection obtained from an editor spans a line # break, the text will contain a Unicode U+2029 paragraph # separator character instead of a newline \n character. # See: spyder-ide/spyder#2675 eol_char = get_eol_chars(self.editor.toPlainText()) seltxt = seltxt.replace(u'\u2029', eol_char) cursor.removeSelectedText() cursor.insertText(re_pattern.sub(replace_text, seltxt)) if self.find_next(set_focus=False): found_cursor = self.editor.textCursor() cursor.setPosition(found_cursor.selectionStart(), QTextCursor.MoveAnchor) cursor.setPosition(found_cursor.selectionEnd(), QTextCursor.KeepAnchor) else: break if not replace_all: break if cursor is not None: cursor.endEditBlock() if focus_replace_text: self.replace_text.setFocus() else: self.editor.setFocus() if getattr(self.editor, 'document_did_change', False): self.editor.document_did_change()
def test_get_eol_chars(): eol_chars = sourcecode.get_eol_chars('foo\r\n') assert eol_chars == '\r\n'