def write(self, filename):
xml = ET.Element("phonebooks")
for book in self.phonebookList:
xml.append(book.getXML())
tree = ET.ElementTree(xml)
if False:
tree.write(filename, encoding="iso-8859-1", xml_declaration=True)
else:
rough_string = ET.tostring(tree.getroot(), encoding="iso-8859-1", method="xml")
reparsed = parseString(rough_string)
pretty = reparsed.toprettyxml(indent=" ", encoding="iso-8859-1").decode("iso-8859-1")
with open(filename, 'w', encoding="iso-8859-1") as outfile:
outfile.write(pretty)
# sid: Login session ID
# phonebookid: 0 for main phone book
# 1 for next phone book in list, etc...
python类ElementTree()的实例源码
def writeXML(dataObj, fname, dtdPath):
if dataObj.predSev == 0:
sev = "ABSENT"
elif dataObj.predSev == 1:
sev = "MILD"
elif dataObj.predSev == 2:
sev = "MODERATE"
elif dataObj.predSev == 3:
sev = "SEVERE"
root = ET.Element("RDoC")
ET.SubElement(root, "TEXT").text = dataObj.text.content
tags = ET.SubElement(root, "TAGS")
pos_val = ET.SubElement(tags, "POSITIVE_VALENCE")
pos_val.set('score', sev)
pos_val.set('annotated_by', dataObj.Nannotators)
tree = ET.ElementTree(root)
tree.write(fname)
testcase_utils_class.py 文件源码
项目:warriorframework
作者: warriorframework
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def print_output(self):
""" Prints the dump of the xml object to the file specified.
This function can be used for debugging purpose and
its called at the end of the functional calls.
:Arguments:
resultfile = Result File
:Returns:
None
"""
try:
import Framework.Utils.config_Utils as config_Utils
resultfile = config_Utils.resultfile
tree = ET.ElementTree(self.root)
tree.write(resultfile)
except UnicodeDecodeError as e:
print_exception(e)
except Exception as err:
print_exception(err)
def getChildTextbyParentTag (datafile, pnode, cnode):
"""
Seraches XML file for the first parent. Finds the child node and returns its text
datafile = xml file searched
pnode = parent node
cnode = child node
"""
value = False
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.text
return value
else:
# print_info("could not find cnode under the given pnode")
return value
else:
# print_info("could not find pnode in the provided file")
return value
def getChildAttributebyParentTag (datafile, pnode, cnode, cattrib):
"""Find the attribute in child node by traversing through the parent node
in the given file
datafile = xml file searched
pnode = parent node
cnode = child node
cattrob = child node attrib
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.get(cattrib)
return value
else:
# print_info("could not find cnode under the given pnode")
return False
else:
# print_info("could not find pnode in the provided file")
return False
def getChildTextbyOtherChild (datafile, pnode, cnode, cvalue, rnode):
"""
Searches XML file for the parent node. Finds the 1st child node and checks its value
if value is a match, then search for second child and return its value
datafile = xml file searched
pnode = parent node
cnode = child node
cvalue = child node value
rnode = reference node or False if doesn't exist
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
rnodev = False
for node in root.findall(pnode):
value = node.find(cnode).text
if value == cvalue:
# print_debug("-D- rnode: '%s'" % rnode)
if node.find(rnode) is not None:
rnodev = node.find(rnode).text
break
return rnodev
def verifyParentandChildrenMatch (datafile, pnode, cnode, cvalue, rnode, rvalue):
"""
Searches XML file for the parent node. Finds the 1st child node and checks its value
if value is a match, then search for second child and check if its value matches
datafile = xml file searched
pnode = parent node
cnode = child node
cvalue = child node value
rnode = reference node
rvalue = refernce node value
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
status = False
for node in root.findall(pnode):
value = node.find(cnode).text
if value == cvalue:
if node.find(rnode) is not None:
cnodev = node.find(rnode).text
# print_debug("-D- cnodev: '%s', rvalue : '%s'" % (cnodev, rvalue))
if cnodev == rvalue:
# print_debug("-D- BREAK END METHOD verifyParentandChildrenMatch_Status '%s'" % status)
return True
return status
def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value):
"""
This method takes an xml document as input and finds all the sub elements (parent/children)
containing specified tag and an attribute with the specified value.
Returns a list of matching elements.
Arguments:
datafile = input xml file to be parsed.
tag = tag value of the sub-element(parent/child) to be searched for.
attrib = attribute name for the sub-element with above given tag should have.
value = attribute value that the sub-element with above given tag, attribute should have.
"""
element_list = []
root = ElementTree.parse(datafile).getroot()
for element in root.iterfind(".//%s[@%s='%s']" % (tag, attrib, value)):
element_list.append(element)
return element_list
def getElementWithTagAttribValueMatch(start, tag, attrib, value):
"""
When start is an xml datafile, it finds the root and first element with:
tag, attrib, value.
Or when it's an xml element, it finds the first child element with:
tag, attrib, value.
If there is not a match, it returns False.
"""
node = False
if isinstance(start, (file, str)):
# check if file exist here
if file_Utils.fileExists(start):
node = ElementTree.parse(start).getroot()
else:
print_warning('The file={0} is not found.'.format(start))
elif isinstance(start, ElementTree.Element):
node = start
if node is not False and node is not None:
elementName = ".//%s[@%s='%s']" % (tag, attrib, value)
element = node.find(elementName)
else:
element = node
return element
def getElementListWithSpecificXpath(datafile, xpath):
"""
This method takes an xml document as input and finds all the sub elements (parent/children)
containing specified xpath
Returns a list of matching elements.
Arguments:
parent = parent element
xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
element_list = []
root = ElementTree.parse(datafile).getroot()
for element in root.iterfind(xpath):
element_list.append(element)
return element_list
def getConfigElementTextWithSpecificXpath(datafile, xpath):
"""
This method takes an xml document as input and finds the first sub element (parent/children)
containing specified xpath which should be a filepath to a netconf config file
Returns the element text attribute
Arguments:
parent = parent element
xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
root = ElementTree.parse(datafile).getroot()
elem1 = root.find(xpath).text
elem2_root = ElementTree.parse(elem1)
elem2 = elem2_root.find('config')
elem2_string = ElementTree.tostring(elem2)
return elem2_string
def del_tags_from_xml(xml, tag_list=[]):
"""
It deletes the tags either by their names or xpath
Arguments:
1.xml: It takes xml file path or xml string as input
2.tag_list: It contains list of tags which needs to be removed
Returns:
It returns xml string
"""
if os.path.exists(xml):
tree = ElementTree.parse(xml)
root = tree.getroot()
else:
root = ElementTree.fromstring(xml)
for tag in tag_list:
if 'xpath=' in tag:
tag = tag.strip('xpath=')
req_tags = getChildElementsListWithSpecificXpath(root, tag)
else:
req_tags = getChildElementsListWithSpecificXpath(root, ".//{0}".format(tag))
recursive_delete_among_children(root, req_tags)
xml_string = ElementTree.tostring(root, encoding='utf-8', method='xml')
return xml_string
def __init__(self):
self.nodetree = [] #tree that will contain the elements for the .mm output file
self.nodetree.append("") #initialise tree
self.previous_level = 0 #initialise depth control
# def open(self, inputfile):
# """ get opml data and load into ElementTree tree """
# if inputfile.endswith('.opml'):
# try:
# # self.tree = ET.parse(inputfile)
# return self.tree
# except:
# print "Cannot open file "+inputfile+'\n' \
# "File may not exist or file may not be a valid xml file\n" \
# "\nUSAGE\n"
# closedown()
def _request(self, action='GET', url='/', data=None, query_params=None):
if data is None:
data = {}
if query_params is None:
query_params = {}
query_params['version'] = 1
query_params['type'] = 'xml'
query_params['key'] = self.options['auth_token']
r = requests.request(action, self.api_endpoint + url, params=query_params)
#data=json.dumps(data))
r.raise_for_status() # if the request fails for any reason, throw an error.
# TODO: check if the response is an error using
tree = ElementTree.ElementTree(ElementTree.fromstring(r.content))
root = tree.getroot()
if root.find('reply').find('code').text != '300':
raise Exception('An error occurred: {0}, {1}'.format(root.find('reply').find('detail').text, root.find('reply').find('code').text))
return root
def _request(self, action='GET', url='/', data=None, query_params=None):
if data is None:
data = {}
if query_params is None:
query_params = {}
else:
query_params['api_key'] = self.options.get('auth_token')
r = requests.request(action, self.api_endpoint + url, params=query_params)
tree = ElementTree.ElementTree(ElementTree.fromstring(r.content))
root = tree.getroot()
if root.tag == 'error':
raise Exception('An error occurred: {0}'.format(root.text))
else:
r.raise_for_status()
return root
def getConfigFromFile(self):
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
if not os.path.exists(self.fileName) :
print "file ", self.fileName, " not exists"
return None
try:
self.docTree = ET.ElementTree(file=self.fileName)
except Exception,e:
print "%s is NOT well-formed : %s "%(self.fileName,e)
return None
self.smtpServer = self.getSectiontText("smtpServer")
self.smtpPort = self.getSectiontInt("smtpPort")
self.sender = self.getSectiontText("sender").strip()
self.senderPasswd = self.getSectiontText("senderPasswd")
self.rcvType = self.getTextAttribute("receivers","type")
self.getReceivers("receivers/user")
return None
def get_status_xml(self, command):
"""Get status XML via HTTP and return it as XML ElementTree."""
# Get XML structure via HTTP get
res = requests.get("http://{host}{command}".format(
host=self._host, command=command), timeout=self.timeout)
# Continue with XML processing only if HTTP status code = 200
if res.status_code == 200:
try:
# Return XML ElementTree
return ET.fromstring(res.text)
except ET.ParseError:
_LOGGER.error(
"Host %s returned malformed XML for: %s",
self._host, command)
raise ValueError
else:
_LOGGER.error((
"Host %s returned HTTP status code %s "
"when trying to receive data"), self._host, res.status_code)
raise ValueError
def probe_metric(service_url, metric):
'''
Query the service at the given URL for the given metric value.
Assumptions are made about the name of the method and output parameters
which are only valid for the WanCommonInterfaceConfig service.
'''
envelope = E(QName(ns['s'], 'Envelope'), {QName(ns['s'], 'encodingStyle'): 'http://schemas.xmlsoap.org/soap/encoding/'})
body = sE(envelope, QName(ns['s'], 'Body'))
method = sE(body, QName(ns['i'], 'Get{}'.format(metric)))
request_tree = ET(envelope)
with io.BytesIO() as out:
out.write(b'<?xml version="1.0"?>')
request_tree.write(out, encoding='utf-8')
out.write(b'\r\n') # or else my Belkin F5D8236-4 never responds...
req = urllib.request.Request(service_url, out.getvalue())
req.add_header('Content-Type', 'text/xml')
req.add_header('SOAPAction', '"{}#{}"'.format(ns['i'], 'Get{}'.format(metric)))
with urllib.request.urlopen(req) as result:
result_tree = ElementTree.parse(result)
return int(result_tree.findtext('.//New{}'.format(metric), namespaces=ns))
def file_init():
"""
>>> import io
>>> stringfile = io.BytesIO(SAMPLE_XML.encode("utf-8"))
>>> tree = ET.ElementTree(file=stringfile)
>>> tree.find("tag").tag
'tag'
>>> tree.find("section/tag").tag
'tag'
>>> tree = ET.ElementTree(file=SIMPLE_XMLFILE)
>>> tree.find("element").tag
'element'
>>> tree.find("element/../empty-element").tag
'empty-element'
"""
def parseliteral():
"""
>>> element = ET.XML("<html><body>text</body></html>")
>>> ET.ElementTree(element).write(sys.stdout, encoding='unicode')
<html><body>text</body></html>
>>> element = ET.fromstring("<html><body>text</body></html>")
>>> ET.ElementTree(element).write(sys.stdout, encoding='unicode')
<html><body>text</body></html>
>>> sequence = ["<html><body>", "text</bo", "dy></html>"]
>>> element = ET.fromstringlist(sequence)
>>> ET.tostring(element)
b'<html><body>text</body></html>'
>>> b"".join(ET.tostringlist(element))
b'<html><body>text</body></html>'
>>> ET.tostring(element, "ascii")
b"<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>"
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
>>> len(ids)
0
>>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
>>> len(ids)
1
>>> ids["body"].tag
'body'
"""
def __init__(self, xml=None, rootnode=None):
if xml is None and rootnode is None:
xml = default_xml
if rootnode is None:
if sys.platform.startswith('win'):
enc = 'ISO-8859-1'
else:
enc = 'UTF-8'
self.dom = ElementTree.fromstring(xml, ElementTree.XMLParser(encoding=enc))
else:
self.dom = rootnode
# determine OME namespaces
self.ns = get_namespaces(self.dom)
if __name__ == '__main__':
if self.ns['ome'] is None:
raise Exception("Error: String not in OME-XML format")
# generate a uuid if there is none
# < OME UUID = "urn:uuid:ef8af211-b6c1-44d4-97de-daca46f16346"
omeElem = self.dom
if not omeElem.get('UUID'):
omeElem.set('UUID', 'urn:uuid:'+str(uuid.uuid4()))
self.uuidStr = omeElem.get('UUID')
def __str__(self):
#
# need to register the ome namespace because BioFormats expects
# that namespace to be the default or to be explicitly named "ome"
#
for ns_key in ["ome"]:
ns = self.ns.get(ns_key) or NS_DEFAULT.format(ns_key=ns_key)
# ElementTree.register_namespace(ns_key, ns)
ElementTree.register_namespace('', ns)
# ElementTree.register_namespace("om", NS_ORIGINAL_METADATA)
result = StringIO()
ElementTree.ElementTree(self.root_node).write(result,
encoding=uenc,
method="xml",
xml_declaration = True
# default_namespace = 'http://www.openmicroscopy.org/Schemas/ome/2013-06'
)
return result.getvalue()
def append_channel(self, index, name):
# add channel
new_channel = OMEXML.Channel(
ElementTree.SubElement(self.node, qn(self.ns['ome'], "Channel")))
new_channel.SamplesPerPixel = 1
new_channel.ID = "Channel:0:"+str(index)
new_channel.Name = name
# add a bunch of planes with "TheC"=str(index)
for t in range(self.get_SizeT()):
for z in range(self.get_SizeZ()):
new_plane = OMEXML.Plane(
ElementTree.SubElement(self.node, qn(self.ns['ome'], "Plane")))
new_plane.TheC = str(index)
new_plane.TheZ = str(z)
new_plane.TheT = str(t)
# update SizeC
self.set_SizeC(self.get_SizeC() + 1)
# can be done as a single step just prior to final output
designSpaceDocument.py 文件源码
项目:designSpaceRoboFontExtension
作者: LettError
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def write(self, pretty=True):
if self.documentObject.axes:
self.root.append(ET.Element("axes"))
for axisObject in self.documentObject.axes:
self._addAxis(axisObject)
if self.documentObject.rules:
self.root.append(ET.Element("rules"))
for ruleObject in self.documentObject.rules:
self._addRule(ruleObject)
if self.documentObject.sources:
self.root.append(ET.Element("sources"))
for sourceObject in self.documentObject.sources:
self._addSource(sourceObject)
if self.documentObject.instances:
self.root.append(ET.Element("instances"))
for instanceObject in self.documentObject.instances:
self._addInstance(instanceObject)
if pretty:
_indent(self.root, whitespace=self._whiteSpace)
tree = ET.ElementTree(self.root)
tree.write(self.path, encoding="utf-8", method='xml', xml_declaration=True)
def create_job_doc(self, object_name=None, operation=None,
contentType='CSV', concurrency=None, external_id_name=None):
root = ET.Element("jobInfo")
root.set("xmlns", self.jobNS)
op = ET.SubElement(root, "operation")
op.text = operation
obj = ET.SubElement(root, "object")
obj.text = object_name
if external_id_name:
ext = ET.SubElement(root, 'externalIdFieldName')
ext.text = external_id_name
if concurrency:
con = ET.SubElement(root, "concurrencyMode")
con.text = concurrency
ct = ET.SubElement(root, "contentType")
ct.text = contentType
buf = BytesIO()
tree = ET.ElementTree(root)
tree.write(buf, encoding="UTF-8")
return buf.getvalue().decode("utf-8")
def run(self, xml, **kwargs):
"""Method takes either an etree.ElementTree or raw XML text
as the first argument.
Args:
xml(etree.ElementTree or text
"""
self.output = self.__graph__()
if isinstance(xml, str):
try:
self.source = etree.XML(xml)
except ValueError:
try:
self.source = etree.XML(xml.encode())
except:
raise ValueError("Cannot run error {}".format(sys.exc_info()[0]))
else:
self.source = xml
super(XMLProcessor, self).run(**kwargs)
def saveSettingsAndTasks(self):
'''Dump current sorting and filtering choices to disk for reloading'''
if not self.settingsFile:
logger.warning('no settings file found, nothing will be saved')
return
logger.info('saving task panel\'s settings to disk: %s' % self.settingsFile)
settingsToBeSaved = {}
settingsToBeSaved['hideFinished'] = str(self.hideButton.isChecked())
settingsToBeSaved['sortState'] = str(self.sortButton.isChecked())
root = ET.Element('ToDoPanel')
settingsEle = ET.SubElement(root, 'Settings')
for k, v in settingsToBeSaved.iteritems():
settingEle = ET.SubElement(settingsEle, k)
settingEle.text = v
for task in self.taskStore.tasks:
taskDict = task.__dict__
tasksEle = ET.SubElement(root, 'Task')
for k, v in taskDict.iteritems():
taskEle = ET.SubElement(tasksEle, k)
taskEle.text = str(v)
tree = ET.ElementTree(root)
tree.write(self.settingsFile)
def __init__(self, filename):
self.fd = gzip.open(filename)
self.xml = ElementTree()
self.xml.parse(self.fd)
self.prjs = {}
self.tasks = {}
self.resources = {}
self.vacations = []
self._process_projects()
self._process_resources()
self._process_tasks()
self._process_bookings()
self._process_vacations()
# __init__()
def __init__(self, dsxml, filename=None):
"""
Constructor. Default is to create datasource from xml.
"""
self._filename = filename
self._datasourceXML = dsxml
self._datasourceTree = ET.ElementTree(self._datasourceXML)
self._name = self._datasourceXML.get('name') or self._datasourceXML.get(
'formatted-name') # TDS files don't have a name attribute
self._version = self._datasourceXML.get('version')
self._caption = self._datasourceXML.get('caption', '')
self._connection_parser = ConnectionParser(
self._datasourceXML, version=self._version)
self._connections = self._connection_parser.get_connections()
self._fields = None
def __init__(self, filepath):
self._filepath = os.path.abspath(filepath)
tree = ElementTree().parse(self._filepath)
self._signature = tree.find("SignatureRegister").text.upper()
project = tree.find("Project")
nifpga = project.find("CompilationResultsTree") \
.find("CompilationResults") \
.find("NiFpga")
self._base_address_on_device = int(nifpga.find("BaseAddressOnDevice").text)
self._registers = {}
for reg_xml in tree.find("VI").find("RegisterList"):
reg = Register(reg_xml)
if reg.datatype is not None:
assert reg.name not in self._registers, \
"One or more registers have the same name '%s', this is not supported" % reg.name
self._registers[reg.name] = reg
self._fifos = {}
for channel_xml in nifpga.find("DmaChannelAllocationList"):
fifo = Fifo(channel_xml)
self._fifos[fifo.name] = fifo