Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ 6ab782f7

History | View | Annotate | Download (2.7 kB)

1 d095a59e Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 d095a59e Shikhar Bhushan
#
3 d095a59e Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 d095a59e Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 d095a59e Shikhar Bhushan
# You may obtain a copy of the License at
6 d095a59e Shikhar Bhushan
#
7 d095a59e Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 d095a59e Shikhar Bhushan
#
9 d095a59e Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 d095a59e Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 d095a59e Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 d095a59e Shikhar Bhushan
# See the License for the specific language governing permissions and
13 d095a59e Shikhar Bhushan
# limitations under the License.
14 d095a59e Shikhar Bhushan
15 78f6c132 Shikhar Bhushan
16 d095a59e Shikhar Bhushan
class Capabilities:
17 d095a59e Shikhar Bhushan
    
18 78f6c132 Shikhar Bhushan
    """Represent the capabilities of client or server. Also facilitates using
19 78f6c132 Shikhar Bhushan
    abbreviated capability names in addition to complete URI.
20 78f6c132 Shikhar Bhushan
    """
21 78f6c132 Shikhar Bhushan
    
22 d095a59e Shikhar Bhushan
    def __init__(self, capabilities=None):
23 78f6c132 Shikhar Bhushan
        "TODO: docstring"
24 d095a59e Shikhar Bhushan
        self._dict = {}
25 d095a59e Shikhar Bhushan
        if isinstance(capabilities, dict):
26 d095a59e Shikhar Bhushan
            self._dict = capabilities
27 d095a59e Shikhar Bhushan
        elif isinstance(capabilities, list):
28 d095a59e Shikhar Bhushan
            for uri in capabilities:
29 d095a59e Shikhar Bhushan
                self._dict[uri] = Capabilities.guess_shorthand(uri)
30 d095a59e Shikhar Bhushan
    
31 d095a59e Shikhar Bhushan
    def __contains__(self, key):
32 78f6c132 Shikhar Bhushan
        "TODO: docstring"
33 d095a59e Shikhar Bhushan
        return ( key in self._dict ) or ( key in self._dict.values() )
34 d095a59e Shikhar Bhushan
    
35 d095a59e Shikhar Bhushan
    def __iter__(self):
36 78f6c132 Shikhar Bhushan
        "TODO: docstring"
37 d095a59e Shikhar Bhushan
        return self._dict.keys().__iter__()
38 d095a59e Shikhar Bhushan
    
39 d095a59e Shikhar Bhushan
    def __repr__(self):
40 78f6c132 Shikhar Bhushan
        "TODO: docstring"
41 d095a59e Shikhar Bhushan
        return repr(self._dict.keys())
42 d095a59e Shikhar Bhushan
    
43 94803aaf Shikhar Bhushan
    def __list__(self):
44 94803aaf Shikhar Bhushan
        return self._dict.keys()
45 94803aaf Shikhar Bhushan
    
46 d095a59e Shikhar Bhushan
    def add(self, uri, shorthand=None):
47 78f6c132 Shikhar Bhushan
        "TODO: docstring"
48 d095a59e Shikhar Bhushan
        if shorthand is None:
49 d095a59e Shikhar Bhushan
            shorthand = Capabilities.guess_shorthand(uri)
50 d095a59e Shikhar Bhushan
        self._dict[uri] = shorthand
51 d095a59e Shikhar Bhushan
    
52 d095a59e Shikhar Bhushan
    set = add
53 d095a59e Shikhar Bhushan
    
54 d095a59e Shikhar Bhushan
    def remove(self, key):
55 78f6c132 Shikhar Bhushan
        "TODO: docstring"
56 d095a59e Shikhar Bhushan
        if key in self._dict:
57 d095a59e Shikhar Bhushan
            del self._dict[key]
58 d095a59e Shikhar Bhushan
        else:
59 d095a59e Shikhar Bhushan
            for uri in self._dict:
60 d095a59e Shikhar Bhushan
                if self._dict[uri] == key:
61 d095a59e Shikhar Bhushan
                    del self._dict[uri]
62 d095a59e Shikhar Bhushan
                    break
63 d095a59e Shikhar Bhushan
    
64 d095a59e Shikhar Bhushan
    @staticmethod
65 d095a59e Shikhar Bhushan
    def guess_shorthand(uri):
66 78f6c132 Shikhar Bhushan
        "TODO: docstring"
67 d095a59e Shikhar Bhushan
        if uri.startswith('urn:ietf:params:netconf:capability:'):
68 d095a59e Shikhar Bhushan
            return (':' + uri.split(':')[5])
69 d095a59e Shikhar Bhushan
70 d095a59e Shikhar Bhushan
CAPABILITIES = Capabilities([
71 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:base:1.0',
72 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:writable-running:1.0',
73 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:candidate:1.0',
74 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
75 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
76 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:startup:1.0',
77 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:url:1.0',
78 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:validate:1.0',
79 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:xpath:1.0',
80 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:notification:1.0',
81 d6688264 Shikhar Bhushan
    'urn:ietf:params:netconf:capability:interleave:1.0'
82 d6688264 Shikhar Bhushan
])