def parseFeed(self, feed): root = ET.parse(feed) try: channel = root.find('.//channel') if channel: title = channel.find('title') self.title = ET.iselement(title) and title.text or 'None' description = channel.find('description') self.description = ET.iselement(description) and description.text or 'None' pubDate = channel.find('pubDate') self.pubDate = ET.iselement(pubDate) and pubDate.text or 'None' items = channel.findall('item') for item in items: newItem = self.Item() element = item.find('title') newItem.title = element.text or 'None' element = item.find('description') newItem.description = element.text or 'None' element = item.find('pubDate') if element is not None: newItem.date = element.text or 'None' enclosure = item.find('enclosure') if enclosure is not None: newItem.url = 'url' in enclosure.attrib and enclosure.attrib['url'] or 'None' newItem.type = 'type' in enclosure.attrib and enclosure.attrib['type'] or 'None' newItem.length = 'length' in enclosure.attrib and enclosure.attrib['length'] or 'None' self.items.append(newItem) except AttributeError: pass
def GetProperty(self, name): subelement = self.element.find(name) if ET.iselement(subelement): data = subelement.text elif self.definition and ET.iselement(self.definition.find(name)): self.SetProperty(name,'') return '' else: error_str = "%s not found in %s." % (name, self.element.tag) log.debug(error_str) raise PropertyNotFoundError(error_str) if data: data = data.strip() if ET.iselement(self.definition): try: type = self.definition.find(name).text property = self.ApplyType(data, type) except ValueError: property = self.ApplyType("", type) except AttributeError: return data else: property = data return property if ET.iselement(self.definition): try: type = self.definition.find(name).text return self.ApplyType("", type) except AttributeError: return "" else: return ""
def job_get_svn(job_name): server = jenkins.Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) job_config = server.get_job_config(job_name) root = ET.fromstring(job_config.encode('utf8')) svn_url = root.find( 'scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote') if ET.iselement(svn_url): svn_url = svn_url.text tag_dir = root.find( 'properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/' + 'hudson.scm.listtagsparameter.ListSubversionTagsParameterDefinition/tagsDir' ) if ET.iselement(tag_dir): tag_dir = tag_dir.text tags_filter = root.find( 'properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/' + 'hudson.scm.listtagsparameter.ListSubversionTagsParameterDefinition/tagsFilter' ) if ET.iselement(tags_filter): tags_filter = tags_filter.text return {'svn_url': svn_url, 'tag_dir': tag_dir, 'tags_filter': tags_filter}
def __getMultiDict(self, xmlTree, xmlDict, cnt=0): """ :Notes: Populates the Dictionary. :Args: xmlTree (XML): XML object of Request xmlDict (dict) : Dictionary to be populated Recursively generates all the (key,value) pairs as Dictionary. """ if cElementTree.iselement(xmlTree) and xmlTree.getchildren(): if xmlTree.tag in xmlDict: tag_count = len([i for i in xmlDict if i.startswith(xmlTree.tag)]) key = xmlTree.tag + "_" + str(tag_count) else: key = xmlTree.tag xmlDict[key] = {} for elem in xmlTree.getchildren(): self.__getMultiDict(elem, xmlDict[key]) elif cElementTree.iselement(xmlTree) and not xmlTree.getchildren(): if xmlTree.text: for key in valuedict: if key in xmlTree.text: xmlTree.text = xmlTree.text.replace(key, valuedict[key]) if xmlTree.tag in xmlDict: tag_count = len([i for i in xmlDict if i.startswith(xmlTree.tag)]) xmlDict[xmlTree.tag + "_" + str(tag_count)] = (xmlTree.text or "").strip() or None else: xmlDict[xmlTree.tag] = (xmlTree.text or "").strip() or None return xmlDict
def parseFeed(self, feed): root = ET.parse(feed) try: channel = root.find('.//channel') if channel: title = channel.find('title') self.title = ET.iselement(title) and title.text or 'None' description = channel.find('description') self.description = ET.iselement( description) and description.text or 'None' pubDate = channel.find('pubDate') self.pubDate = ET.iselement(pubDate) and pubDate.text or 'None' items = channel.findall('item') for item in items: newItem = self.Item() element = item.find('title') newItem.title = element.text or 'None' element = item.find('description') newItem.description = element.text or 'None' element = item.find('pubDate') if element is not None: newItem.date = element.text or 'None' enclosure = item.find('enclosure') if enclosure is not None: newItem.url = 'url' in enclosure.attrib and enclosure.attrib[ 'url'] or 'None' newItem.type = 'type' in enclosure.attrib and enclosure.attrib[ 'type'] or 'None' newItem.length = 'length' in enclosure.attrib and enclosure.attrib[ 'length'] or 'None' self.items.append(newItem) except AttributeError: pass
def negateEvents(input, output=None, verbose=False): if not (ET.iselement(input) and input.tag == "sentence"): print >> sys.stderr, "Loading corpus file", input corpusTree = ETUtils.ETFromObj(input) corpusRoot = corpusTree.getroot() if not (ET.iselement(input) and input.tag == "sentence"): sentences = corpusRoot.getiterator("sentence") else: sentences = [input] counts = defaultdict(int) for sentence in sentences: for entity in sentence.findall("entity"): counts["all-entities"] += 1 eType = entity.get("type") if not isNegatableEPITrigger(eType): counts["out-of-scope"] += 1 continue eBaseType = getEPIBaseType(eType) eText = entity.get("text").lower() eNewType = determineNewType(eType, eText) # Insert changed charOffset counts["entities"] += 1 if verbose: print "Entity", entity.get("id"), [entity.get("text")], [ eType, eBaseType, eNewType ], if eNewType != eBaseType: counts["negated"] += 1 if verbose: print "NEGATED", if eNewType == eType: counts["correct"] += 1 if verbose: print "CORRECT" else: counts["incorrect"] += 1 if eNewType == eBaseType: counts["incorrect-pos"] += 1 else: counts["incorrect-neg"] += 1 if verbose: print "INCORRECT" entity.set("type", eNewType) if verbose: print counts if not (ET.iselement(input) and input.tag == "sentence"): if output != None: print >> sys.stderr, "Writing output to", output ETUtils.write(corpusRoot, output) return corpusTree
def objToEl(el, objects): """ Convert 'object' arguments to ET.Element types and append them to el. """ assert ET.iselement(el) for obj in objects: if isinstance(obj, IntacctObjectType): obj = obj() elif isinstance(obj, str): obj = ET.fromstring(obj) if not ET.iselement(obj): raise Exception('Unable to process object: %s' % str(obj)) el.append(obj)
def negateEvents(input, output=None, verbose=False): if not (ET.iselement(input) and input.tag == "sentence"): print >> sys.stderr, "Loading corpus file", input corpusTree = ETUtils.ETFromObj(input) corpusRoot = corpusTree.getroot() if not (ET.iselement(input) and input.tag == "sentence"): sentences = corpusRoot.getiterator("sentence") else: sentences = [input] counts = defaultdict(int) for sentence in sentences: for entity in sentence.findall("entity"): counts["all-entities"] += 1 eType = entity.get("type") if not isNegatableEPITrigger(eType): counts["out-of-scope"] += 1 continue eBaseType = getEPIBaseType(eType) eText = entity.get("text").lower() eNewType = determineNewType(eType, eText) # Insert changed charOffset counts["entities"] += 1 if verbose: print "Entity", entity.get("id"), [entity.get("text")], [eType, eBaseType, eNewType], if eNewType != eBaseType: counts["negated"] += 1 if verbose: print "NEGATED", if eNewType == eType: counts["correct"] += 1 if verbose: print "CORRECT" else: counts["incorrect"] += 1 if eNewType == eBaseType: counts["incorrect-pos"] += 1 else: counts["incorrect-neg"] += 1 if verbose: print "INCORRECT" entity.set("type", eNewType) if verbose: print counts if not (ET.iselement(input) and input.tag == "sentence"): if output != None: print >> sys.stderr, "Writing output to", output ETUtils.write(corpusRoot, output) return corpusTree
def __SendDataPart(data, connection): """This method is deprecated, use atom.http._send_data_part""" deprecated('call to deprecated function __SendDataPart') if isinstance(data, six.binary_type): connection.send(data) return elif isinstance(data, six.text_type): connection.send(data.encode()) return elif ElementTree.iselement(data): connection.send(ElementTree.tostring(data)) return # Check to see if data is a file-like object that has a read method. elif hasattr(data, 'read'): # Read the file and send it a chunk at a time. while True: binarydata = data.read(100000) if not binarydata: break connection.send(binarydata) return else: # The data object was not a file. # Try to convert to a string and send the data. connection.send(str(data)) return
def run(cls,inFile,multiplier=1.0,outFile=None,targetLabel="neg", binary=False): """inFile can be a string with file name (.xml or .xml.gz) or an ElementTree or an Element or an open input stream multiplier adjusts the level of boosting the non-negative predictions, it is a real number (0,inf) multiplier 1.0 does nothing, <1.0 decreases negative class confidence, >1.0 increases negative class confidence the root of the modified tree is returned and, if outFile is a string, written out to outFile as well""" print >> sys.stderr, "##### Recall adjust with multiplier " + str(multiplier)[:5] + " #####" tree=ETUtils.ETFromObj(inFile) if not ET.iselement(tree): assert isinstance(tree,ET.ElementTree) root=tree.getroot() else: root = tree if multiplier != -1: if binary: print >> sys.stderr, "Recall binary mode" classRanges = getClassRanges(root.getiterator("entity")) assert len(classRanges.keys()) in [0,2] if len(classRanges.keys()) == 0: print >> sys.stderr, "Warning, recall adjustment skipped because no prediction weights found" else: print >> sys.stderr, "Recall multiclass mode" classRanges = None for entityNode in root.getiterator("entity"): adjustEntity(entityNode,targetLabel,multiplier,classRanges) if outFile: ETUtils.write(root,outFile) return tree
def load_from_xml(self, element): """Populate object using Xml Document Node""" self._class_id = element.tag meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case( self._class_id) if meta_class_id: self._class_id = meta_class_id if NamingPropertyId.DN in element.attrib: self.dn = element.attrib[NamingPropertyId.DN] if self.dn: self.rn = os.path.basename(self.dn) # Write the attribute and value to dictionary properties, as it is . self.write_to_attributes(element) # Run the load_from_xml for each child_node recursively and populate child list too. child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element): continue child = _GenericMO() self._child.append(child) child.load_from_xml(child_element)
def load_from_xml(self, element, handle): """ Method creates the object from the XML representation of the an external method object. """ from Imc import class_factory self.set_handle(handle) if element.attrib: for attr_name, attr_value in element.attrib.iteritems(): attr = ImcUtils.word_u(attr_name) if attr in CoreUtils.get_property_list(self._class_id): at_meta = CoreUtils.get_method_property_meta( self._class_id, attr) if at_meta.io == "Input" or at_meta.is_complex_type: continue self.set_attr(attr, str(attr_value)) elif attr_name in _EXTERNAL_METHOD_ATTRS: self.set_attr(_EXTERNAL_METHOD_ATTRS[attr_name], str(attr_value)) child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element): continue cln = ImcUtils.word_u(child_element.tag) if cln in CoreUtils.get_property_list(self._class_id): prop_meta = CoreUtils.get_method_property_meta( self._class_id, cln) if prop_meta.io == "Output" and prop_meta.is_complex_type: child = class_factory(prop_meta.field_type) if child != None: self.set_attr(cln, child) child.load_from_xml(child_element, handle)
def load_from_xml(self, element, handle): """ Method creates the object from the xml representation of the managed object. """ self.set_handle(handle) if element.attrib: for attr_name, attr_value in element.attrib.iteritems(): attr = ImcUtils.word_u(attr_name) if CoreUtils.find_class_id_in_mo_meta_ignore_case( self._class_id) != None: if attr in CoreUtils.get_property_list(self._class_id): self.set_attr(attr, str(attr_value)) else: self.set_attr(attr, str(attr_value)) else: self.set_attr(ImcUtils.word_u(attr), str(attr_value)) if self.get_attr("Rn") == None and self.get_attr("Dn") != None: self.set_attr("Rn", str(re.sub(r'^.*/', '', self.get_attr("Dn")))) child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element): continue if child_element.tag in self.prop_mo_meta.field_names: pass #TODO: Need code analysis. child = ManagedObject(ImcUtils.word_u(child_element.tag)) self._child.append(child) child.load_from_xml(child_element, handle)
def __init__(self, xmltree, imageprovider=None): self.root = None self.imageprovider = imageprovider self.stroke = None self.fill = None self.level = 0 self.seen = set() self.skip = set() self.styles = {} self.hasFill = False self.gradient = None self.hasStroke = False self.bounds = Box() if hasattr(xmltree, 'element'): xmltree = xmltree.element self.xmltree = xmltree # svg fragment? if etree.iselement(xmltree) and xmltree.tag == self.SVG_ROOT: root = xmltree else: root = xmltree.getroot() if not root.tag == self.SVG_ROOT: raise ValueError("Expected SVG fragment as root object") # parse svg constructs for quicker display self.preparse(root)
def __new__(cls,tag,thing = None,*args,**kwargs): if hasattr(tag,'__xml__'): return tag.__xml__() self = object.__new__(xml) if cElementTree.iselement(tag): self.__content = tag elif isinstance(tag,cElementTree.ElementTree): self.__content = tag.getroot() elif is_file(tag): self.__content = cElementTree.parse(tag).getroot() elif isinstance(tag,str) and len(tag) > 0 and tag[0] == '<': self.__content = cElementTree.fromstring(tag) else: if type(tag) != str: raise TypeError("Cannot convert %s object to xml" % str(type(tag))) self.__content = cElementTree.fromstring('<%s/>' % tag) if is_text(thing) or type(thing) == int: self.__content.text = text(thing) elif thing != None: self.append(xml(thing)) for subthing in args: self.append(xml(subthing)) for key,value in kwargs.items(): if key == '__class' or key == 'klass': self['class'] = value else: self[key] = value if '{' in self.__content.tag: self.__prefix = PREFIX_PAT.search(self.__content.tag).groups()[0] else: self.__prefix = '' return self
def convert(arg): """ Convert an object to its xml representation """ if iselement(arg): return arg # the element if isinstance(arg, dict_accessor): try: return arg.text_ except: raise Exception("Cannot serialize %s, missing text_ attribute" % (arg,)) if isinstance(arg, dict): return arg # attributes of the element if isinstance(arg, unicode): return arg if isinstance(arg, decimal.Decimal): return unicode(arg) if arg is True: return 'true' if arg is False: return 'false' if isinstance(arg, float): return unicode(round(arg, 2)) # there's nothing less than cents anyway if isinstance(arg, (int, long)): return unicode(arg) if isinstance(arg, str): raise Exception("'%s' not unicode: can only accept unicode strings" % (arg,)) raise Exception("Cannot convert %s of type %s" % (arg, type(arg)))
def __format_attributes(elt_data): """ Private method which converts a single XML tag to a python dict. It also checks that the elt_data given as argument is of type xml.etree.ElementTree.Element :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element """ rval = {} if not ET.iselement(elt_data): raise NmapParserException("Error while trying to parse supplied " "data attributes: format is not XML or " "XML tag is empty") try: for dkey in elt_data.keys(): rval[dkey] = elt_data.get(dkey) if rval[dkey] is None: raise NmapParserException("Error while trying to build-up " "element attributes: empty " "attribute {0}".format(dkey)) except: raise return rval
def __init__(self, xmltree, imageprovider = None): self.root = None self.imageprovider = imageprovider self.stroke = None self.fill = None self.level = 0 self.seen = set() self.skip = set() self.styles = {} self.hasFill = False self.gradient = None self.hasStroke = False self.bounds = Box() if hasattr(xmltree,'element'): xmltree = xmltree.element self.xmltree = xmltree # svg fragment? if etree.iselement(xmltree) and xmltree.tag == self.SVG_ROOT: root = xmltree else: root = xmltree.getroot() if not root.tag == self.SVG_ROOT: raise ValueError("Expected SVG fragment as root object") # parse svg constructs for quicker display self.preparse(root)
def getTrack(self, track_elem): """ Given a track element (which is returned by getSequence), read the relevant information. Returns dictionary containing the following: locked = True/False enabled = True/False width = width in pixels (if specified) height = height in pixels (if specified) clip_list = list of clipitem SubElements that are a part of this track. To read data from the clips, use getClip """ if not ET.iselement(track_elem) or track_elem.tag != "track": self.logger.error("FCP.getTrack must be given a <track> item") return {} self.cur_elem = track_elem track_info = {"clip_list": []} for item in track_elem: if item.tag == "clipitem" or item.tag == "transitionitem": track_info["clip_list"].append(item) self.parent_pointers[item] = track_elem elif item.tag == "locked": # TODO: determine if FCP XML is case-sensitive about its boolean values track_info["locked"] = (item.text.upper() == "TRUE") elif item.tag == "enabled": track_info["enabled"] = (item.text.upper() == "TRUE") elif item.tag == "format": track_info.update(self._getFormat(item)) return track_info
def load_from_xml(self, element, handle): """ Method creates the object from the XML representation of the an external method object. """ from Imc import class_factory self.set_handle(handle) if element.attrib: for attr_name, attr_value in element.attrib.iteritems(): attr = ImcUtils.word_u(attr_name) if attr in CoreUtils.get_property_list(self._class_id): at_meta = CoreUtils.get_method_property_meta(self._class_id, attr) if at_meta.io == "Input" or at_meta.is_complex_type: continue self.set_attr(attr, str(attr_value)) elif attr_name in _EXTERNAL_METHOD_ATTRS: self.set_attr(_EXTERNAL_METHOD_ATTRS[attr_name], str(attr_value)) child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element) : continue cln = ImcUtils.word_u(child_element.tag) if cln in CoreUtils.get_property_list(self._class_id): prop_meta = CoreUtils.get_method_property_meta(self._class_id, cln) if prop_meta.io == "Output" and prop_meta.is_complex_type: child = class_factory(prop_meta.field_type) if child != None: self.set_attr(cln, child) child.load_from_xml(child_element, handle)
def load_from_xml(self, element, handle): """ Method creates the object from the xml representation of the managed object. """ self.set_handle(handle) if element.attrib: for attr_name, attr_value in element.attrib.iteritems(): attr = ImcUtils.word_u(attr_name) if CoreUtils.find_class_id_in_mo_meta_ignore_case(self._class_id) != None: if attr in CoreUtils.get_property_list(self._class_id): self.set_attr(attr, str(attr_value)) else: self.set_attr(attr, str(attr_value)) else: self.set_attr(ImcUtils.word_u(attr), str(attr_value)) if self.get_attr("Rn") == None and self.get_attr("Dn") != None: self.set_attr("Rn", str(re.sub(r'^.*/', '', self.get_attr("Dn")))) child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element) : continue if child_element.tag in self.prop_mo_meta.field_names: pass #TODO: Need code analysis. child = ManagedObject(ImcUtils.word_u(child_element.tag)) self._child.append(child) child.load_from_xml(child_element, handle)
def load_from_xml(self, element): """Populate object using Xml Document Node""" self._class_id = element.tag meta_class_id = CoreUtils.find_class_id_in_mo_meta_ignore_case(self._class_id) if meta_class_id: self._class_id = meta_class_id if NamingPropertyId.DN in element.attrib: self.dn = element.attrib[NamingPropertyId.DN] if self.dn: self.rn = os.path.basename(self.dn) # Write the attribute and value to dictionary properties, as it is . self.write_to_attributes(element) # Run the load_from_xml for each child_node recursively and populate child list too. child_elements = element.getchildren() if child_elements: for child_element in child_elements: if not ET.iselement(child_element) : continue child = _GenericMO() self._child.append(child) child.load_from_xml(child_element)
def from_xml(self, elem): # , handle, modify_self=False, mo=None): """Method updates/fills the object from the xml representation of the external method object. """ if elem.attrib: for attr_name, attr_value in elem.attrib.iteritems(): if attr_name in self.__property_map: attr = self.__property_map[attr_name] method_prop_meta = self.__property_meta[attr] if method_prop_meta.inp_out == "Input" or (method_prop_meta.is_complex_type): continue self.set_attr(attr, str(attr_value)) elif attr_name in ExternalMethod._external_method_attrs: self.set_attr(ExternalMethod._external_method_attrs[attr_name], str(attr_value)) child_elems = elem.getchildren() if child_elems: for child_elem in child_elems: if not ET.iselement(child_elem): continue child_name = child_elem.tag if child_name in self.__property_map: child_name = self.__property_map[child_name] method_prop_meta = self.__property_meta[child_name] if method_prop_meta.inp_out == "Output" and (method_prop_meta.is_complex_type): child_obj = ucscoreutils.get_ucs_obj(method_prop_meta.field_type, child_elem) if child_obj is not None: self.set_attr(child_name, child_obj) # print child_method_obj.__dict__ child_obj.from_xml(child_elem)
def convert(arg): """ Convert an object to its xml representation """ if iselement(arg): return arg # the element if isinstance(arg, dict_accessor): try: return arg.text_ except: raise Exception("Cannot serialize %s, missing text_ attribute" % (arg, )) if isinstance(arg, dict): return arg # attributes of the element if isinstance(arg, unicode): return arg if isinstance(arg, decimal.Decimal): return unicode(arg) if arg is True: return 'true' if arg is False: return 'false' if isinstance(arg, float): return unicode(round(arg, 2)) # there's nothing less than cents anyway if isinstance(arg, (int, long)): return unicode(arg) if isinstance(arg, str): raise Exception("'%s' not unicode: can only accept unicode strings" % (arg, )) raise Exception("Cannot convert %s of type %s" % (arg, type(arg)))
def __init__(self, oElement, Checksum=None): """ Either pass a valid xml.etree.ElementTree.Element that represents the <Node> tag, or a string containing valid <Node> xml (none other). """ #------------------------------------------------------------------------------------------ if not ElementTree.iselement(oElement): raise TypeError("Invalid type '%s' passed to constructor." % type(XML)) #------------------------------------------------------------------------------------------ try: if oElement.tag != 'Struct': raise _SpecError("Invalid tag passed to constructor: <%s>" % oElement.tag) try: self.Name = oElement.attrib['Name'] except KeyError: self.Name = PrivateSpecName if not REGEX_SPEC_NAME.match(self.Name): raise _SpecError("Attribute 'Name' is not valid: %s" % self.Name) self.Prop = [] for element in oElement: self.Prop.append(self.MakeNode(element)) except _SpecError, e: if Debug: raise # Convert an internal _SpecError into a public SpecError e.InsertStack(oElement) raise SpecError(e)
def _getRate(self, rate_elem): """ Given a <rate> SubElement, return the frame rate """ if not ET.iselement(rate_elem) or rate_elem.tag != "rate": self.logger.warning( "FCP._getRate must be passed a <rate> sub element") return 0 # Final Cut Pro supports drop-frame formats as follows. If the <ntsc> tag is FALSE, it's # a whole-number frame rate. If it's TRUE, we have a decimal frame rate, so we apply a # multiplier to the timebase. This table lists the supported rates: # http://developer.apple.com/mac/library/documentation/AppleApplications/Reference/FinalCutPro_XML/FrameRate/FrameRate.html#//apple_ref/doc/uid/TP30001158-TPXREF103 # multiplier = 1.0 timebase = 0 for item in rate_elem: if item.tag == "ntsc": if item.text.upper() == "TRUE": multipier = 0.999 elif item.tag == "timebase": timebase = int(item.text) if timebase == 0: self.logger.error("timebase was not specified for rate in %s" % self.formatErrorElement(self.cur_elem)) return (multiplier * timebase)
def writeFile(self, clip_elem, file_info): """ Add a file to the specified clip. file_info must contain the following: id = used for linking of audio/video name or pathurl = file name or absolute path to the location duration = media length (sequence frames) """ if not ET.iselement(clip_elem) or clip_elem.tag != "clipitem": self.logger.error( "FCP.writeFile must be passed a valid <clipitem> element") return None file_elem = ET.SubElement(clip_elem, "file") if "id" in file_info: file_elem.set("id", file_info["id"]) for k, v in file_info.iteritems(): elem = ET.SubElement(file_elem, k) if k == "pathurl": if not os.path.isabs(str(v)): self.logger.warning( "FCP.writeFile must be given an absolute file path. Received: %s" % str(v)) filename = v.replace(" ", "%20") elem.text = "file://%s" % filename else: elem.text = str(v) return file_elem
def _getSampleCharacteristics(self, sample_elem): """ Given a <samplecharacteristics> SubElement, return dictionary with the following information (if appropriate): width = width of video height = height of video depth = audio bit depth samplerate = audio sample rate """ if not ET.iselement( sample_elem) or sample_elem.tag != "samplecharacteristics": self.logger.error( "FCP._getSampleCharacteristics must be given a <samplecharacteristics> item" ) return {} #TODO: there are lots of other tags in samplecharacteristics, like: # anamorphic, pixelaspectration,fielddominance,rate etc. Which ones # do we care about enough to return? sample_info = {} for item in sample_elem: if (item.tag == "width" or item.tag == "height" or item.tag == "depth" or item.tag == "samplerate"): sample_info[item.tag] = item.text return sample_info
def __format_element(elt_data): """ Private method which ensures that a XML portion to be parsed is of type xml.etree.ElementTree.Element. If elt_data is a string, then it is converted to an XML Element type. :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element """ if isinstance(elt_data, str): try: xelement = ET.fromstring(elt_data) except: raise NmapParserException("Error while trying " "to instanciate XML Element from " "string {0}".format(elt_data)) elif ET.iselement(elt_data): xelement = elt_data else: raise NmapParserException("Error while trying to parse supplied " "data: unsupported format") return xelement
def from_xml(self, elem, handle=None): """Method updates/fills the object from the xml representation of the external method object. """ self._handle = handle if elem.attrib: for attr_name, attr_value in ucscgenutils.iteritems(elem.attrib): if attr_name in self.__property_map: attr = self.__property_map[attr_name] method_prop_meta = self.__property_meta[attr] if method_prop_meta.inp_out == "Input" or ( method_prop_meta.is_complex_type): continue self.set_attr(attr, str(attr_value)) elif attr_name in ExternalMethod._external_method_attrs: self.set_attr( ExternalMethod._external_method_attrs[attr_name], str(attr_value)) child_elems = list(elem) if child_elems: for child_elem in child_elems: if not ET.iselement(child_elem): continue child_name = child_elem.tag if child_name in self.__property_map: child_name = self.__property_map[child_name] method_prop_meta = self.__property_meta[child_name] if method_prop_meta.inp_out == "Output" and \ (method_prop_meta.is_complex_type): child_obj = ucsccoreutils.get_ucsc_obj( method_prop_meta.field_type, child_elem) if child_obj is not None: self.set_attr(child_name, child_obj) child_obj.from_xml(child_elem, handle)
def get_author(self): author = self.xml_root.find("teiHeader/fileDesc/titleStmt/author") if not ET.iselement(author): return "" author_str = ET.tostring(author, encoding="gbk", method="text").strip() return author_str
def get_title(self): title = self.xml_root.find("teiHeader/fileDesc/titleStmt/title") if not ET.iselement(title): return "" title_str = ET.tostring(title, encoding="gbk", method="text").strip() return title_str
def __new__(cls, tag, thing=None, *args, **kwargs): if hasattr(tag, '__xml__'): return tag.__xml__() self = object.__new__(xml) if cElementTree.iselement(tag): self.__content = tag elif isinstance(tag, cElementTree.ElementTree): self.__content = tag.getroot() elif is_file(tag): self.__content = cElementTree.parse(tag).getroot() elif isinstance(tag, str) and len(tag) > 0 and tag[0] == '<': self.__content = cElementTree.fromstring(tag) else: if type(tag) != str: raise TypeError("Cannot convert %s object to xml" % str(type(tag))) self.__content = cElementTree.fromstring('<%s/>' % tag) if is_text(thing) or type(thing) == int: self.__content.text = text(thing) elif thing != None: self.append(xml(thing)) for subthing in args: self.append(xml(subthing)) for key, value in kwargs.items(): if key == '__class' or key == 'klass': self['class'] = value else: self[key] = value if '{' in self.__content.tag: self.__prefix = PREFIX_PAT.search(self.__content.tag).groups()[0] else: self.__prefix = '' return self
def get_date(self): date = self.xml_root.find("teiHeader/fileDesc/publicationStmt/date") if not ET.iselement(date): return "" date_str = ET.tostring(date, encoding="gbk", method="text").strip() return date_str
def import_epf(epf): """Import an EPF file. :param epf: Either a path to an EPF-file, a file-like object, or an instance of :class:`xml.etree.ElementTree.Element`. :returns: The Object Dictionary. :rtype: canopen.ObjectDictionary """ od = objectdictionary.ObjectDictionary() if etree.iselement(epf): tree = epf else: tree = etree.parse(epf).getroot() # Find and set default bitrate can_config = tree.find("Configuration/CANopen") if can_config is not None: bitrate = can_config.get("BitRate", "250") bitrate = bitrate.replace("U", "") od.bitrate = int(bitrate) * 1000 # Parse Object Dictionary for group_tree in tree.iterfind("Dictionary/Parameters/Group"): name = group_tree.get("SymbolName") parameters = group_tree.findall("Parameter") index = int(parameters[0].get("Index"), 0) if len(parameters) == 1: # Simple variable var = build_variable(parameters[0]) # Use top level index name instead var.name = name od.add_object(var) elif len(parameters) == 2 and parameters[1].get( "ObjectType") == "ARRAY": # Array arr = objectdictionary.Array(name, index) for par_tree in parameters: var = build_variable(par_tree) arr.add_member(var) description = group_tree.find("Description") if description is not None: arr.description = description.text od.add_object(arr) else: # Complex record record = objectdictionary.Record(name, index) for par_tree in parameters: var = build_variable(par_tree) record.add_member(var) description = group_tree.find("Description") if description is not None: record.description = description.text od.add_object(record) return od
def _set_elem(self, elem): """Set elementtree element. :raise TypeError: invalid element """ if not etree.iselement(elem): raise TypeError('xml: invalid etree element (%s).' % _etree_elem) self._etree_elem = elem
def path(self, element): path = [] current = element while ET.iselement(current) : path.append(current) current = self.parent_map[current] path.reverse() return path
def _parse(self, block): """:returns: a parse tree given a string.""" if ElementTree.iselement(block): xmlblock = block else: # NB: parse because raw XML might contain entities etc. xmlblock = ElementTree.fromstring(block) return alpinotree( xmlblock, self.functions, self.morphology, self.lemmas)
async def rep_body(msg) -> str: """ 根据用户发来的消息进行回复, 一期为echo server; 二期将做区分命令和普通消息的处理 :param msg: 明文消息体xml :return: 明文消息体xml """ # 根据接收到的xml消息,实例化成xml对象 xml_tree = Et.fromstring(msg) # 只处理发送消息的事件 # if xml.etree.ElementTree.iselement(xml_tree.find("Content")): if Et.iselement(xml_tree.find("Content")): # 获取用户发送的消息 content: str = xml_tree.find("Content").text else: # raise error logger.info("Only Support Message Event") return "Only Support Message Event" user_id: str = xml_tree.find("FromUserName").text # 用户id corp_id: str = xml_tree.find("ToUserName").text # 企业id create_time: str = xml_tree.find( "CreateTime").text # 时间戳(用request的时间戳即可 # 构建xml树 rep_data: Et.Element = Et.Element("xml") to_user_name: Et.Element = Et.SubElement(rep_data, "ToUserName") from_user_name: Et.Element = Et.SubElement(rep_data, "FromUserName") rep_create_time: Et.Element = Et.SubElement(rep_data, "CreateTime") message_type: Et.Element = Et.SubElement(rep_data, "MsgType") rep_content: Et.Element = Et.SubElement(rep_data, "Content") # 创建CDATA对象 to_user_name_cdata = CDATA(user_id) from_user_name_cdata = CDATA(corp_id) message_type_cdata = CDATA("text") rep_content_cdata = CDATA("📮 消息已转发至PaaS团队🍪") # 插曲~ 将用户发来的信息, 转发到 paas chat group user_info: dict = await WeUser().get_user_info(user_id=user_id) transmit_content = "📨 From: {0} {1} \n📒 Details: {2}".format( user_info["name"], user_info["position"], content) message_obj: MessageService = MessageService.get_message_obj() await message_obj.send_message_to_chat_group(paas_chat_id, transmit_content) # 为节点赋值 to_user_name.append(to_user_name_cdata) from_user_name.append(from_user_name_cdata) rep_create_time.text = create_time message_type.append(message_type_cdata) rep_content.append(rep_content_cdata) # 生成明文的xml回文 rep_xml_data: str = Et.tostring(rep_data, encoding="utf-8", method="xml").decode(encoding='utf-8') return rep_xml_data
def run( cls, fileIn, fileOut=None, tokenization="split-Charniak-Lease", entityOffsetKey="charOffset", includeNeg=False, stem=False, ): """Builds the master gazzeteer. fileIn: a string (ending with .xml or .xml.gz), an open input stream, an ElementTree or an Element fileOut: a string or None. If given, the resulting gazzetteer will be written out tokenization: name of the tokenization to be used Produces a dictionary with... """ print >>sys.stderr, "Building gazetteer" gztr = {} # key: token value: dictionary (key: className, value count) root = ETUtils.ETFromObj(fileIn) if not ET.iselement(root): assert isinstance(root, ET.ElementTree) root = root.getroot() sentences = [] for sNode in root.getiterator("sentence"): sentences.append(sNode) counter = ProgressCounter(len(sentences), "Build gazetteer") for sNode in sentences: counter.update(1, "Adding to gazetteer sentence " + sNode.get("id") + ", ") for tokenizationNode in sNode.getiterator("tokenization"): if tokenizationNode.get("tokenizer") == tokenization: break else: assert False, "Did not find %s tokenization" % tokenization tClasses = tokClasses(tokenizationNode, sNode, entityOffsetKey) assert len(tClasses) == len(tokenizationNode) for tokIdx, tokNode in enumerate(tokenizationNode): gsClass = tClasses[tokIdx] b, e = charOffStr2tuple(tokNode.get("charOffset")) tokNodeTxt = tokTxt(b, e, sNode, stem).lower() tokDict = gztr.setdefault(tokNodeTxt, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 # for multi-part texts, add collapsed and last token versions if tokNodeTxt.find("-") != -1: # collapsed text = tokNodeTxt.replace("-", "") if text != "": tokDict = gztr.setdefault(text, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 # last part text = tokNodeTxt.rsplit("-", 1)[-1] if text != "": tokDict = gztr.setdefault(text, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 if fileOut: Gazetteer.saveGztr(gztr, fileOut, includeNeg) return gztr
def _set_id(self, value = None): if etree.iselement(value): self.id = value.get("id") if not self.id: if self.title: self.id = "%s_%s" % (str(self.get_title()).replace(" ","").lower(), random_string(8)) else: self.id = random_string(8)
def run(cls, fileIn, fileOut=None, tokenization="split-Charniak-Lease", entityOffsetKey="charOffset", includeNeg=False, stem=False): """Builds the master gazzeteer. fileIn: a string (ending with .xml or .xml.gz), an open input stream, an ElementTree or an Element fileOut: a string or None. If given, the resulting gazzetteer will be written out tokenization: name of the tokenization to be used Produces a dictionary with... """ print >> sys.stderr, "Building gazetteer" gztr = {} #key: token value: dictionary (key: className, value count) root = ETUtils.ETFromObj(fileIn) if not ET.iselement(root): assert isinstance(root, ET.ElementTree) root = root.getroot() sentences = [] for sNode in root.getiterator("sentence"): sentences.append(sNode) counter = ProgressCounter(len(sentences), "Build gazetteer") for sNode in sentences: counter.update( 1, "Adding to gazetteer sentence " + sNode.get("id") + ", ") for tokenizationNode in sNode.getiterator("tokenization"): if tokenizationNode.get("tokenizer") == tokenization: break else: assert False, "Did not find %s tokenization" % tokenization tClasses = tokClasses(tokenizationNode, sNode, entityOffsetKey) assert len(tClasses) == len(tokenizationNode) for tokIdx, tokNode in enumerate(tokenizationNode): gsClass = tClasses[tokIdx] b, e = charOffStr2tuple(tokNode.get("charOffset")) tokNodeTxt = tokTxt(b, e, sNode, stem).lower() tokDict = gztr.setdefault(tokNodeTxt, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 # for multi-part texts, add collapsed and last token versions if tokNodeTxt.find("-") != -1: # collapsed text = tokNodeTxt.replace("-", "") if text != "": tokDict = gztr.setdefault(text, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 # last part text = tokNodeTxt.rsplit("-", 1)[-1] if text != "": tokDict = gztr.setdefault(text, {}) tokDict[gsClass] = tokDict.get(gsClass, 0) + 1 if fileOut: Gazetteer.saveGztr(gztr, fileOut, includeNeg) return gztr
def to_ele(x): """Convert XML to `~xml.etree.ElementTree.Element`. If passed an `~xml.etree.ElementTree.Element` simply returns that. :arg x: the XML document or element :type x: `string` or `~xml.etree.ElementTree.Element` :rtype: `~xml.etree.ElementTree.Element` """ return x if ET.iselement(x) else ET.fromstring(x)
def _word(self, block, orig=False): if ElementTree.iselement(block): xmlblock = block else: xmlblock = ElementTree.fromstring(block) sent = xmlblock.find('sentence').text.split(' ') if orig or self.punct != "remove": return sent return [word for word in sent if not ispunct(word, None)] # fixme: don't have tag
def get_repo_info(job_name): server = jenkins.Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) job_config = server.get_job_config(job_name) root = ET.fromstring(job_config.encode('utf8')) if ET.iselement( root.find( 'scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote' )): svn_url = root.find( 'scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote' ).text tag_dir = root.find( 'properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/' + 'hudson.scm.listtagsparameter.ListSubversionTagsParameterDefinition/tagsDir' ) if ET.iselement(tag_dir): tag_dir = tag_dir.text tags_filter = root.find( 'properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/' + 'hudson.scm.listtagsparameter.ListSubversionTagsParameterDefinition/tagsFilter' ) if ET.iselement(tags_filter): tags_filter = tags_filter.text return { 'svn_url': svn_url, 'tag_dir': tag_dir, 'tags_filter': tags_filter, 'job_type': 'svn' } elif ET.iselement( root.find( 'scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url' )): git_url = root.find( 'scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url' ).text return {'git_url': git_url, 'job_type': 'git'} else: return {'job_type': None}
def from_xml(self, elem, handle=None): """ This method is form objects out of xml element. This is called internally from ucsxmlcode.from_xml_str method. Example: xml = '<testLsA a="1" b="2" c="3" dn="org-root/" rn=""> <testLsB a="1" b="2" c="3" dn="org-root/" rn="" /></testLsA>'\n obj = xc.from_xml_str(xml)\n print type(obj)\n Outputs: <class 'ucsmsdk.ucsmo.GenericMo'> """ if elem is None: return None self._handle = handle self._class_id = elem.tag if elem.attrib: for name, value in ucsgenutils.iteritems(elem.attrib): self.__dict__[name] = value self.__properties[name] = str(value) if self.rn and self.dn: pass elif self.rn and not self.dn: if self.__parent_dn is not None and self.__parent_dn != "": self.dn = self.__parent_dn + '/' + self.rn self.__properties['dn'] = self.dn else: self.dn = self.rn self.__properties['dn'] = self.dn elif not self.rn and self.dn: self.rn = os.path.basename(self.dn) self.__properties['rn'] = self.rn # else: # raise ValueError("Both rn and dn does not present.") children = elem.getchildren() if children: for child in children: if not ET.iselement(child): continue class_id = ucsgenutils.word_u(child.tag) # child_obj = ucscoreutils.get_ucs_obj(class_id, child, self) pdn = None if 'dn' in dir(self): pdn = self.dn child_obj = GenericMo(class_id, parent_mo_or_dn=pdn) self.child_add(child_obj) child_obj.from_xml(child, handle)
def SetProperty(self, name, data): type = "string" property = self.element.find(name) if ET.iselement(self.definition) and ET.iselement(property): try: type = self.definition.find(name).text if not self.ApplyType(data, type) == self.GetProperty(name): self.SetChanged(True) except ValueError: log.debug(data + " is not " + type + ", skipped") return except AttributeError: log.debug("Index error. Element does not exist.") except PropertyNotFoundError: log.debug("Index error. Element does not exist.") if ET.iselement(property): property.text = unicode(data) else: newElement = ET.SubElement(self.element,name) newElement.text = unicode(data)
def test_etree(): global xml_string e = Message() e.from_string(xml_string) root = e.body.getroot() assert ET.iselement(root) p = root.findtext("{http://www.w3.org/1999/xhtml}p") assert p == "This is a test" print e.to_string()
def from_xml(self, elem, handle=None): """ This method is form objects out of xml element. This is called internally from imcxmlcode.from_xml_str method. Example: xml = '<testLsA a="1" b="2" c="3" dn="org-root/" rn=""> <testLsB a="1" b="2" c="3" dn="org-root/" rn="" /></testLsA>'\n obj = xc.from_xml_str(xml)\n print type(obj)\n Outputs: <class 'imcsdk.imcmo.GenericMo'> """ if elem is None: return None self._handle = handle self._class_id = elem.tag if elem.attrib: for name, value in list(elem.attrib.items()): self.__dict__[name] = value self.__properties[name] = str(value) if self.rn and self.dn: pass elif self.rn and not self.dn: if self.__parent_dn and self.__parent_dn != "": self.dn = self.__parent_dn + '/' + self.rn self.__properties['dn'] = self.dn else: self.dn = self.rn self.__properties['dn'] = self.dn elif not self.rn and self.dn: self.rn = os.path.basename(self.dn) self.__properties['rn'] = self.rn children = list(elem) if children: for child in children: if not ET.iselement(child): continue class_id = imcgenutils.word_u(child.tag) pdn = None if 'dn' in dir(self): pdn = self.dn child_obj = GenericMo(class_id, parent_mo_or_dn=pdn) self.child_add(child_obj) child_obj.from_xml(child, handle)
def concat(docs): """ Concatenate together the contents of multiple documents from a single corpus, using an appropriate concatenation function. This utility function is used by corpus readers when the user requests more than one document at a time. """ if len(docs) == 1: return docs[0] if len(docs) == 0: raise ValueError('concat() expects at least one object!') types = set(d.__class__ for d in docs) # If they're all strings, use string concatenation. if all(isinstance(doc, string_types) for doc in docs): return ''.join(docs) # If they're all corpus views, then use ConcatenatedCorpusView. for typ in types: if not issubclass(typ, (StreamBackedCorpusView, ConcatenatedCorpusView)): break else: return ConcatenatedCorpusView(docs) # If they're all lazy sequences, use a lazy concatenation for typ in types: if not issubclass(typ, AbstractLazySequence): break else: return LazyConcatenation(docs) # Otherwise, see what we can do: if len(types) == 1: typ = list(types)[0] if issubclass(typ, list): return reduce((lambda a, b: a + b), docs, []) if issubclass(typ, tuple): return reduce((lambda a, b: a + b), docs, ()) if ElementTree.iselement(typ): xmltree = ElementTree.Element('documents') for doc in docs: xmltree.append(doc) return xmltree # No method found! raise ValueError("Don't know how to concatenate types: %r" % types)
def _parse(self, block): """:returns: a parse tree given a string.""" if ElementTree.iselement(block): xmlblock = block else: # NB: parse because raw XML might contain entities etc. try: xmlblock = ElementTree.fromstring(block) except ElementTree.ParseError: print('Problem with:\n%s' % block.decode('utf8', errors='replace'), file=sys.stderr) raise return alpinotree( xmlblock, self.functions, self.morphology, self.lemmas)
def __call__(self, tag, *children, **attrib): elem = ET.Element(tag, attrib) for item in children: if isinstance(item, dict): elem.attrib.update(item) elif isinstance(item, basestring): if len(elem): elem[-1].tail = (elem[-1].tail or "") + item else: elem.text = (elem.text or "") + item elif ET.iselement(item): elem.append(item) else: raise TypeError("bad argument: %r" % item) return elem
def _getFormat(self, format_elem): """ Given a <format> SubElement, return a dictionary with relevant info. Note that format only has one possible child: samplecharacteristics. But <samplecharacteristics> tags can be embedded in <video> or <audio> tags as well. """ if not ET.iselement(format_elem) or format_elem.tag != "format": self.logger.warning("FCP._getFormat must be given a <format> item") return {} for item in format_elem: if item.tag == "samplecharacteristics": return self._getSampleCharacteristics(item) return {}