Revision ee4bb099 ncclient/content.py

b/ncclient/content.py
12 12
# See the License for the specific language governing permissions and
13 13
# limitations under the License.
14 14

  
15
import logging
15 16
from xml.etree import cElementTree as ElementTree
16 17

  
17
NAMESPACE = 'urn:ietf:params:xml:ns:netconf:base:1.0'
18
logger = logging.getLogger('ncclient.content')
18 19

  
19
def qualify(tag, ns=NAMESPACE):
20
BASE_NS = 'urn:ietf:params:xml:ns:netconf:base:1.0'
21
NOTIFICATION_NS = 'urn:ietf:params:xml:ns:netconf:notification:1.0'
22

  
23
def qualify(tag, ns=BASE_NS):
20 24
    return '{%s}%s' % (ns, tag)
21 25

  
22 26
_ = qualify
23 27

  
24 28
def make_hello(capabilities):
25
    return '<hello xmlns="%s">%s</hello>' % (NAMESPACE, capabilities)
29
    return '<hello xmlns="%s">%s</hello>' % (BASE_NS, capabilities)
26 30

  
27 31
def make_rpc(id, op):
28
    return '<rpc message-id="%s" xmlns="%s">%s</rpc>' % (id, NAMESPACE, op)
32
    return '<rpc message-id="%s" xmlns="%s">%s</rpc>' % (id, BASE_NS, op)
29 33

  
30 34
def parse_hello(raw):
31
    from capability import Capabilities
35
    from capabilities import Capabilities
32 36
    id, capabilities = 0, Capabilities()
33 37
    root = ElementTree.fromstring(raw)
34 38
    if root.tag == _('hello'):
......
40 44
                    capabilities.add(cap.text)
41 45
    return id, capabilities
42 46

  
43
def parse_message_type(raw):
44
    
45
    target = RootElementParser()
46
    parser = ElementTree.XMLTreeBuilder(target=target)
47
    parser.feed(raw)
48
    return target.id
47
def parse_message_root(raw):
48
    from cStringIO import StringIO
49
    fp = StringIO(raw)
50
    for event, element in ElementTree.iterparse(fp, events=('start',)):
51
        if element.tag == _('rpc'):
52
            return element.attrib['message-id']
53
        elif element.tag == _('notification', NOTIFICATION_NS):
54
            return 'notification'
55
        else:
56
            return None

Also available in: Unified diff