Statistics
| Branch: | Tag: | Revision:

root / ncclient / content / hello.py @ 589b23e4

History | View | Annotate | Download (2.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
import logging
16
from xml.etree import cElementTree as ElementTree
17

    
18
logging.getLogger('ncclient.content.hello')
19

    
20
from ..capability import Capabilities
21

    
22
ns = 'urn:ietf:params:xml:ns:netconf:base:1.0'
23

    
24
def make(capabilities):
25
    return '<hello xmlns="%s">%s</hello>' % (ns, capabilities)
26

    
27
def parse(raw):
28
    id, capabilities = 0, Capabilities()
29
    hello = ElementTree.fromstring(raw)
30
    for child in hello.getchildren():
31
        if child.tag == '{%s}session-id' % ns:
32
            id = child.text
33
        elif child.tag == '{%s}capabilities' % ns:
34
            for cap in child.getiterator('{%s}capability' % ns):
35
                capabilities.add(cap.text)
36
    return id, capabilities
37

    
38
#class HelloParser:
39
#    
40
#    'Fast parsing with expat'
41
#    
42
#    capability, sid = range(2)
43
#    
44
#    def __init__(self, raw):
45
#        self._sid = None
46
#        self._capabilities = Capabilities()
47
#        p = xml.parsers.expat.ParserCreate()
48
#        p.StartElementHandler = self._start_element
49
#        p.EndElementHandler = self._end_element
50
#        p.CharacterDataHandler = self._char_data
51
#        self._expect = None
52
#        p.parse(raw, True)
53
#    
54
#    def _start_element(self, name, attrs):
55
#        if name == 'capability':
56
#            self._expect = HelloParser.capability
57
#        elif name == 'session-id':
58
#            self._expect = HelloParser.sid
59
#    
60
#    def _end_element(self, name):
61
#        self._expect = None
62
#    
63
#    def _char_data(self, data):
64
#        if self._expect == HelloParser.capability:
65
#            self._capabilities.add(data)
66
#        elif self._expect == HelloParser.sid:
67
#            self._sid = int(data)
68
#    
69
#    @property
70
#    def sid(self): return self._sid
71
#    
72
#    @property
73
#    def capabilities(self): return self._capabilities