annotate sat_tmp/twisted/domish.py @ 71:a564fc84d5d0

twisted: added Twisted module for Python 3.8 compatibility: Current Twisted version (19.10.0) is not Python 3.8 compatible, so a twisted directory has been added to fix `twisted.words.xish.domish` and be able to use it with Python 3.8. To be removed once Python 3.8 compatible version of Twisted is published.
author Goffi <goffi@goffi.org>
date Mon, 18 Nov 2019 20:48:45 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 # -*- test-case-name: twisted.words.test.test_domish -*-
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # Copyright (c) Twisted Matrix Laboratories.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # See LICENSE for details.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 DOM-like XML processing support.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This module provides support for parsing XML into DOM-like object structures
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 and serializing such structures to an XML string representation, optimized
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 for use in streaming XML applications.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
12
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 from __future__ import absolute_import, division
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 from zope.interface import implementer, Interface, Attribute
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 from twisted.python.compat import (_PY3, StringType, _coercedUnicode,
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 iteritems, itervalues, unicode)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 def _splitPrefix(name):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 """ Internal method for splitting a prefixed Element name into its
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 respective parts """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 ntok = name.split(":", 1)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 if len(ntok) == 2:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 return ntok
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 return (None, ntok[0])
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 # Global map of prefixes that always get injected
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 # into the serializers prefix map (note, that doesn't
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 # mean they're always _USED_)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 G_PREFIXES = { "http://www.w3.org/XML/1998/namespace":"xml" }
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 class _ListSerializer:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 """ Internal class which serializes an Element tree into a buffer """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def __init__(self, prefixes=None, prefixesInScope=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.writelist = []
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.prefixes = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if prefixes:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.prefixes.update(prefixes)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.prefixes.update(G_PREFIXES)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.prefixStack = [G_PREFIXES.values()] + (prefixesInScope or [])
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.prefixCounter = 0
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def getValue(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 return u"".join(self.writelist)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def getPrefix(self, uri):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 if uri not in self.prefixes:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.prefixes[uri] = "xn%d" % (self.prefixCounter)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.prefixCounter = self.prefixCounter + 1
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return self.prefixes[uri]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def prefixInScope(self, prefix):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 stack = self.prefixStack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 for i in range(-1, (len(self.prefixStack)+1) * -1, -1):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if prefix in stack[i]:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return True
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return False
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def serialize(self, elem, closeElement=1, defaultUri=''):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 # Optimization shortcuts
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 write = self.writelist.append
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 # Shortcut, check to see if elem is actually a chunk o' serialized XML
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if isinstance(elem, SerializedXML):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 write(elem)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 return
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 # Shortcut, check to see if elem is actually a string (aka Cdata)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if isinstance(elem, StringType):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 write(escapeToXml(elem))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 # Further optimizations
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 name = elem.name
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 uri = elem.uri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 defaultUri, currentDefaultUri = elem.defaultUri, defaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 for p, u in iteritems(elem.localPrefixes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.prefixes[u] = p
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.prefixStack.append(list(elem.localPrefixes.keys()))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 # Inherit the default namespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if defaultUri is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 defaultUri = currentDefaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 if uri is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 uri = defaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 prefix = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 if uri != defaultUri or uri in self.prefixes:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 prefix = self.getPrefix(uri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 inScope = self.prefixInScope(prefix)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 # Create the starttag
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 if not prefix:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 write("<%s" % (name))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 write("<%s:%s" % (prefix, name))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if not inScope:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 write(" xmlns:%s='%s'" % (prefix, uri))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.prefixStack[-1].append(prefix)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 inScope = True
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 if defaultUri != currentDefaultUri and \
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 (uri != defaultUri or not prefix or not inScope):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 write(" xmlns='%s'" % (defaultUri))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 for p, u in iteritems(elem.localPrefixes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 write(" xmlns:%s='%s'" % (p, u))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 # Serialize attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 for k,v in elem.attributes.items():
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 # If the attribute name is a tuple, it's a qualified attribute
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if isinstance(k, tuple):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 attr_uri, attr_name = k
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 attr_prefix = self.getPrefix(attr_uri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if not self.prefixInScope(attr_prefix):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 write(" xmlns:%s='%s'" % (attr_prefix, attr_uri))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 self.prefixStack[-1].append(attr_prefix)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 write(" %s:%s='%s'" % (attr_prefix, attr_name,
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 escapeToXml(v, 1)))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 write((" %s='%s'" % ( k, escapeToXml(v, 1))))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 # Shortcut out if this is only going to return
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 # the element (i.e. no children)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 if closeElement == 0:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 write(">")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # Serialize children
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if len(elem.children) > 0:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 write(">")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 for c in elem.children:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.serialize(c, defaultUri=defaultUri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 # Add closing tag
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if not prefix:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 write("</%s>" % (name))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 write("</%s:%s>" % (prefix, name))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 write("/>")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 self.prefixStack.pop()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 SerializerClass = _ListSerializer
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def escapeToXml(text, isattrib = 0):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 """ Escape text to proper XML form, per section 2.3 in the XML specification.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 @type text: C{str}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 @param text: Text to escape
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 @type isattrib: C{bool}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 @param isattrib: Triggers escaping of characters necessary for use as
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 attribute values
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 text = text.replace("&", "&amp;")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 text = text.replace("<", "&lt;")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 text = text.replace(">", "&gt;")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if isattrib == 1:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 text = text.replace("'", "&apos;")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 text = text.replace("\"", "&quot;")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 return text
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 def unescapeFromXml(text):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 text = text.replace("&lt;", "<")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 text = text.replace("&gt;", ">")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 text = text.replace("&apos;", "'")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 text = text.replace("&quot;", "\"")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 text = text.replace("&amp;", "&")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 return text
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 def generateOnlyInterface(list, int):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 """ Filters items in a list by class
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 for n in list:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if int.providedBy(n):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 yield n
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
187
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 def generateElementsQNamed(list, name, uri):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """ Filters Element items in a list with matching name and URI. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 for n in list:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 if IElement.providedBy(n) and n.name == name and n.uri == uri:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 yield n
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def generateElementsNamed(list, name):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 """ Filters Element items in a list with matching name, regardless of URI.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 for n in list:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 if IElement.providedBy(n) and n.name == name:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 yield n
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 class SerializedXML(unicode):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 """ Marker class for pre-serialized XML in the DOM. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 pass
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 class Namespace:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 """ Convenience object for tracking namespace declarations. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 def __init__(self, uri):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 self._uri = uri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 def __getattr__(self, n):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 return (self._uri, n)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 def __getitem__(self, n):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 return (self._uri, n)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 class IElement(Interface):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 Interface to XML element nodes.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 See L{Element} for a detailed example of its general use.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 Warning: this Interface is not yet complete!
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 uri = Attribute(""" Element's namespace URI """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 name = Attribute(""" Element's local name """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 defaultUri = Attribute(""" Default namespace URI of child elements """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 attributes = Attribute(""" Dictionary of element attributes """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 children = Attribute(""" List of child nodes """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 parent = Attribute(""" Reference to element's parent element """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 localPrefixes = Attribute(""" Dictionary of local prefixes """)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def toXml(prefixes=None, closeElement=1, defaultUri='',
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 prefixesInScope=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 """ Serializes object to a (partial) XML document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
236
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 @param prefixes: dictionary that maps namespace URIs to suggested
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 prefix names.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 @type prefixes: L{dict}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 @param closeElement: flag that determines whether to include the
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 closing tag of the element in the serialized string. A value of
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 C{0} only generates the element's start tag. A value of C{1} yields
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 a complete serialization.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 @type closeElement: L{int}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
246
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @param defaultUri: Initial default namespace URI. This is most useful
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 for partial rendering, where the logical parent element (of which
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 the starttag was already serialized) declares a default namespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 that should be inherited.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 @type defaultUri: L{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
252
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 @param prefixesInScope: list of prefixes that are assumed to be
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 declared by ancestors.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 @type prefixesInScope: C{list}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
256
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 @return: (partial) serialized XML
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 @rtype: C{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 def addElement(name, defaultUri=None, content=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 Create an element and add as child.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
264
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 The new element is added to this element as a child, and will have
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 this element as its parent.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 @param name: element name. This can be either a L{unicode} object that
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 contains the local name, or a tuple of (uri, local_name) for a
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 fully qualified name. In the former case, the namespace URI is
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 inherited from this element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 @type name: L{unicode} or L{tuple} of (L{unicode}, L{unicode})
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 @param defaultUri: default namespace URI for child elements. If
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 L{None}, this is inherited from this element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 @type defaultUri: L{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
277
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 @param content: text contained by the new element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 @type content: L{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 @return: the created element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 @rtype: object providing L{IElement}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
284
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 def addChild(node):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 Adds a node as child of this element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
288
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 The C{node} will be added to the list of childs of this element, and
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 will have this element set as its parent when C{node} provides
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 L{IElement}. If C{node} is a L{unicode} and the current last child is
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 character data (L{unicode}), the text from C{node} is appended to the
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 existing last child.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
294
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 @param node: the child node.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 @type node: L{unicode} or object implementing L{IElement}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
298
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 def addContent(text):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 Adds character data to this element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
302
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 If the current last child of this element is a string, the text will
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 be appended to that string. Otherwise, the text will be added as a new
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 child.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
306
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 @param text: The character data to be added to this element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 @type text: L{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
311
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 @implementer(IElement)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 class Element(object):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 """ Represents an XML element node.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 An Element contains a series of attributes (name/value pairs), content
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 (character data), and other child Element objects. When building a document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 with markup (such as HTML or XML), use this object as the starting point.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
319
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 Element objects fully support XML Namespaces. The fully qualified name of
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 the XML Element it represents is stored in the C{uri} and C{name}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 attributes, where C{uri} holds the namespace URI. There is also a default
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 namespace, for child elements. This is stored in the C{defaultUri}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 attribute. Note that C{''} means the empty namespace.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
325
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 Serialization of Elements through C{toXml()} will use these attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 for generating proper serialized XML. When both C{uri} and C{defaultUri}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 are not None in the Element and all of its descendents, serialization
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 proceeds as expected:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
330
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 >>> from twisted.words.xish import domish
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 >>> root = domish.Element(('myns', 'root'))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 >>> root.addElement('child', content='test')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 <twisted.words.xish.domish.Element object at 0x83002ac>
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 >>> root.toXml()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 u"<root xmlns='myns'><child>test</child></root>"
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
337
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 For partial serialization, needed for streaming XML, a special value for
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 namespace URIs can be used: L{None}.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
340
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 Using L{None} as the value for C{uri} means: this element is in whatever
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 namespace inherited by the closest logical ancestor when the complete XML
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 document has been serialized. The serialized start tag will have a
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 non-prefixed name, and no xmlns declaration will be generated.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
345
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 Similarly, L{None} for C{defaultUri} means: the default namespace for my
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 child elements is inherited from the logical ancestors of this element,
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 when the complete XML document has been serialized.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
349
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 To illustrate, an example from a Jabber stream. Assume the start tag of the
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 root element of the stream has already been serialized, along with several
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 complete child elements, and sent off, looking like this::
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
353
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 <stream:stream xmlns:stream='http://etherx.jabber.org/streams'
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 xmlns='jabber:client' to='example.com'>
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 ...
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
357
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 Now suppose we want to send a complete element represented by an
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 object C{message} created like:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
360
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 >>> message = domish.Element((None, 'message'))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 >>> message['to'] = 'user@example.com'
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 >>> message.addElement('body', content='Hi!')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 <twisted.words.xish.domish.Element object at 0x8276e8c>
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 >>> message.toXml()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 u"<message to='user@example.com'><body>Hi!</body></message>"
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
367
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 As, you can see, this XML snippet has no xmlns declaration. When sent
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 off, it inherits the C{jabber:client} namespace from the root element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 Note that this renders the same as using C{''} instead of L{None}:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
371
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 >>> presence = domish.Element(('', 'presence'))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 >>> presence.toXml()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 u"<presence/>"
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
375
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 However, if this object has a parent defined, the difference becomes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 clear:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
378
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 >>> child = message.addElement(('http://example.com/', 'envelope'))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 >>> child.addChild(presence)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 <twisted.words.xish.domish.Element object at 0x8276fac>
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 >>> message.toXml()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 u"<message to='user@example.com'><body>Hi!</body><envelope xmlns='http://example.com/'><presence xmlns=''/></envelope></message>"
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
384
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 As, you can see, the <presence/> element is now in the empty namespace, not
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 in the default namespace of the parent or the streams'.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
387
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 @type uri: C{unicode} or None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 @ivar uri: URI of this Element's name
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
390
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 @type name: C{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 @ivar name: Name of this Element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
393
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 @type defaultUri: C{unicode} or None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 @ivar defaultUri: URI this Element exists within
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
396
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 @type children: C{list}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 @ivar children: List of child Elements and content
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
399
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 @type parent: L{Element}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 @ivar parent: Reference to the parent Element, if any.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
402
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 @type attributes: L{dict}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 @ivar attributes: Dictionary of attributes associated with this Element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
405
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 @type localPrefixes: L{dict}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 @ivar localPrefixes: Dictionary of namespace declarations on this
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 element. The key is the prefix to bind the
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 namespace uri to.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
411
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 _idCounter = 0
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
413
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 def __init__(self, qname, defaultUri=None, attribs=None,
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 localPrefixes=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 @param qname: Tuple of (uri, name)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 @param defaultUri: The default URI of the element; defaults to the URI
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 specified in C{qname}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 @param attribs: Dictionary of attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 @param localPrefixes: Dictionary of namespace declarations on this
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 element. The key is the prefix to bind the
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 namespace uri to.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 self.localPrefixes = localPrefixes or {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 self.uri, self.name = qname
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 if defaultUri is None and \
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 self.uri not in itervalues(self.localPrefixes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 self.defaultUri = self.uri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 self.defaultUri = defaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 self.attributes = attribs or {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 self.children = []
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 self.parent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
435
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 def __getattr__(self, key):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 # Check child list for first Element with a name matching the key
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 for n in self.children:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 if IElement.providedBy(n) and n.name == key:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 return n
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
441
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 # Tweak the behaviour so that it's more friendly about not
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 # finding elements -- we need to document this somewhere :)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if key.startswith('_'):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 raise AttributeError(key)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 return None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
448
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 def __getitem__(self, key):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 return self.attributes[self._dqa(key)]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
451
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 def __delitem__(self, key):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 del self.attributes[self._dqa(key)];
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
454
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 def __setitem__(self, key, value):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 self.attributes[self._dqa(key)] = value
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
457
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 def __unicode__(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 Retrieve the first CData (content) node
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 for n in self.children:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 if isinstance(n, StringType):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 return n
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 return u""
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
466
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 def __bytes__(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 Retrieve the first character data node as UTF-8 bytes.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 return unicode(self).encode('utf-8')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
472
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 if _PY3:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 __str__ = __unicode__
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 __str__ = __bytes__
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
477
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 def _dqa(self, attr):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 """ Dequalify an attribute key as needed """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 if isinstance(attr, tuple) and not attr[0]:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 return attr[1]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 return attr
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
484
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 def getAttribute(self, attribname, default = None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 """ Retrieve the value of attribname, if it exists """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 return self.attributes.get(attribname, default)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
488
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 def hasAttribute(self, attrib):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 """ Determine if the specified attribute exists """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 return self._dqa(attrib) in self.attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
492
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 def compareAttribute(self, attrib, value):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 """ Safely compare the value of an attribute against a provided value.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
495
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 L{None}-safe.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 return self.attributes.get(self._dqa(attrib), None) == value
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
499
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 def swapAttributeValues(self, left, right):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 """ Swap the values of two attribute. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 d = self.attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 l = d[left]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
504 d[left] = d[right]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 d[right] = l
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
506
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 def addChild(self, node):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 """ Add a child to this Element. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 if IElement.providedBy(node):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 node.parent = self
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
511 self.children.append(node)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 return node
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
513
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 def addContent(self, text):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 """ Add some text data to this Element. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 text = _coercedUnicode(text)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 c = self.children
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 if len(c) > 0 and isinstance(c[-1], unicode):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 c[-1] = c[-1] + text
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 c.append(text)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
522 return c[-1]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
523
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 def addElement(self, name, defaultUri = None, content = None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 if isinstance(name, tuple):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 if defaultUri is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 defaultUri = name[0]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 child = Element(name, defaultUri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
530 if defaultUri is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
531 defaultUri = self.defaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
532 child = Element((defaultUri, name), defaultUri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
533
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 self.addChild(child)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
535
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
536 if content:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
537 child.addContent(content)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
538
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
539 return child
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
540
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
541 def addRawXml(self, rawxmlstring):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
542 """ Add a pre-serialized chunk o' XML as a child of this Element. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 self.children.append(SerializedXML(rawxmlstring))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
544
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 def addUniqueId(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 """ Add a unique (across a given Python session) id attribute to this
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
547 Element.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 self.attributes["id"] = "H_%d" % Element._idCounter
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 Element._idCounter = Element._idCounter + 1
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
551
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
552
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 def elements(self, uri=None, name=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
554 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
555 Iterate across all children of this Element that are Elements.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
556
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 Returns a generator over the child elements. If both the C{uri} and
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 C{name} parameters are set, the returned generator will only yield
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 on elements matching the qualified name.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
560
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
561 @param uri: Optional element URI.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
562 @type uri: C{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 @param name: Optional element name.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 @type name: C{unicode}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
565 @return: Iterator that yields objects implementing L{IElement}.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 if name is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 return generateOnlyInterface(self.children, IElement)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
570 return generateElementsQNamed(self.children, name, uri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
571
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
572
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
573 def toXml(self, prefixes=None, closeElement=1, defaultUri='',
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
574 prefixesInScope=None):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
575 """ Serialize this Element and all children to a string. """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 s = SerializerClass(prefixes=prefixes, prefixesInScope=prefixesInScope)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
577 s.serialize(self, closeElement=closeElement, defaultUri=defaultUri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
578 return s.getValue()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
579
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
580 def firstChildElement(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
581 for c in self.children:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 if IElement.providedBy(c):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
583 return c
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 return None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
585
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
586
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 class ParserError(Exception):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
588 """ Exception thrown when a parsing error occurs """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
589 pass
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
590
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
591 def elementStream():
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 """ Preferred method to construct an ElementStream
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
593
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
594 Uses Expat-based stream if available, and falls back to Sux if necessary.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 """
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
596 try:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
597 es = ExpatElementStream()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
598 return es
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
599 except ImportError:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
600 if SuxElementStream is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
601 raise Exception("No parsers available :(")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
602 es = SuxElementStream()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
603 return es
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
604
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
605 try:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 from twisted.web import sux
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
607 except:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
608 SuxElementStream = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
609 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
610 class SuxElementStream(sux.XMLParser):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
611 def __init__(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
612 self.connectionMade()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
613 self.DocumentStartEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
614 self.ElementEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
615 self.DocumentEndEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
616 self.currElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
617 self.rootElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
618 self.documentStarted = False
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
619 self.defaultNsStack = []
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
620 self.prefixStack = []
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
621
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
622 def parse(self, buffer):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
623 try:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
624 self.dataReceived(buffer)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
625 except sux.ParseError as e:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
626 raise ParserError(str(e))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
627
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
628
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
629 def findUri(self, prefix):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
630 # Walk prefix stack backwards, looking for the uri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 # matching the specified prefix
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
632 stack = self.prefixStack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
633 for i in range(-1, (len(self.prefixStack)+1) * -1, -1):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
634 if prefix in stack[i]:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 return stack[i][prefix]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 return None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
637
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
638 def gotTagStart(self, name, attributes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 defaultUri = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 localPrefixes = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 attribs = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 uri = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
643
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 # Pass 1 - Identify namespace decls
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 for k, v in list(attributes.items()):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 if k.startswith("xmlns"):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 x, p = _splitPrefix(k)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
648 if (x is None): # I.e. default declaration
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
649 defaultUri = v
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
651 localPrefixes[p] = v
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
652 del attributes[k]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
653
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 # Push namespace decls onto prefix stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
655 self.prefixStack.append(localPrefixes)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
656
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
657 # Determine default namespace for this element; if there
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
658 # is one
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
659 if defaultUri is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
660 if len(self.defaultNsStack) > 0:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 defaultUri = self.defaultNsStack[-1]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 defaultUri = ''
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
664
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
665 # Fix up name
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
666 prefix, name = _splitPrefix(name)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
667 if prefix is None: # This element is in the default namespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
668 uri = defaultUri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
669 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
670 # Find the URI for the prefix
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
671 uri = self.findUri(prefix)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
672
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
673 # Pass 2 - Fix up and escape attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
674 for k, v in attributes.items():
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
675 p, n = _splitPrefix(k)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
676 if p is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
677 attribs[n] = v
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
678 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
679 attribs[(self.findUri(p)), n] = unescapeFromXml(v)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
680
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
681 # Construct the actual Element object
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
682 e = Element((uri, name), defaultUri, attribs, localPrefixes)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
683
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
684 # Save current default namespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
685 self.defaultNsStack.append(defaultUri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
686
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
687 # Document already started
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
688 if self.documentStarted:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
689 # Starting a new packet
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
690 if self.currElem is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
691 self.currElem = e
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
692 # Adding to existing element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
693 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
694 self.currElem = self.currElem.addChild(e)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
695 # New document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
696 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
697 self.rootElem = e
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
698 self.documentStarted = True
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
699 self.DocumentStartEvent(e)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
700
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
701 def gotText(self, data):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
702 if self.currElem != None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
703 if isinstance(data, bytes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
704 data = data.decode('ascii')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
705 self.currElem.addContent(data)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
706
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
707 def gotCData(self, data):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
708 if self.currElem != None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
709 if isinstance(data, bytes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
710 data = data.decode('ascii')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
711 self.currElem.addContent(data)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
712
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
713 def gotComment(self, data):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
714 # Ignore comments for the moment
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
715 pass
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
716
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
717 entities = { "amp" : "&",
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
718 "lt" : "<",
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
719 "gt" : ">",
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
720 "apos": "'",
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
721 "quot": "\"" }
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
722
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
723 def gotEntityReference(self, entityRef):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
724 # If this is an entity we know about, add it as content
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
725 # to the current element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
726 if entityRef in SuxElementStream.entities:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
727 data = SuxElementStream.entities[entityRef]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
728 if isinstance(data, bytes):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
729 data = data.decode('ascii')
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
730 self.currElem.addContent(data)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
731
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
732 def gotTagEnd(self, name):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
733 # Ensure the document hasn't already ended
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
734 if self.rootElem is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
735 # XXX: Write more legible explanation
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
736 raise ParserError("Element closed after end of document.")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
737
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
738 # Fix up name
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
739 prefix, name = _splitPrefix(name)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
740 if prefix is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
741 uri = self.defaultNsStack[-1]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
742 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
743 uri = self.findUri(prefix)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
744
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
745 # End of document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
746 if self.currElem is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
747 # Ensure element name and uri matches
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
748 if self.rootElem.name != name or self.rootElem.uri != uri:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
749 raise ParserError("Mismatched root elements")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
750 self.DocumentEndEvent()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
751 self.rootElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
752
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
753 # Other elements
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
754 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
755 # Ensure the tag being closed matches the name of the current
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
756 # element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
757 if self.currElem.name != name or self.currElem.uri != uri:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
758 # XXX: Write more legible explanation
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
759 raise ParserError("Malformed element close")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
760
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
761 # Pop prefix and default NS stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
762 self.prefixStack.pop()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
763 self.defaultNsStack.pop()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
764
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
765 # Check for parent null parent of current elem;
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
766 # that's the top of the stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
767 if self.currElem.parent is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
768 self.currElem.parent = self.rootElem
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
769 self.ElementEvent(self.currElem)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
770 self.currElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
771
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
772 # Anything else is just some element wrapping up
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
773 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
774 self.currElem = self.currElem.parent
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
775
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
776
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
777 class ExpatElementStream:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
778 def __init__(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
779 import pyexpat
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
780 self.DocumentStartEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
781 self.ElementEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
782 self.DocumentEndEvent = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
783 self.error = pyexpat.error
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
784 self.parser = pyexpat.ParserCreate("UTF-8", " ")
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
785 self.parser.StartElementHandler = self._onStartElement
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
786 self.parser.EndElementHandler = self._onEndElement
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
787 self.parser.CharacterDataHandler = self._onCdata
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
788 self.parser.StartNamespaceDeclHandler = self._onStartNamespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
789 self.parser.EndNamespaceDeclHandler = self._onEndNamespace
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
790 self.currElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
791 self.defaultNsStack = ['']
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
792 self.documentStarted = 0
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
793 self.localPrefixes = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
794
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
795 def parse(self, buffer):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
796 try:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
797 self.parser.Parse(buffer)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
798 except self.error as e:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
799 raise ParserError(str(e))
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
800
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
801 def _onStartElement(self, name, attrs):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
802 # Generate a qname tuple from the provided name. See
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
803 # http://docs.python.org/library/pyexpat.html#xml.parsers.expat.ParserCreate
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
804 # for an explanation of the formatting of name.
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
805 qname = name.rsplit(" ", 1)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
806 if len(qname) == 1:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
807 qname = ('', name)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
808
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
809 # Process attributes
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
810 new_attrs = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
811 to_delete = []
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
812 for k, v in attrs.items():
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
813 if " " in k:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
814 aqname = k.rsplit(" ", 1)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
815 new_attrs[(aqname[0], aqname[1])] = v
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
816 to_delete.append(k)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
817
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
818 attrs.update(new_attrs)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
819
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
820 for k in to_delete:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
821 del attrs[k]
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
822
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
823 # Construct the new element
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
824 e = Element(qname, self.defaultNsStack[-1], attrs, self.localPrefixes)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
825 self.localPrefixes = {}
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
826
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
827 # Document already started
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
828 if self.documentStarted == 1:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
829 if self.currElem != None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
830 self.currElem.children.append(e)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
831 e.parent = self.currElem
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
832 self.currElem = e
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
833
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
834 # New document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
835 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
836 self.documentStarted = 1
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
837 self.DocumentStartEvent(e)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
838
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
839 def _onEndElement(self, _):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
840 # Check for null current elem; end of doc
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
841 if self.currElem is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
842 self.DocumentEndEvent()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
843
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
844 # Check for parent that is None; that's
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
845 # the top of the stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
846 elif self.currElem.parent is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
847 self.ElementEvent(self.currElem)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
848 self.currElem = None
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
849
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
850 # Anything else is just some element in the current
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
851 # packet wrapping up
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
852 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
853 self.currElem = self.currElem.parent
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
854
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
855 def _onCdata(self, data):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
856 if self.currElem != None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
857 self.currElem.addContent(data)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
858
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
859 def _onStartNamespace(self, prefix, uri):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
860 # If this is the default namespace, put
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
861 # it on the stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
862 if prefix is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
863 self.defaultNsStack.append(uri)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
864 else:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
865 self.localPrefixes[prefix] = uri
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
866
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
867 def _onEndNamespace(self, prefix):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
868 # Remove last element on the stack
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
869 if prefix is None:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
870 self.defaultNsStack.pop()
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
871
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
872 ## class FileParser(ElementStream):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
873 ## def __init__(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
874 ## ElementStream.__init__(self)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
875 ## self.DocumentStartEvent = self.docStart
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
876 ## self.ElementEvent = self.elem
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
877 ## self.DocumentEndEvent = self.docEnd
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
878 ## self.done = 0
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
879
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
880 ## def docStart(self, elem):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
881 ## self.document = elem
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
882
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
883 ## def elem(self, elem):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
884 ## self.document.addChild(elem)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
885
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
886 ## def docEnd(self):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
887 ## self.done = 1
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
888
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
889 ## def parse(self, filename):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
890 ## with open(filename) as f:
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
891 ## for l in f.readlines():
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
892 ## self.parser.Parse(l)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
893 ## assert self.done == 1
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
894 ## return self.document
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
895
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
896 ## def parseFile(filename):
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
897 ## return FileParser().parse(filename)
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
898
a564fc84d5d0 twisted: added Twisted module for Python 3.8 compatibility:
Goffi <goffi@goffi.org>
parents:
diff changeset
899