def save(self, force=False): ''' Saves changes to the file. :return: saved, errors, msg :rtype: bool, bool, str ''' if self.isReadOnly(): return False, True, "Cannot save, the content was not loaded properly!" if force or self.document().isModified(): try: mtime = nm.nmd().file.save_file(self.filename, self.toPlainText().encode('utf-8'), 0 if force else self.file_mtime) self.file_mtime = mtime if mtime == 0: MessageBox.warning(self, "Warning", "File not saved and not error reported: %s" % os.path.basename(self.filename)) self.document().setModified(mtime == 0) ext = os.path.splitext(self.filename) # validate the xml structure of the launch files if ext[1] in self.CONTEXT_FILE_EXT: imported = False try: from lxml import etree imported = True parser = etree.XMLParser() etree.fromstring(self.toPlainText().encode('utf-8'), parser) except Exception as e: if imported: self.markLine(e.position[0]) return True, True, utf8(e) # validate the yaml structure of yaml files elif ext[1] in self.YAML_VALIDATION_FILES: try: import ruamel.yaml ruamel.yaml.load(self.toPlainText().encode('utf-8'), Loader=ruamel.yaml.Loader) except ruamel.yaml.MarkedYAMLError as e: return True, True, "YAML validation error: %s" % e return True, False, '' except IOError as ioe: if ioe.errno in [file_item.EFILE_CHANGED, file_item.EFILE_REMOVED]: result = MessageBox.question(self, "Changed file", "%s\n%s" % (utf8(ioe), "Save anyway?"), buttons=MessageBox.Yes | MessageBox.No) if result == MessageBox.Yes: return self.save(force=True) else: return False, True, utf8(ioe) except Exception as e: print(traceback.format_exc()) return False, True, utf8(e) return False, False, ''
def toprettyyaml(self): try: from . import yamlformatter formatter = yamlformatter.YamlFormatter() pretty_str = formatter.format_string(self.toPlainText().encode('utf-8')) cursor = self.textCursor() if not cursor.isNull(): cursor.beginEditBlock() cursor.movePosition(QTextCursor.Start) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.insertText(pretty_str) cursor.endEditBlock() except Exception as err: msg = "Format YAML failed: %s" % utf8(err) rospy.logwarn(msg) MessageBox.warning(self, "Warning", msg)
def _on_edit_interface_clicked(self): self._new_iface = False self.interface_field.setVisible(False) self.toolButton_CreateInterface.setVisible(False) self.toolButton_EditInterface.setVisible(False) self.textedit.setVisible(True) if self.interface_field.currentText() in self._interfaces_files: try: with open( self._interfaces_files[ self.interface_field.currentText()], 'rw') as f: iface = f.read() self.textedit.setText(iface) except Exception as e: MessageBox.warning(self, "Edit sync interface", "Error while open interface", utf8(e)) self.resize(350, 300)
def toprettyxml(self): try: from . import xmlformatter formatter = xmlformatter.Formatter(indent="2", indent_char=" ", encoding_output='utf-8', preserve=["literal"]) if self._is_launchfile: formatter.attr_order = ['if', 'unless', 'name', 'pkg', 'type'] xml_pretty_str = formatter.format_string(self.toPlainText().encode('utf-8')) cursor = self.textCursor() if not cursor.isNull(): cursor.beginEditBlock() cursor.movePosition(QTextCursor.Start) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.insertText(xml_pretty_str) cursor.endEditBlock() except Exception as err: msg = "Format XML failed: %s" % utf8(err) rospy.logwarn(msg) MessageBox.warning(self, "Warning", msg)
def accept(self): if self.textedit.isVisible(): try: tmp_file = os.path.join(screen.LOG_PATH, 'tmp_sync_interface.sync') with open(tmp_file, 'w+') as f: f.write(self.textedit.toPlainText()) from fkie_master_discovery.common import read_interface read_interface(tmp_file) if not self._new_iface and self.interface_field.currentText( ) in self._interfaces_files: fileName = self._interfaces_files[ self.interface_field.currentText()] else: fileName, _ = QFileDialog.getSaveFileName( self, 'Save sync interface', '/home', "Sync Files (*.sync)") if fileName: with open(fileName, 'w+') as f: self._interface_filename = fileName f.write(self.textedit.toPlainText()) if self._new_iface: self.interface_field.clear() self._interfaces_files = None self._on_select_interface_clicked() # QDialog.accept(self) # self.resetView() except Exception as e: MessageBox.warning(self, "Create sync interface", "Error while create interface", utf8(e)) elif self.interface_field.isVisible(): interface = self.interface_field.currentText() if self._interfaces_files and interface in self._interfaces_files: self._interface_filename = self._interfaces_files[interface] self._sync_args = [] self._sync_args.append(''.join(['_interface_url:=', interface])) QDialog.accept(self) self.resetView() else: QDialog.accept(self) self.resetView()
def show_error_msg(self, msg): MessageBox.warning(self, "Publish error", 'Error while publish to %s' % self.topic.name, tr(utf8(msg)))