def ToXml(self):
return u'<gd:email address=%s />' % saxutils.quoteattr(self)
python类quoteattr()的实例源码
def ToXml(self):
return (u'<gd:im protocol=%s address=%s />' %
(saxutils.quoteattr(self.protocol),
saxutils.quoteattr(self.address)))
def _PropertiesToXml(self, properties):
""" Returns a list of the XML representations of each of the given
properties. Ignores properties that don't exist in this entity.
Arg:
properties: string or list of strings
Returns:
list of strings
"""
xml_properties = []
for propname in properties:
if not self.has_key(propname):
continue
propname_xml = saxutils.quoteattr(propname)
values = self[propname]
if isinstance(values, list) and not values:
continue
if not isinstance(values, list):
values = [values]
proptype = datastore_types.PropertyTypeName(values[0])
proptype_xml = saxutils.quoteattr(proptype)
escaped_values = self._XmlEscapeValues(propname)
open_tag = u'<property name=%s type=%s>' % (propname_xml, proptype_xml)
close_tag = u'</property>'
xml_properties += [open_tag + val + close_tag for val in escaped_values]
return xml_properties
def _quoteattr(self, attr):
"""Escape an XML attribute. Value can be unicode."""
attr = xml_safe(attr)
return saxutils.quoteattr(attr)
def getTag(self):
"Return an XML tag representation"
attrs = []
for k, v in self.__dict__.items():
if k not in ['timeModified']:
if v:
attrs.append('%s=%s' % (k, quoteattr(str(v))))
return '<font ' + ' '.join(attrs) + '/>'
def write_dict(self, dictionary):
"""Write one record for the specified entity."""
if self.xml_style == self.ELEMENT_CENTRIC:
self.output_stream.write('%s<%s>\n' % (self.indent, self.entity_node))
self.write_iterable_as_elements(dictionary)
self.output_stream.write('%s</%s>\n' % (self.indent, self.entity_node))
else:
self.output_stream.write('%s<%s ' % (self.indent, self.entity_node))
for (name, value) in dictionary.iteritems():
self.output_stream.write('%s=%s ' % (name, saxutils.quoteattr(value)))
self.output_stream.write('/>\n')
def ToXml(self):
return u'<category term="%s" label=%s />' % (Category.TERM,
saxutils.quoteattr(self))
def ToXml(self):
return u'<link href=%s />' % saxutils.quoteattr(self)
def ToXml(self):
return u'<gd:email address=%s />' % saxutils.quoteattr(self)
def ToXml(self):
return (u'<gd:im protocol=%s address=%s />' %
(saxutils.quoteattr(self.protocol),
saxutils.quoteattr(self.address)))
def _PropertiesToXml(self, properties):
""" Returns a list of the XML representations of each of the given
properties. Ignores properties that don't exist in this entity.
Arg:
properties: string or list of strings
Returns:
list of strings
"""
xml_properties = []
for propname in properties:
if not self.has_key(propname):
continue
propname_xml = saxutils.quoteattr(propname)
values = self[propname]
if isinstance(values, list) and not values:
continue
if not isinstance(values, list):
values = [values]
proptype = datastore_types.PropertyTypeName(values[0])
proptype_xml = saxutils.quoteattr(proptype)
escaped_values = self._XmlEscapeValues(propname)
open_tag = u'<property name=%s type=%s>' % (propname_xml, proptype_xml)
close_tag = u'</property>'
xml_properties += [open_tag + val + close_tag for val in escaped_values]
return xml_properties
def _parseXml(self, settingXmlFile):
try:
root = ET.parse(settingXmlFile).getroot()
except: # ParseError
from xml.sax.saxutils import quoteattr
with open(settingXmlFile, 'r') as f:
content = f.read()
content = re.sub(r'(?<==)(["\'])(.*?)\1', lambda x: quoteattr(x.group(2)), content)
root = ET.fromstring(content)
return root
def startElementNS(self, name, qname, attrs):
self._write(unicode('<' + self.makeName(name)))
for pair in self._undeclared_ns_maps:
self._write(unicode(' xmlns:%s="%s"' % pair))
self._undeclared_ns_maps = []
for (name, value) in attrs.items():
self._write(unicode(' %s=%s' % (self.makeName(name), quoteattr(value))))
self._write(unicode('>'))
# general purpose xml writer, does a bunch of useful stuff above & beyond XmlGenerator
def startElementNS(self, name, qname, attrs):
self._write(unicode('<' + self.makeName(name)))
for pair in self._undeclared_ns_maps:
self._write(unicode(' xmlns:%s="%s"' % pair))
self._undeclared_ns_maps = []
for (name, value) in attrs.items():
self._write(unicode(' %s=%s' % (self.makeName(name), quoteattr(value))))
self._write(unicode('>'))
# general purpose xml writer, does a bunch of useful stuff above & beyond XmlGenerator
def write_dict(self, dictionary):
"""Write one record for the specified entity."""
if self.xml_style == self.ELEMENT_CENTRIC:
self.output_stream.write('%s<%s>\n' % (self.indent, self.entity_node))
self.write_iterable_as_elements(dictionary)
self.output_stream.write('%s</%s>\n' % (self.indent, self.entity_node))
else:
self.output_stream.write('%s<%s ' % (self.indent, self.entity_node))
for (name, value) in dictionary.iteritems():
self.output_stream.write('%s=%s ' % (name, saxutils.quoteattr(value)))
self.output_stream.write('/>\n')
def ToXml(self):
return u'<category term="%s" label=%s />' % (Category.TERM,
saxutils.quoteattr(self))