def populate_widget(widget, contents): """ uses the Parameters ---------- widget : QtGui:QWidget This widget has QLineEdits with names that correspond to the keys in the dictionary. contents : dict A dictionary containing key that correspond to line edits and values that will be inserted as text. This dictionary will be flattened if it contains a nested hierarchy. Returns ------- None """ if not isinstance(contents, dict): contents = xml_utils.node_to_dict(contents) for key, value in contents.items(): if isinstance(value, dict): populate_widget(widget, value) else: try: child_widget = getattr(widget.ui, key) except AttributeError: try: child_widget = getattr(widget, key) except AttributeError: child_widget = None set_text(child_widget, value)
def from_xml(self, attr): """ parses the xml code into the relevant timeperd elements Parameters ---------- metadata_date - the xml element timeperd and its contents Returns ------- None """ try: if attr.tag == 'attr': self.edoms = [] self.ui.listWidget.clear() for edom in attr.xpath('attrdomv/edom'): edom_dict = xml_utils.node_to_dict(edom, False) self.add_edom(**edom_dict) else: print ("The tag is not udom") except KeyError: pass
def get_usgs_contact_info(ad_username, as_dictionary=True): """ Parameters ---------- ad_username : str The active directory username to return the contact information for as_dictionary : bool specify return format as nested dictionary or lxml element Returns ------- None if ad_username is not found FGDC Contact Section as dictionary or lxml element """ result = requests_pem_get(USGS_AD_URL.format(ad_username)) element = xml_utils.string_to_node(result.content) try: if element.xpath('cntperp/cntper')[0].text == 'GS ScienceBase': element.xpath('cntperp')[0].tag = 'cntorgp' except: pass if as_dictionary: return xml_utils.node_to_dict(element) else: return element
def get_full_record_from_tsn(tsn, as_dataframe=False, **kwargs): """ Returns a list of items from the ITIS getFullHierarchyFromTSN function. The resulting pandas dataframe or list of dictionaries contains the tsn, as well as common name Parameters ---------- tsn : int The ITIS taxonomic serial number to query as_dataframe : bool if True return pandas dataframe, if False return list of dictionaries Returns ------- pandas dataframe or list of dictionaries with common names and tsns """ results = _get_xml(ITIS_BASE_URL + "getFullRecordFromTSN", payload={ "tsn": tsn }).getchildren()[0] if as_dataframe: dfs = collections.OrderedDict() for child in results.getchildren(): df = xml_utils.element_to_df([child]).dropna() dfs[xml_utils.parse_tag(child.tag)] = df return dfs else: return xml_utils.node_to_dict(results, add_fgdc=False)
def from_xml(self, contact_information): self.clear_widget() contact_dict = xml_utils.node_to_dict(contact_information) utils.populate_widget(self, contact_dict) addrtype_widget = self.findChild(QComboBox, 'fgdc_addrtype') if 'cntinfo' in contact_dict: contact_dict = contact_dict['cntinfo'] if 'fgdc_cntinfo' in contact_dict: contact_dict = contact_dict['fgdc_cntinfo'] try: addrtype = contact_dict['cntaddr']['addrtype'] addrtype_widget.setEditText(addrtype) except KeyError: pass try: if 'fgdc_cntorgp' in contact_dict: rbtn_orgp = self.findChild(QRadioButton, 'rbtn_orgp') rbtn_orgp.setChecked(True) elif 'fgdc_cntperp' in contact_dict: rbtn_perp = self.findChild(QRadioButton, 'rbtn_perp') rbtn_perp.setChecked(True) except KeyError: pass
def from_xml(self, contact_information): self.clear_widget() contact_dict = xml_utils.node_to_dict(contact_information) utils.populate_widget(self, contact_dict) addrtype_widget = self.findChild(QComboBox, "fgdc_addrtype") if "cntinfo" in contact_dict: contact_dict = contact_dict["cntinfo"] if "fgdc_cntinfo" in contact_dict: contact_dict = contact_dict["fgdc_cntinfo"] try: addrtype = contact_dict["cntaddr"]["addrtype"] addrtype_widget.setEditText(addrtype) except KeyError: pass try: if "fgdc_cntorgp" in contact_dict: rbtn_orgp = self.findChild(QRadioButton, "rbtn_orgp") rbtn_orgp.setChecked(True) elif "fgdc_cntperp" in contact_dict: rbtn_perp = self.findChild(QRadioButton, "rbtn_perp") rbtn_perp.setChecked(True) except KeyError: pass
def from_xml(self, spdom): self.original_xml = spdom self.clear_widget() utils.populate_widget(self, spdom) contents = xml_utils.node_to_dict(spdom, add_fgdc=False) if 'bounding' in contents: contents = contents['bounding'] try: if self.all_good_coords(): self.add_rect() self.update_map() else: self.remove_rect() except KeyError: self.remove_rect()
def _from_xml(self, spdom): self.original_xml = spdom utils.populate_widget(self, spdom) contents = xml_utils.node_to_dict(spdom, add_fgdc=False) if 'bounding' in contents: contents = contents['bounding'] try: jstr = """east = {eastbc}; west = {westbc}; south = {southbc}; north = {northbc}; updateMap(); fitMap(); """.format(**contents) self.frame.evaluateJavaScript(jstr) except KeyError: pass
def from_xml(self, attr): """ Populate widget with a representation of the passed XML element Parameters ---------- attr : XML Element Returns ------- None """ try: self.clear_widget() if attr.tag == 'attr': utils.populate_widget(self, attr) attr_dict = xml_utils.node_to_dict(attr) if not 'fgdc_attrdomv' in attr_dict.keys(): self.ui.comboBox.setCurrentIndex(3) elif 'fgdc_udom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(3) self._domain_content[3] = attr.xpath('attrdomv/udom')[0] elif 'fgdc_rdom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(1) self._domain_content[1] = attr.xpath('attrdomv/rdom')[0] elif 'fgdc_edom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(0) self._domain_content[0] = attr elif 'fgdc_codesetd' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(2) self._domain_content[2] = attr.xpath( 'attrdomv/codesetd')[0] else: self.ui.comboBox.setCurrentIndex(3) else: print("The tag is not attr") except KeyError: pass
def _from_xml(self, attr): """ parses the xml code into the relevant timeperd elements Parameters ---------- metadata_date - the xml element timeperd and its contents Returns ------- None """ try: if attr.tag == 'attr': utils.populate_widget(self, attr) attr_dict = xml_utils.node_to_dict(attr) if not 'fgdc_attrdomv' in attr_dict.keys(): self.ui.comboBox.setCurrentIndex(3) elif 'fgdc_udom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(3) self.domain._from_xml(attr.xpath('attrdomv/udom')[0]) elif 'fgdc_rdom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(1) self.domain._from_xml(attr.xpath('attrdomv/rdom')[0]) elif 'fgdc_edom' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(0) self.change_domain(0) self.domain._from_xml(attr) elif 'fgdc_codesetd' in attr_dict['fgdc_attrdomv'].keys(): self.ui.comboBox.setCurrentIndex(2) self.domain._from_xml(attr.xpath('attrdomv/codesetd')[0]) else: self.ui.comboBox.setCurrentIndex(3) else: print("The tag is not udom") except KeyError: pass
def get_usgs_contact_info(ad_username, as_dictionary=True): """ Parameters ---------- ad_username : str The active directory username to return the contact information for as_dictionary : bool specify return format as nested dictionary or lxml element Returns ------- None if ad_username is not found FGDC Contact Section as dictionary or lxml element """ result = requests.get(USGS_AD_URL.format(ad_username)) parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8') element = etree.fromstring(result.content, parser=parser) if as_dictionary: return xml_utils.node_to_dict(element) else: return element
def test_node_to_dict(): result = xml_utils.node_to_dict(element) assert result['fgdc_cntperp']['fgdc_cntper'] == 'Colin Talbert'
def test_node_to_dict(): result = xml_utils.node_to_dict(element) assert result["fgdc_cntperp"]["fgdc_cntper"] == "Colin Talbert"