def _date_expr_to_str(self, expr_el: _Element) -> str: date_spec = expr_el.find("./date_spec") duration = expr_el.find("./duration") string_parts = [] # "operation" is defined as mandatory in CIB schema operation = expr_el.get("operation", "") if operation == "date_spec": string_parts.append("date-spec") if date_spec is not None: string_parts.append(self._attrs_to_str(date_spec)) elif operation == "in_range": string_parts.extend(["date", "in_range"]) # CIB schema allows "start" + "duration" or optional "start" + "end" if "start" in expr_el.attrib: string_parts.append(str(expr_el.get("start", ""))) string_parts.append("to") if "end" in expr_el.attrib: string_parts.append(str(expr_el.get("end", ""))) if duration is not None: string_parts.append("duration") string_parts.append(self._attrs_to_str(duration)) else: # CIB schema allows operation=="gt" + "start" or # operation=="lt" + "end" string_parts.extend(["date", str(expr_el.get("operation", ""))]) if "start" in expr_el.attrib: string_parts.append(str(expr_el.get("start", ""))) if "end" in expr_el.attrib: string_parts.append(str(expr_el.get("end", ""))) return " ".join(string_parts)
def process_item(self, xml: etree._Element): """given an xml item return a dict, ensure is http://dtd.nlm.nih.gov/publishing/2.3 is mainly focussed on contributors and authors""" data = {} header = xml.find('.//{' + nsmap['oai'] + '}header') metadata = xml.find('.//{' + nsmap['oai'] + '}metadata') identifier = header.find('.//{' + nsmap['oai'] + '}identifier') data['original_identifier'] = identifier.text setSpec = header.find('.//{' + nsmap['oai'] + '}setSpec') data['spec'] = setSpec.text # article_meta = xml.find('.//{' + self.xmlns + '}article-meta') # contribs = metadata.findall('.//' + self.xmlns + 'contrib') # # print(contribs) creators, contributors = IrokoPerson.get_people_from_nlm(metadata) # = [] # for contrib in contribs: # p, is_author = IrokoPerson.get_people_from_nlm(contrib) # # print(is_author) # # TODO: los nombres de los autores se estan uniendo... # if p is not None: # if is_author: # creators.append(p) # else: # contributors.append(p) data['creators'] = creators data['contributors'] = contributors return data
def _get_parameter( self, parameter_element: _Element ) -> Optional[AgentParameterDto]: if parameter_element.get("name", None) is None: return None value_type = "string" default_value = None content_element = parameter_element.find("content") if content_element is not None: value_type = content_element.get("type", value_type) default_value = content_element.get("default", default_value) return AgentParameterDto( str(parameter_element.attrib["name"]), self._get_text_from_dom_element( parameter_element.find("shortdesc") ), self._get_text_from_dom_element(parameter_element.find("longdesc")), value_type, default_value, required=is_true(parameter_element.get("required", "0")), advanced=False, deprecated=is_true(parameter_element.get("deprecated", "0")), deprecated_by=[], obsoletes=parameter_element.get("obsoletes", None), unique=is_true(parameter_element.get("unique", "0")), pcs_deprecated_warning=None, )
def id_match(id_type: IdType, id_: str, person: _Element) -> bool: if id_type == IdType.CPR: id_element = person.find(id_type.value) else: employment_element = person.find("Employment") id_element = employment_element.find(id_type.value) if id_ == id_element.text.strip(): return True return False
def qualify_entry(entry: etree._Element, ns: dict): ''' :param entry: an 'entry' child of the arXiv feed element :return: whether the entry has a journal reference, a pdf link and a doi ''' whether = True whether = whether and entry.find('arxiv:doi', ns) is not None whether = whether and entry.find('arxiv:journal_ref', ns) is not None whether = whether and entry.find('atom:link[@type="application/pdf"]', ns) is not None return whether
def visit_NameIdentifier(self, node: Element): """Visit method for NameIdentifier tag.""" name_id_type = node.find(xsd_ns("NameIDType")).text if name_id_type == "21": self.contributor["nameIdentifier"] = { "nameIdentifier": node.find(xsd_ns("IDValue")).text, "nameIdentifierScheme": "ORCID", "schemeURI": "https://orcid.org/", } else: raise ValueError("type not yet implemented")
def _date_expr_to_dto(self, expr_el: _Element) -> CibRuleExpressionDto: date_spec = expr_el.find("./date_spec") duration = expr_el.find("./duration") return CibRuleExpressionDto( str(expr_el.get("id", "")), self._tag_to_type[str(expr_el.tag)], CibRuleInEffectStatus.UNKNOWN, export_attributes(expr_el, with_id=False), None if date_spec is None else self._date_common_to_dto(date_spec), None if duration is None else self._date_common_to_dto(duration), [], self._str_eval.get_str(expr_el), )
def _get_name(offer_tag: etree._Element) -> str: """Returns product name.""" name = offer_tag.find('name') if name is not None: name = name.text else: # Tries to combine name from prefix, # vendor and model name = ' '.join(o.text for o in (offer_tag.find('typePrefix'), offer_tag.find('vendor'), offer_tag.find('model')) if o is not None) return name
def visit_WorkIdentifier(self, node: Element): """Visit method for WorkIdentifier tag.""" if node.find(xsd_ns("WorkIDType")).text != "06": return self.append( "relatedIdentifiers", { "relatedIdentifier": node.find(xsd_ns("IDValue")).text, "relatedIdentifierType": "DOI", "relationType": "IsSupplementedBy", }, )
def _get_port_service(cls, xp: etree._Element, portinfo: PortInfo): """xp: xmlelement_port\n get: service name/product/product version/extrainfo/devicetype""" xservice = xp.find("service") if xservice is None: return service: str = xservice.get("name") if isinstance(service, str) and service != "": portinfo.service = service product: str = xservice.get("product") if isinstance(product, str) and product != "": portinfo.app = product version: str = xservice.get("version") if isinstance(version, str) and version != "": portinfo.version = version extrainfo: str = xservice.get("extrainfo") if isinstance(extrainfo, str) and extrainfo != "": portinfo.extrainfo += extrainfo devicetype = xservice.get("devicetype") if isinstance(devicetype, str) and devicetype != "": portinfo.device = devicetype # get cpe for service xcpe: str = xservice.find("cpe") if not xcpe is None: portinfo.set_cpe(xcpe.text)
def process_xml(con: Connection, bra_xml: ET._Element) -> List[Dict]: # split the XML in multiple entity object before merging them. bra_id = uuid4() weather_forecasts = _get_weather_forecast(bra_xml, bra_id) return [ { BraRecordTable: _get_bra_record(bra_xml, bra_id, con) }, { RiskTable: _get_risk(bra_xml.find("//RISQUE"), bra_id) }, { SnowRecordTable: _get_bra_snow_records(bra_xml, bra_id) }, { FreshSnowRecordTable: _get_fresh_snow_record(bra_xml, bra_id) }, { WeatherForecastTable: weather_forecasts["weather_forecast"] }, { WeatherForecastAtAltitudeTable: weather_forecasts["weather_forecast_at_altitude"] }, { RiskForecastTable: _get_risk_forecast(bra_xml, bra_id) }, ]
def _get_weather_forecast(bra_xml: _Element, bra_id: UUID) -> Dict: weather_forecasts = list() weather_forecasts_at_altitude = list() for record in bra_xml.find("//METEO").getchildren(): if record.tag == "ECHEANCE": wf_id = uuid4() weather_forecasts.append({ "wf_id": wf_id, "wf_bra_record": bra_id, "wf_expected_date": datetime.strptime(record.get("DATE"), "%Y-%m-%dT%H:%M:%S"), "wf_weather_type": WeatherType(int(record.get("TEMPSSENSIBLE"))) if record.get("TEMPSSENSIBLE") != '-1' else None, "wf_sea_of_clouds": int(record.get("MERNUAGES")), "wf_rain_snow_limit": int(record.get("PLUIENEIGE")), "wf_iso0": int(record.get("ISO0")) if record.get("ISO0") != '-1' else None, "wf_iso_minus_10": int(record.get("ISO-10")) if record.get("ISO-10") != '-1' else None, }) weather_forecasts_at_altitude += _get_weather_forecast_at_altitude( bra_xml, wf_id) return { "weather_forecast": weather_forecasts, "weather_forecast_at_altitude": weather_forecasts_at_altitude, }
def _get_fresh_snow_record(bra_xml: _Element, bra_id) -> Generator[Dict, None, None]: for record in bra_xml.find("//NEIGEFRAICHE").getchildren(): if record.tag == "NEIGE24H": yield { "fsr_bra_record": bra_id, "fsr_date": datetime.strptime(record.get("DATE"), "%Y-%m-%dT%H:%M:%S"), "fsr_altitude": int(bra_xml.find("//NEIGEFRAICHE").get("ALTITUDESS")), "sfr_massif_snowfall": int(record.get("SS241")), "fsr_second_massif_snowfall": int(record.get("SS242")), }
def _read_response(self, r_xml: Et._Element) -> bool: e = r_xml.find(self.response_xpath) if e is not None and e.text: self.result = e.text LOGGER.debug('AsConnector SceneGetActive request successful. Found scene %s', self.result) return True if self.result else False
def from_lxml_element(cls, element: _Element) -> "Siri": sd_element = element.find("x:ServiceDelivery", namespaces=_NSMAP) if sd_element is None: raise SiriParsingError("missing 'ServiceDelivery'.") service_delivery = ServiceDelivery.from_lxml_element( element=sd_element) return cls(service_delivery=service_delivery)
def get_sd_person_changed( identifier_type: IdType, identifier: str, xml_root: _Element ) -> SdPersonChange: # Get the request date timestamps request_structure = xml_root.find("RequestStructure") activation_date = request_structure.find("ActivationDate").text.strip() deactivation_date = request_structure.find("DeactivationDate").text.strip() def id_match(id_type: IdType, id_: str, person: _Element) -> bool: if id_type == IdType.CPR: id_element = person.find(id_type.value) else: employment_element = person.find("Employment") id_element = employment_element.find(id_type.value) if id_ == id_element.text.strip(): return True return False person_elements = xml_root.findall("Person") persons = filter(partial(id_match, identifier_type, identifier), person_elements) sd_person_changed = SdPersonChange( start_date=activation_date, end_date=deactivation_date, change=only(persons) ) return sd_person_changed
def pipe_connector_symbol_handler(node: xml._Element, ctx: Context) -> svgwrite.container.Group: """ Handler to process Proteus PipeConnectorSymbol node. This is a complex node which contains other nodes :param node: :param ctx: :return: """ ensure_type(node, 'PipeConnectorSymbol') pipe_conn_group = create_group(ctx, node) cross_page_conn = node.find('CrossPageConnection') if cross_page_conn is not None: pipe_conn_group.attribs[ 'data-drawing-name'] = cross_page_conn.attrib.get('DrawingName') pipe_conn_group.attribs[ 'data-drawing-link-label'] = cross_page_conn.attrib.get( 'LinkLabel') # todo we could also need support for CrossPageConnection with linkedPersistentId if node.attrib.get(ATTR_COMP_NAME) is not None: shape_reference = ctx.get_from_shape_catalog( 'PipeConnectorSymbol', node.attrib[ATTR_COMP_NAME]) process_shape_reference(node, shape_reference, ctx) return pipe_conn_group
def xml_get_opt( item: XMLElement, path: Optional[str] = None, attr: Optional[str] = None, *, xpath: bool = False, ) -> Optional[str]: """ Helper for LXML to get an attribute at a path from an XML element. Returns None if the path or attribute do not exist. """ if path: if xpath: elems = item.xpath(path) assert isinstance(elems, list) elem = elems[0] if elems else None assert elem is None or isinstance(elem, XMLElement) else: elem = item.find(path) else: elem = item if elem is None: return None ret = elem.get(attr) if attr else elem.text if ret is None: return None if isinstance(ret, bytes): raise ValueError("Expected result to be a str not bytes.") return ret
def _sub_element_matches_spec(self, e: etree._Element, name: str, value): matching_subelement = e.find(self._add_namespace(name)) if matching_subelement is None and name != "text": return value is None elif matching_subelement is not None: return matching_subelement.text == value else: # matching_subelement is None and name == "text" return e.text == value
def visit_OtherText(self, node: Element): """Visit method for OtherText tag.""" self.abstracts.append({ "description": node.find(xsd_ns("Text")).text.strip(), "descriptionType": "Abstract", })
def parse(cls, element: etree._Element, subcls: T, tag: str = None, **kwargs) -> "Optional[Source]": sid = element.get('id') language = element.get('language') alignment_id = element.get('alignment-id') title = element.find('title').text citation_part = element.find('citation-part').text editorial_note = element.find('editorial-note').text annotator = element.find('annotator').text reviewer = element.find('reviewer').text original_url = element.find('electronic-text-original-url').text source = super(Source, cls).parse(element.find('div'), subcls, tag, **kwargs) source.id = sid source.language = language source.alignment_id = alignment_id source.title = title source.citation_part = citation_part source.editorial_note = editorial_note source.annotator = annotator source.reviewer = reviewer source.original_url = original_url source.tag = sid return source
def verse_from_ab(ab: et._Element, ignore): ref = ab.get('verse').replace('-APP', '') basetext = construct_basetext(ab) all_apps = get_all_apps(ab, ignore) app = ab.find('app') selected_app = get_focus_app(app) rdgs = get_all_rdgs(app) arcs, nodes = get_arcs(app) return ref, basetext, all_apps, app, selected_app, rdgs, arcs, nodes, ab
def _read_response(self, r_xml: Et._Element) -> bool: e = r_xml.find(self.response_xpath) if e is not None and e.text[0].isdigit(): self.result = e.text if self.result: LOGGER.debug('AsConnector VersionInfo request successful. Found version %s', self.result) return True if self.result else False
def _is_element_empty(element: _Element, xml_annotation: XMLAnnotation) -> bool: if isinstance(xml_annotation, XMLAttribute): return xml_annotation.tag not in element.attrib else: return ( element.find('*') is None and element.text is None and not element.attrib )
def _read_response(self, r_xml: Et._Element) -> bool: e = r_xml.find(self.response_xpath) LOGGER.debug('Event/Selected result: %s', r_xml) if e is not None: self.result = NodeInfo.get_node_from_as_connector_element(e) return True else: return False
def _read_response(self, r_xml: Et._Element) -> bool: e = r_xml.find(self.response_xpath) result = True if e is not None and e.text == self.expected_result else False if result: LOGGER.debug('AsConnector LoadFilepart request successful: %s', self.result) else: LOGGER.error('AsConnector LoadFilepart request failed: %s %s', e, self.to_string()) return result
def _read_response(self, r_xml: Et._Element) -> bool: e = r_xml.find(self.response_xpath) if e is not None: self.result = NodeInfo.get_node_from_as_connector_element(e) LOGGER.debug('Node/Create result: %s', self.result) return True else: LOGGER.error('Node Create Request failed: %s', self.to_string()) return False
def _get_risk_forecast(bra_xml: _Element, bra_id: UUID) -> Generator[Dict, None, None]: for forecast in bra_xml.find("//TENDANCES").getchildren(): evol = RiskEvolution(int(forecast.get("VALEUR"))) yield { "rf_bra_record": bra_id, "rf_date": datetime.strptime(forecast.get("DATE"), "%Y-%m-%dT%H:%M:%S"), "rf_evolution": evol, }
def get_oai_header_data(header: Header = None, xml: _Element = None): if not (header or xml): # pragma: no cover raise Exception("Must provide header or xml") if header and xml: # pragma: no cover raise Exception("You must provide only header or xml") if xml: header = Header(xml.find('.//' + get_namespace(xml) + 'header')) datestamp = header.datestamp oai_identifier = header.identifier deleted = header.deleted return datestamp, deleted, oai_identifier
def parse(annotation: etree._Element, tag: str = 'annotation', **kwargs) -> "Annotation": XMLItem.check_tag(annotation, tag) morphology = Morphology.parse(element=annotation.find('morphology'), subcls=Morphology.cls) pos = annotation.find('parts-of-speech') if pos is not None: parts_of_speech = Field.parse(pos, subcls=Value, **kwargs) gls = annotation.find('gloss') if gls is not None: gloss = Field.parse(gls, subcls=Value, **kwargs) return Annotation(annotation.get('tag'), annotation.tag, morphology, parts_of_speech, gloss)