Revision dbef85cb

b/ncclient/capabilities.py
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
class Capabilities:
16
    
17
    def __init__(self, capabilities=None):
18
        self._dict = {}
19
        if isinstance(capabilities, dict):
20
            self._dict = capabilities
21
        elif isinstance(capabilities, list):
22
            for uri in capabilities:
23
                self._dict[uri] = Capabilities.guess_shorthand(uri)
24
    
25
    def __contains__(self, key):
26
        return ( key in self._dict ) or ( key in self._dict.values() )
27
    
28
    def __iter__(self):
29
        return self._dict.keys().__iter__()
30
    
31
    def __repr__(self):
32
        return repr(self._dict.keys())
33
    
34
    def add(self, uri, shorthand=None):
35
        if shorthand is None:
36
            shorthand = Capabilities.guess_shorthand(uri)
37
        self._dict[uri] = shorthand
38
    
39
    set = add
40
    
41
    def remove(self, key):
42
        if key in self._dict:
43
            del self._dict[key]
44
        else:
45
            for uri in self._dict:
46
                if self._dict[uri] == key:
47
                    del self._dict[uri]
48
                    break
49
    
50
    @staticmethod
51
    def guess_shorthand(uri):
52
        if uri.startswith('urn:ietf:params:netconf:capability:'):
53
            return (':' + uri.split(':')[5])
54

  
55

  
56
CAPABILITIES = Capabilities([
57
    'urn:ietf:params:netconf:base:1.0',
58
    'urn:ietf:params:netconf:capability:writable-running:1.0',
59
    'urn:ietf:params:netconf:capability:candidate:1.0',
60
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
61
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
62
    'urn:ietf:params:netconf:capability:startup:1.0',
63
    'urn:ietf:params:netconf:capability:url:1.0',
64
    'urn:ietf:params:netconf:capability:validate:1.0',
65
    'urn:ietf:params:netconf:capability:xpath:1.0',
66
    'urn:ietf:params:netconf:capability:notification:1.0',
67
    'urn:ietf:params:netconf:capability:interleave:1.0'
68
    ])
69

  
70
if __name__ == "__main__":
71
    assert(':validate' in CAPABILITIES) # test __contains__
/dev/null
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
class Capabilities:
16
    
17
    def __init__(self, capabilities=None):
18
        self._dict = {}
19
        if isinstance(capabilities, dict):
20
            self._dict = capabilities
21
        elif isinstance(capabilities, list):
22
            for uri in capabilities:
23
                self._dict[uri] = Capabilities.guess_shorthand(uri)
24
    
25
    def __contains__(self, key):
26
        return ( key in self._dict ) or ( key in self._dict.values() )
27
    
28
    def __iter__(self):
29
        return self._dict.keys().__iter__()
30
    
31
    def __repr__(self):
32
        return repr(self._dict.keys())
33
    
34
    def add(self, uri, shorthand=None):
35
        if shorthand is None:
36
            shorthand = Capabilities.guess_shorthand(uri)
37
        self._dict[uri] = shorthand
38
    
39
    set = add
40
    
41
    def remove(self, key):
42
        if key in self._dict:
43
            del self._dict[key]
44
        else:
45
            for uri in self._dict:
46
                if self._dict[uri] == key:
47
                    del self._dict[uri]
48
                    break
49
    
50
    @staticmethod
51
    def guess_shorthand(uri):
52
        if uri.startswith('urn:ietf:params:netconf:capability:'):
53
            return (':' + uri.split(':')[5])
54

  
55

  
56
CAPABILITIES = Capabilities([
57
    'urn:ietf:params:netconf:base:1.0',
58
    'urn:ietf:params:netconf:capability:writable-running:1.0',
59
    'urn:ietf:params:netconf:capability:candidate:1.0',
60
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
61
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
62
    'urn:ietf:params:netconf:capability:startup:1.0',
63
    'urn:ietf:params:netconf:capability:url:1.0',
64
    'urn:ietf:params:netconf:capability:validate:1.0',
65
    'urn:ietf:params:netconf:capability:xpath:1.0',
66
    'urn:ietf:params:netconf:capability:notification:1.0',
67
    'urn:ietf:params:netconf:capability:interleave:1.0'
68
    ])
69

  
70
if __name__ == "__main__":
71
    assert(':validate' in CAPABILITIES) # test __contains__

Also available in: Unified diff