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