Revision a7cb58ce ncclient/capabilities.py

b/ncclient/capabilities.py
14 14

  
15 15
def abbreviate(uri):
16 16
    if uri.startswith('urn:ietf:params:netconf:capability:'):
17
        return (':' + uri.split(':')[5])
17
        return ':' + uri.split(':')[5]
18
    elif uri.startswith('urn:ietf:params:netconf:base:'):
19
        return ':base'
18 20

  
19
def schemes(uri):
20
    return uri.partition("?scheme=")[2].split(',')
21
def version(uri):
22
    if uri.startswith('urn:ietf:params:netconf:capability:'):
23
        return uri.split(':')[6]
24
    elif uri.startswith('urn:ietf:params:netconf:base:'):
25
        return uri.split(':')[5]
21 26

  
22 27
class Capabilities:
23
    
24
    """Represent the capabilities of client or server. Also facilitates using
25
    abbreviated capability names in addition to complete URI.
26
    """
27
    
28

  
28 29
    def __init__(self, capabilities=None):
29 30
        self._dict = {}
30 31
        if isinstance(capabilities, dict):
31 32
            self._dict = capabilities
32 33
        elif isinstance(capabilities, list):
33 34
            for uri in capabilities:
34
                self._dict[uri] = abbreviate(uri)
35
    
35
                self._dict[uri] = (abbreviate(uri), version(uri))
36

  
36 37
    def __contains__(self, key):
37
        return ( key in self._dict ) or ( key in self._dict.values() )
38
    
38
        if key in self._dict:
39
            return True
40
        for info in self._dict.values():
41
            if key == info[0]:
42
                return True
43
        return False
44

  
39 45
    def __iter__(self):
40 46
        return self._dict.keys().__iter__()
41
    
47

  
42 48
    def __repr__(self):
43 49
        return repr(self._dict.keys())
44
    
50

  
45 51
    def __list__(self):
46 52
        return self._dict.keys()
47
    
48
    def add(self, uri, shorthand=None):
49
        if shorthand is None:
50
            shorthand = abbreviate(uri)
51
        self._dict[uri] = shorthand
52
    
53

  
54
    def add(self, uri, info=None):
55
        if info is None:
56
            info = (abbreviate(uri), version(uri))
57
        self._dict[uri] = info
58

  
53 59
    set = add
54
    
60

  
55 61
    def remove(self, key):
56 62
        if key in self._dict:
57 63
            del self._dict[key]
58 64
        else:
59 65
            for uri in self._dict:
60
                if self._dict[uri] == key:
66
                if key in self._dict[uri]:
61 67
                    del self._dict[uri]
62 68
                    break
63 69

  
64
# : the capabilities currently supported by ncclient
70
    def get_uri(self, shortname):
71
        for uri, info in self._dict.items():
72
            if info[0] == shortname:
73
                return uri
74

  
75
    def url_schemes(self):
76
        url_uri = get_uri(':url')
77
        if url_uri is None:
78
            return []
79
        else:
80
            return url_uri.partition("?scheme=")[2].split(',')
81

  
82
    def version(self, key):
83
        try:
84
            return self._dict[key][1]
85
        except KeyError:
86
            for uri, info in self._dict.items():
87
                if info[0] == key:
88
                    return info[1]
89

  
90

  
91
#: the capabilities supported by NCClient
65 92
CAPABILITIES = Capabilities([
66 93
    'urn:ietf:params:netconf:base:1.0',
67 94
    'urn:ietf:params:netconf:capability:writable-running:1.0',
......
74 101
    'urn:ietf:params:netconf:capability:xpath:1.0',
75 102
    #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
76 103
    #'urn:ietf:params:netconf:capability:interleave:1.0' # theoretically already supported
77
])
104
])

Also available in: Unified diff