本文整理汇总了Python中xml.dom.minidom.Element.toprettyxml方法的典型用法代码示例。如果您正苦于以下问题:Python Element.toprettyxml方法的具体用法?Python Element.toprettyxml怎么用?Python Element.toprettyxml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.dom.minidom.Element
的用法示例。
在下文中一共展示了Element.toprettyxml方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from xml.dom.minidom import Element [as 别名]
# 或者: from xml.dom.minidom.Element import toprettyxml [as 别名]
def get(self, *args):
self.response.headers['Content-Type'] = "application/rss+xml"
items = self.getItems(*args)
itemElems = []
for item in items:
itemElem = Element("item")
self._appendElementWithTextNode(itemElem, "title", item.title)
self._appendElementWithTextNode(itemElem, "link", item.link)
self._appendElementWithTextNode(itemElem, "description", item.description)
self._appendElementWithTextNode(itemElem, "guid", item.link)
if item.subPlaceFeedLink:
self._appendElementWithTextNode(itemElem, "sharkattackdata:subPlaceFeedLink", item.subPlaceFeedLink)
if item.attackFeedLink:
self._appendElementWithTextNode(itemElem, "sharkattackdata:attackFeedLink", item.attackFeedLink)
itemElems.append(itemElem)
# Need to make channel element after the generator returned by getItems has been iterated.
channelElem = Element("channel")
self._appendElementWithTextNode(channelElem, "title", self._feedTitle)
self._appendElementWithTextNode(channelElem, "link", self._feedLink)
self._appendElementWithTextNode(channelElem, "description", self._feedDescription)
for itemElem in itemElems:
channelElem.appendChild(itemElem)
responseText = """<?xml version="1.0"?>
<rss version="2.0" xmlns:sharkattackdata="http://sharkattackdata.com/rss/modules/1.0/">
%s
</rss>
""" % (channelElem.toprettyxml())
self.response.out.write(responseText)
示例2: getXML
# 需要导入模块: from xml.dom.minidom import Element [as 别名]
# 或者: from xml.dom.minidom.Element import toprettyxml [as 别名]
def getXML(self):
"""
Converts TFC implementation (dict) into a XML string representation.
The method reflects this class implementation - dictionary containing
list of mappings while each mapping (i.e. entry, see addMapping
method) is a dictionary of key, value pairs.
"""
def _getElementForMappingEntry(entry, mappingStyle):
e = Element(mappingStyle)
for k, v in entry.items():
# ignore empty, None or compiled regexp items into output
if not v or (k == "path-match-expr"):
continue
e.setAttribute(k, str(v))
return e
root = Element("storage-mapping") # root element name
for mappingStyle, mappings in self.items():
for mapping in mappings:
mapElem = _getElementForMappingEntry(mapping, mappingStyle)
root.appendChild(mapElem)
return root.toprettyxml()
示例3: daemonize
# 需要导入模块: from xml.dom.minidom import Element [as 别名]
# 或者: from xml.dom.minidom.Element import toprettyxml [as 别名]
def daemonize(stdout= '/dev/null', stderr = None, stdin= '/dev/null', \
workdir= None, startmsg = 'started with pid %s', \
keepParent = False ):
'''
This forks the current process into a daemon.
The stdin, stdout, and stderr arguments are file names that
will be opened and be used to replace the standard file descriptors
in sys.stdin, sys.stdout, and sys.stderr.
These arguments are optional and default to /dev/null.
Note that stderr is opened unbuffered, so
if it shares a file with stdout then interleaved output
may not appear in the order that you expect.
'''
# Do first fork.
try:
pid = os.fork()
if pid > 0:
if not keepParent:
os._exit(0) # Exit first parent.
return pid
except OSError as e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
print("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
# Decouple from parent environment.
os.chdir("/")
os.umask(UMASK)
os.setsid()
# Do second fork.
try:
pid = os.fork()
if pid > 0: os._exit(0) # Exit second parent.
except OSError as e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
print("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
# Open file descriptors and print start message
if not stderr: stderr = stdout
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
pid = str(os.getpid())
sys.stderr.write("\n%s\n" % startmsg % pid)
sys.stderr.flush()
if workdir:
#file(pidfile,'w+').write("%s\n" % pid)
#Since the current working directory may be a mounted filesystem, we
#avoid the issue of not being able to unmount the filesystem at
#shutdown time by changing it to the root directory.
os.chdir(workdir)
#We probably don't want the file mode creation mask inherited from
#the parent, so we give the child complete control over permissions.
os.umask(UMASK)
daemon = Element("Daemon")
processId = Element("ProcessID")
processId.setAttribute("Value", str(os.getpid()))
daemon.appendChild(processId)
parentProcessId = Element("ParentProcessID")
parentProcessId.setAttribute("Value", str(os.getppid()))
daemon.appendChild(parentProcessId)
processGroupId = Element("ProcessGroupID")
processGroupId.setAttribute("Value", str(os.getpgrp()))
daemon.appendChild(processGroupId)
userId = Element("UserID")
userId.setAttribute("Value", str(os.getuid()))
daemon.appendChild(userId)
effectiveUserId = Element("EffectiveUserID")
effectiveUserId.setAttribute("Value", str(os.geteuid()))
daemon.appendChild(effectiveUserId)
groupId = Element("GroupID")
groupId.setAttribute("Value", str(os.getgid()))
daemon.appendChild(groupId)
effectiveGroupId = Element("EffectiveGroupID")
effectiveGroupId.setAttribute("Value", str(os.getegid()))
daemon.appendChild(effectiveGroupId)
dom = Document()
dom.appendChild(daemon)
props = open("Daemon.xml", "w")
props.write(daemon.toprettyxml())
props.close()
# Redirect standard file descriptors.
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
return 0