Statistics
| Branch: | Tag: | Revision:

root / ncclient / content / parsers.py @ 6625258b

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
from xml.etree import cElementTree as ET
16
from cStringIO import StringIO
17

    
18
from common import BASE_NS
19
from common import qualify as _
20
from common import unqualify as __
21

    
22
class HelloParser:
23

    
24
    @staticmethod
25
    def parse(raw):
26
        'Returns tuple of (session-id, ["capability_uri", ...])'
27
        sid, capabilities = 0, []
28
        root = ET.fromstring(raw)
29
        # cisco's too posh to namespace its hello
30
        if __(root.tag) == 'hello':
31
            for child in root.getchildren():
32
                if __(child.tag) == 'session-id':
33
                    sid = child.text
34
                elif __(child.tag) == 'capabilities':
35
                    for cap in child.getiterator('capability'): 
36
                        capabilities.append(cap.text)
37
                    for cap in child.getiterator(_('capability', BASE_NS)):
38
                        capabilities.append(cap.text)
39
        return sid, capabilities
40

    
41
class RootParser:
42
    '''Parser for the top-level element of an XML document. Does not look at any
43
    sub-elements. It is useful for efficiently determining the type of received
44
    messages.
45
    '''
46
    
47
    @staticmethod
48
    def parse(raw, recognized=[]):
49
        '''Parse the top-level element from a string representing an XML document.
50
        
51
        recognized is a list of tag names that will be successfully parsed.
52
        The tag names should not be qualified. This is for simplicity of parsing
53
        where the NETCONF implementation is non-compliant (e.g. cisco's which 
54
        uses an incorrect namespace)
55
        
56
        Returns a `(tag, attributes)` tuple, where `tag` is a string representing
57
        the qualified name of the recognized element and `attributes` is an
58
        `{attribute: value}` dictionary.
59
        '''
60
        fp = StringIO(raw)
61
        for event, element in ET.iterparse(fp, events=('start',)):
62
            for ele in recognized:
63
                if __(element.tag) == ele:
64
                    attrs = {}
65
                    for attr in element.attrib:
66
                        attrs[__(attr)] = element.attrib[attr]
67
                    return (ele, attrs)
68
            break
69

    
70

    
71
class RPCReplyParser:
72
    
73
    @staticmethod
74
    def parse(raw):
75
        pass