root / ncclient / glue.py @ 1d540e60
History | View | Annotate | Download (3 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 threading import Thread |
19 |
from Queue import Queue |
20 |
from threading import Lock |
21 |
from xml.etree import cElementTree as ET |
22 |
|
23 |
import logging |
24 |
logger = logging.getLogger('ncclient.glue')
|
25 |
|
26 |
def parse_root(raw): |
27 |
'''Parse the top-level element from a string representing an XML document.
|
28 |
|
29 |
Returns a `(tag, attributes)` tuple, where `tag` is a string representing
|
30 |
the qualified name of the root element and `attributes` is an
|
31 |
`{attribute: value}` dictionary.
|
32 |
'''
|
33 |
fp = StringIO(raw) |
34 |
for event, element in ET.iterparse(fp, events=('start',)): |
35 |
return (element.tag, element.attrib)
|
36 |
|
37 |
|
38 |
class Subject(Thread): |
39 |
|
40 |
'Meant for subclassing by transport.Session'
|
41 |
|
42 |
def __init__(self): |
43 |
"TODO: docstring"
|
44 |
Thread.__init__(self)
|
45 |
self._q = Queue()
|
46 |
self._listeners = set() # TODO(?) weakref |
47 |
self._lock = Lock()
|
48 |
|
49 |
def _dispatch_received(self, raw): |
50 |
"TODO: docstring"
|
51 |
root = parse_root(raw) |
52 |
with self._lock: |
53 |
listeners = list(self._listeners) |
54 |
for l in listeners: |
55 |
logger.debug('[dispatching] message to %s' % l)
|
56 |
l.callback(root, raw) |
57 |
|
58 |
def _dispatch_error(self, err): |
59 |
"TODO: docstring"
|
60 |
with self._lock: |
61 |
listeners = list(self._listeners) |
62 |
for l in listeners: |
63 |
logger.debug('[dispatching] error to %s' % l)
|
64 |
l.errback(err) |
65 |
|
66 |
def add_listener(self, listener): |
67 |
"TODO: docstring"
|
68 |
logger.debug('[installing listener] %r' % listener)
|
69 |
with self._lock: |
70 |
self._listeners.add(listener)
|
71 |
|
72 |
def remove_listener(self, listener): |
73 |
"TODO: docstring"
|
74 |
logger.debug('[discarding listener] %r' % listener)
|
75 |
with self._lock: |
76 |
self._listeners.discard(listener)
|
77 |
|
78 |
def get_listener_instance(self, cls): |
79 |
'''This is useful when we want to maintain one listener of a particular
|
80 |
type per subject i.e. a multiton.
|
81 |
'''
|
82 |
with self._lock: |
83 |
for listener in self._listeners: |
84 |
if isinstance(listener, cls): |
85 |
return listener
|
86 |
|
87 |
def send(self, message): |
88 |
"TODO: docstring"
|
89 |
logger.debug('[queueing] %s' % message)
|
90 |
self._q.put(message)
|
91 |
|
92 |
|
93 |
class Listener(object): |
94 |
|
95 |
"TODO: docstring"
|
96 |
|
97 |
def callback(self, root, raw): |
98 |
raise NotImplementedError |
99 |
|
100 |
def errback(self, err): |
101 |
raise NotImplementedError |