def validate(self, schema='fgdc', as_dataframe=True): """ Returns a list of schema validation errors for a given CSDGM XML file. Parameters ---------- xsl_fname : str (optional) can be one of: 'fgdc' - uses the standard fgdc schema ../resources/FGDC/fgdc-std-001-1998-annotated.xsd 'bdp' = use the Biological Data profile schema, ../resources/FGDC/BDPfgdc-std-001-1998-annotated.xsd full file path to another local schema. if not specified defaults to 'fgdc' as_dataframe : bool used to specify return format (list of tuples or dataframe) Returns ------- list of tuples (xpath, error message, line number) or pandas dataframe """ from pymdwizard.core import fgdc_utils return fgdc_utils.validate_xml(self._contents.to_xml(), xsl_fname=schema, as_dataframe=as_dataframe)
def format_errors(xml_document, which="bdp"): if which == "bdp": xsl_fname = utils.get_resource_path("FGDC/BDPfgdc-std-001-1998-annotated.xsd") else: xsl_fname = utils.get_resource_path("FGDC/fgdc-std-001-1998-annotated.xsd") return fgdc_utils.validate_xml(xml_document.record, xsl_fname)
def validate(self): """ Check the current record against the schema and highlight any error widgets Returns ------- None """ if self.metadata_root.schema == 'bdp': xsl_fname = utils.get_resource_path('FGDC/BDPfgdc-std-001-1998-annotated.xsd') else: xsl_fname = utils.get_resource_path('FGDC/fgdc-std-001-1998-annotated.xsd') from pymdwizard.core import fgdc_utils errors = fgdc_utils.validate_xml(self.metadata_root._to_xml(), xsl_fname) self.clear_validation() marked_errors = [] self.widget_lookup = self.metadata_root.make_tree(widget=self.metadata_root) error_count = 0 for error in errors: xpath, error_msg, line_num = error if xpath not in marked_errors: action = QAction(self, visible=True) action.setText(error_msg) action.setData(xpath) action.triggered.connect(self.goto_error) self.ui.menuErrors.addAction(action) marked_errors.append(xpath) # widget = self.metadata_root.get_widget(xpath) widgets = self.widget_lookup.xpath_march(xpath, as_list=True) for widget in widgets: if isinstance(widget, list): for w in widget: print('problem highlighting error', xpath, widget) else: self.highlight_error(widget.widget, error_msg) self.error_widgets.append(widget.widget) error_count += 1 if errors: msg = "There are {} errors in this record".format(error_count) self.statusBar().showMessage(msg, 20000) msg += "\n\n These errors are highlighted in red in the form below." msg += "\n\n These errors are also listed in the Validation Menu's Errors submenu item above." msg += "\n Clicking each error will take you to the section it is contained in." msg += "\n Note that some highlighed errors can be in collapsed items, scrolled out of view, or in non-selected tabs" QMessageBox.warning(self, "Validation", msg) else: msg = "Congratulations there were No FGDC Errors!" self.statusBar().showMessage(msg, 20000) QMessageBox.information(self, "Validation", msg)
def validate(self): if self.metadata_root.schema == 'bdp': xsl_fname = utils.get_resource_path('FGDC/BDPfgdc-std-001-1998-annotated.xsd') else: xsl_fname = utils.get_resource_path('FGDC/fgdc-std-001-1998-annotated.xsd') from pymdwizard.core import fgdc_utils errors = fgdc_utils.validate_xml(self.metadata_root._to_xml(), xsl_fname) self.clear_validation() for error in errors: xpath, error_msg, line_num = error widget = self.metadata_root.get_widget(xpath) self.error_widgets.append(widget) if widget.objectName() not in ['metadata_root', 'fgdc_metadata']: widget.setToolTip(error_msg) widget.setStyleSheet( """ QGroupBox#{widgetname}{{ background-color: rgb(255,76,77); border: 2px solid red; subcontrol-position: top left; /* position at the top left*/ padding-top: 20px; font: bold 14px; color: rgb(90, 90, 90); }} QGroupBox#{widgetname}::title {{ text-align: left; subcontrol-origin: padding; subcontrol-position: top left; /* position at the top center */padding: 3 3px; }} QLabel{{ font: 9pt "Arial"; color: rgb(90, 90, 90); }} QLineEdit#{widgetname}, QPlainTextEdit#{widgetname}, QComboBox#{widgetname} {{ font: 9pt "Arial"; color: rgb(50, 50, 50); background-color: rgb(255,76,77); opacity: 25; }} QToolTip {{ background-color: rgb(255,76,77); border-color: red; opacity: 255; }} """.format(widgetname=widget.objectName()))
def validate(self): """ Check the current record against the schema and highlight any error widgets Returns ------- None """ self.error_list_dialog.show() if self.metadata_root.schema == "bdp": xsl_fname = utils.get_resource_path( "FGDC/BDPfgdc-std-001-1998-annotated.xsd" ) else: xsl_fname = utils.get_resource_path("FGDC/fgdc-std-001-1998-annotated.xsd") from pymdwizard.core import fgdc_utils errors = fgdc_utils.validate_xml(self.metadata_root.to_xml(), xsl_fname) self.clear_validation() marked_errors = [] # We need to expand / populate all attributes that have an error for error in errors: try: xpath, error_msg, line_num = error if "attr" in xpath: try: detailed_index = xpath.split("/detailed[")[1].split("/")[0][:-1] detailed_index = int(detailed_index) - 1 except IndexError: detailed_index = 0 try: attr_index = xpath.split("/attr[")[1].split("/")[0][:-1] attr_index = int(attr_index) - 1 except IndexError: attr_index = 0 self.metadata_root.eainfo.detaileds[ detailed_index ].attributes.attrs[attr_index].regular_me() self.metadata_root.eainfo.detaileds[ detailed_index ].attributes.attrs[attr_index].supersize_me() except: pass widget_lookup = self.metadata_root.make_tree(widget=self.metadata_root) self.metadata_root.add_children( self.metadata_root.spatial_tab, widget_lookup.metadata.idinfo ) self.metadata_root.add_children( self.metadata_root.dataqual.sourceinput, widget_lookup.metadata.dataqual.lineage, ) error_count = 0 for error in errors: try: xpath, error_msg, line_num = error if xpath not in marked_errors: self.error_list.add_error(error_msg, xpath) marked_errors.append(xpath) # widget = self.metadata_root.get_widget(xpath) widgets = widget_lookup.xpath_march(xpath, as_list=True) for widget in widgets: if isinstance(widget, list): for w in widget: print("problem highlighting error", xpath, widget) else: self.highlight_error(widget.widget, error_msg) self.error_widgets.append(widget.widget) error_count += 1 except BaseException as e: import traceback msg = "Error encountered highlighting error:" msg += "\t" + xpath msg += "\n\n" + traceback.format_exc() QMessageBox.warning(self, "Bug encountered", msg) widget_lookup = self.metadata_root.make_tree(widget=self.metadata_root) if errors: msg = "There are {} errors in this record".format(error_count) self.statusBar().showMessage(msg, 20000) msg += "\n\n These errors are highlighted in red in the form below." msg += "\n\n These errors are also listed in the Validation Errors Form that just popped up." msg += "\n Clicking each error will take you to the section it is contained in." msg += "\n Note that some highlighed errors can be in collapsed items, scrolled out of view, or in non-selected tabs" QMessageBox.warning(self, "Validation", msg) self.error_list_dialog.show() else: msg = "Congratulations there were No FGDC Errors!" self.statusBar().showMessage(msg, 20000) QMessageBox.information(self, "Validation", msg)
def validate(self, schema='fgdc', as_dataframe=True): from pymdwizard.core import fgdc_utils return fgdc_utils.validate_xml(self._contents.to_xml(), xsl_fname=schema, as_dataframe=as_dataframe)