Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ b2d60e49

History | View | Annotate | Download (2.4 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
def _abbreviate(uri):
16
    if uri.startswith("urn:ietf:params:netconf:"):
17
        splitted = uri.split(":")
18
        if ":capability:" in uri:
19
            name, version = splitted[5], splitted[6]
20
            return [ ":" + name, ":" + name + ":" + version ]
21
        elif ":base:" in uri:
22
            return [ ":base", ":base" + ":" + splitted[5] ]
23
    return []
24

    
25
def schemes(url_uri):
26
    """Given a URI that has a *scheme* query string (i.e. *:url* capability URI), will return a list
27
    of supported schemes.
28
    """
29
    return url_uri.partition("?scheme=")[2].split(",")
30

    
31
class Capabilities:
32

    
33
    """Represents the set of capabilities available to a NETCONF client or server. It is initialized
34
    with a list of capability URI's. These can be iterated over.
35
    
36
    Presence of a capability can be checked with the *in* operation. In addition to the URI, for
37
    capabilities of the form *urn:ietf:params:netconf:capability:$name:$version* their shorthand can
38
    be used as a key. For example, for *urn:ietf:params:netconf:capability:candidate:1.0* the
39
    shorthand would be *:candidate*. If version is significant, use *:candidate:1.0* as key.
40
    """
41
    
42
    def __init__(self, capabilities):
43
        self._dict = {}
44
        for uri in capabilities:
45
            self._dict[uri] = _abbreviate(uri)
46

    
47
    def __contains__(self, key):
48
        if key in self._dict:
49
            return True
50
        for abbrs in self._dict.values():
51
            if key in abbrs:
52
                return True
53
        return False
54

    
55
    def __len__(self):
56
        return len(self._dict)
57

    
58
    def __iter__(self):
59
        return self._dict.iterkeys()
60

    
61
    def __repr__(self):
62
        return repr(self._dict.keys())
63

    
64
    def add(self, uri):
65
        "Add a capability."
66
        self._dict[uri] = _abbreviate(uri)
67

    
68
    def remove(self, uri):
69
        "Remove a capability."
70
        if key in self._dict:
71
            del self._dict[key]