def mapPrefix(self, node, attribute): skip = False if attribute.name == 'xmlns': if len(attribute.value): node.expns = unicode(attribute.value) skip = True elif attribute.prefix == 'xmlns': prefix = attribute.name node.nsprefixes[prefix] = unicode(attribute.value) skip = True return skip
def startElement(self, name, attrs): top = self.top() node = Element(unicode(name), parent=top) for a in attrs.getNames(): n = unicode(a) v = unicode(attrs.getValue(a)) attribute = Attribute(n, v) if self.mapPrefix(node, attribute): continue node.append(attribute) node.charbuffer = [] top.append(node) self.push(node)
def str(self, indent=0): """ Get a string representation of this XML fragment. @param indent: The indent to be used in formatting the output. @type indent: int @return: A I{pretty} string. @rtype: basestring """ tab = '%*s' % (indent * 3, '') result = [] result.append('%s<%s' % (tab, self.qname())) result.append(self.nsdeclarations()) for a in [unicode(a) for a in self.attributes]: result.append(' %s' % a) if self.isempty(): result.append('/>') return ''.join(result) result.append('>') if self.hasText(): result.append(self.text.escape()) for c in self.children: result.append('\n') result.append(c.str(indent + 1)) if len(self.children): result.append('\n%s' % tab) result.append('</%s>' % self.qname()) result = ''.join(result) return result
def str(self, indent=0): """ Get a string representation of this XML fragment. @param indent: The indent to be used in formatting the output. @type indent: int @return: A I{pretty} string. @rtype: basestring """ tab = '%*s' % (indent * 3, '') result = [] result.append('%s<%s' % (tab, self.qname())) result.append(self.nsdeclarations()) for a in [unicode(a) for a in self.attributes]: result.append(' %s' % a) if self.isempty(): result.append('/>') return ''.join(result) result.append('>') if self.hasText(): result.append(self.text.escape()) for c in self.children: result.append('\n') result.append(c.str(indent+1)) if len(self.children): result.append('\n%s' % tab) result.append('</%s>' % self.qname()) result = ''.join(result) return result
def endElement(self, name): name = unicode(name) current = self.top() if len(current.charbuffer): current.text = Text(u''.join(current.charbuffer)) del current.charbuffer if len(current): current.trim() currentqname = current.qname() if name == currentqname: self.pop() else: raise Exception('malformed document')
def tostr(object, encoding=None): """ get a unicode safe string representation of an object """ if isinstance(object, basestring): if encoding is None: return object else: return object.encode(encoding) if isinstance(object, tuple): s = ['('] for item in object: if isinstance(item, basestring): s.append(item) else: s.append(tostr(item)) s.append(', ') s.append(')') return ''.join(s) if isinstance(object, list): s = ['['] for item in object: if isinstance(item, basestring): s.append(item) else: s.append(tostr(item)) s.append(', ') s.append(']') return ''.join(s) if isinstance(object, dict): s = ['{'] for item in object.items(): if isinstance(item[0], basestring): s.append(item[0]) else: s.append(tostr(item[0])) s.append(' = ') if isinstance(item[1], basestring): s.append(item[1]) else: s.append(tostr(item[1])) s.append(', ') s.append('}') return ''.join(s) try: return unicode(object) except: return str(object)
def plain(self): """ Get a string representation of this XML fragment. @return: A I{plain} string. @rtype: basestring """ result = [] result.append('<%s' % self.qname()) result.append(self.nsdeclarations()) for a in [unicode(a) for a in self.attributes]: result.append(' %s' % a) if self.isempty(): result.append('/>') return ''.join(result) result.append('>') if self.hasText(): result.append(self.text.escape()) for c in self.children: result.append(c.plain()) result.append('</%s>' % self.qname()) result = ''.join(result) return result
def smart_str(s, encoding='utf-8', errors='strict'): """ Returns a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects. from django """ if not isinstance(s, basestring): try: return str(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return ' '.join(smart_str(arg, encoding, errors) for arg in s) return unicode(s).encode(encoding, errors) elif isinstance(s, unicode): return s.encode(encoding, errors) elif s and encoding != 'utf-8': return s.decode('utf-8', errors).encode(encoding, errors) else: return s
def characters(self, content): text = unicode(content) node = self.top() node.charbuffer.append(text)
def __str__(self): return unicode(self).encode('utf-8')
def __str__(self): return unicode(self)
def __str__(self): """ get an xml string representation """ return unicode(self).encode('utf-8')