1 # Copyright 2009 Shikhar Bhushan
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from cStringIO import StringIO
18 from Queue import Queue
19 from threading import Lock
20 from xml.etree import cElementTree as ET
24 '''Parse the top-level element from a string representing an XML document.
26 Returns a `(tag, attributes)` tuple, where `tag` is a string representing
27 the qualified name of the root element and `attributes` is an
28 `{attribute: value}` dictionary.
31 for event, element in ET.iterparse(fp, events=('start',)):
32 return (element.tag, element.attrib)
39 self._listeners = set([])
43 def _dispatch_received(self, raw):
45 root = parse_root(raw)
47 listeners = list(self._listeners)
51 def _dispatch_error(self, err):
54 listeners = list(self._listeners)
58 def add_listener(self, listener):
61 self._listeners.add(listener)
63 def remove_listener(self, listener):
66 self._listeners.discard(listener)
68 def send(self, message):
70 logger.debug('queueing:%s' % message)
71 self._outQ.put(message)
78 def callback(self, root, raw):
79 raise NotImplementedError
81 def errback(self, err):
82 raise NotImplementedError