root / ncclient / glue.py @ bf31e33e
History | View | Annotate | Download (2.7 kB)
1 |
# Copyright 2009 Shikhar Bhushan
|
---|---|
2 |
#
|
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
|
6 |
#
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
#
|
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.
|
14 |
|
15 |
"TODO: docstring"
|
16 |
|
17 |
from cStringIO import StringIO |
18 |
from Queue import Queue |
19 |
from threading import Lock |
20 |
from xml.etree import cElementTree as ET |
21 |
|
22 |
|
23 |
def parse_root(raw): |
24 |
'''Parse the top-level element from a string representing an XML document.
|
25 |
|
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.
|
29 |
'''
|
30 |
fp = StringIO(raw) |
31 |
for event, element in ET.iterparse(fp, events=('start',)): |
32 |
return (element.tag, element.attrib)
|
33 |
|
34 |
|
35 |
class Subject(object): |
36 |
|
37 |
'Meant for subclassing by transport.Session'
|
38 |
|
39 |
def __init__(self): |
40 |
"TODO: docstring"
|
41 |
self._q = Queue()
|
42 |
self._listeners = set() |
43 |
self._lock = Lock()
|
44 |
|
45 |
def _dispatch_received(self, raw): |
46 |
"TODO: docstring"
|
47 |
root = parse_root(raw) |
48 |
with self._lock: |
49 |
listeners = list(self._listeners) |
50 |
for l in listeners: |
51 |
l.callback(root, raw) |
52 |
|
53 |
def _dispatch_error(self, err): |
54 |
"TODO: docstring"
|
55 |
with self._lock: |
56 |
listeners = list(self._listeners) |
57 |
for l in listeners: |
58 |
l.errback(err) |
59 |
|
60 |
def add_listener(self, listener): |
61 |
"TODO: docstring"
|
62 |
with self._lock: |
63 |
self._listeners.add(listener)
|
64 |
|
65 |
def remove_listener(self, listener): |
66 |
"TODO: docstring"
|
67 |
with self._lock: |
68 |
self._listeners.discard(listener)
|
69 |
|
70 |
def get_listener_instance(self, cls): |
71 |
'''This is useful when we want to maintain one listener of a particular
|
72 |
type per subject i.e. a multiton.
|
73 |
'''
|
74 |
with self._lock: |
75 |
for listener in self._listeners: |
76 |
if isinstance(listener, cls): |
77 |
return listener
|
78 |
|
79 |
def send(self, message): |
80 |
"TODO: docstring"
|
81 |
logger.debug('queueing:%s' % message)
|
82 |
self._q.put(message)
|
83 |
|
84 |
|
85 |
class Listener(object): |
86 |
|
87 |
"TODO: docstring"
|
88 |
|
89 |
def callback(self, root, raw): |
90 |
raise NotImplementedError |
91 |
|
92 |
def errback(self, err): |
93 |
raise NotImplementedError |