Statistics
| Branch: | Tag: | Revision:

root / ncclient / content / parsers.py @ f5c75f88

History | View | Annotate | Download (2.5 kB)

1 1dd43130 Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 1dd43130 Shikhar Bhushan
#
3 1dd43130 Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 1dd43130 Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 1dd43130 Shikhar Bhushan
# You may obtain a copy of the License at
6 1dd43130 Shikhar Bhushan
#
7 1dd43130 Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 1dd43130 Shikhar Bhushan
#
9 1dd43130 Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 1dd43130 Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 1dd43130 Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 1dd43130 Shikhar Bhushan
# See the License for the specific language governing permissions and
13 1dd43130 Shikhar Bhushan
# limitations under the License.
14 1dd43130 Shikhar Bhushan
15 8b4b9936 Shikhar Bhushan
from xml.etree import cElementTree as ET
16 1dd43130 Shikhar Bhushan
17 1dd43130 Shikhar Bhushan
from common import BASE_NS
18 1dd43130 Shikhar Bhushan
from common import qualify as _
19 1dd43130 Shikhar Bhushan
20 1dd43130 Shikhar Bhushan
class HelloParser:
21 1dd43130 Shikhar Bhushan
22 1dd43130 Shikhar Bhushan
    @staticmethod
23 1dd43130 Shikhar Bhushan
    def parse(raw):
24 1dd43130 Shikhar Bhushan
        'Returns tuple of (session-id, ["capability_uri", ...])'
25 1dd43130 Shikhar Bhushan
        sid, capabilities = 0, []
26 1dd43130 Shikhar Bhushan
        root = ET.fromstring(raw)
27 f5c75f88 Shikhar Bhushan
        if root.tag in ('hello', _('hello', BASE_NS)):
28 1dd43130 Shikhar Bhushan
            for child in root.getchildren():
29 f5c75f88 Shikhar Bhushan
                if child.tag in ('session-id', _('session-id', BASE_NS)):
30 1dd43130 Shikhar Bhushan
                    sid = child.text
31 f5c75f88 Shikhar Bhushan
                elif child.tag in ('capabilities', _('capabilities', BASE_NS)):
32 f5c75f88 Shikhar Bhushan
                    for cap in child.getiterator('capability'): 
33 f5c75f88 Shikhar Bhushan
                        capabilities.append(cap.text)
34 1dd43130 Shikhar Bhushan
                    for cap in child.getiterator(_('capability', BASE_NS)):
35 1dd43130 Shikhar Bhushan
                        capabilities.append(cap.text)
36 1dd43130 Shikhar Bhushan
        return sid, capabilities
37 1dd43130 Shikhar Bhushan
38 1dd43130 Shikhar Bhushan
39 1dd43130 Shikhar Bhushan
class RootParser:
40 1dd43130 Shikhar Bhushan
    '''Parser for the top-level element of an XML document. Does not look at any
41 d0452bd7 Shikhar Bhushan
    sub-elements. It is useful for efficiently determining the type of received
42 d0452bd7 Shikhar Bhushan
    messages.
43 1dd43130 Shikhar Bhushan
    '''
44 1dd43130 Shikhar Bhushan
    
45 1dd43130 Shikhar Bhushan
    def __init__(self, recognize=[]):
46 1dd43130 Shikhar Bhushan
        self._recognized = recognize
47 1dd43130 Shikhar Bhushan
    
48 1dd43130 Shikhar Bhushan
    def recognize(self, element):
49 1dd43130 Shikhar Bhushan
        '''Specify an element that should be successfully parsed.
50 1dd43130 Shikhar Bhushan
        
51 1dd43130 Shikhar Bhushan
        element should be a string that represents a qualified name of the form
52 1dd43130 Shikhar Bhushan
        `{namespace}tag`.
53 1dd43130 Shikhar Bhushan
        '''
54 1dd43130 Shikhar Bhushan
        self._recognized.append(element)
55 1dd43130 Shikhar Bhushan
    
56 1dd43130 Shikhar Bhushan
    def parse(self, raw):
57 1dd43130 Shikhar Bhushan
        '''Parse the top-level element from a string representing an XML document.
58 1dd43130 Shikhar Bhushan
        
59 1dd43130 Shikhar Bhushan
        Returns a `(tag, attributes)` tuple, where `tag` is a string representing
60 1dd43130 Shikhar Bhushan
        the qualified name of the recognized element and `attributes` is an
61 1dd43130 Shikhar Bhushan
        `{attribute: value}` dictionary.
62 1dd43130 Shikhar Bhushan
        '''
63 1dd43130 Shikhar Bhushan
        fp = StringIO(raw)
64 1dd43130 Shikhar Bhushan
        for event, element in ET.iterparse(fp, events=('start',)):
65 d0452bd7 Shikhar Bhushan
            for ele in self._recognized:
66 1dd43130 Shikhar Bhushan
                if element.tag == ele:
67 1dd43130 Shikhar Bhushan
                    return (element.tag, element.attrib)
68 1dd43130 Shikhar Bhushan
            break
69 1dd43130 Shikhar Bhushan
        return None