Statistics
| Branch: | Tag: | Revision:

root / ncclient / capabilities.py @ e769089a

History | View | Annotate | Download (2.3 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") and ":netconf:" in uri:
17
        splitted = uri.split(":")
18
        if ":capability:" in uri:
19
            if uri.startswith("urn:ietf:params:xml:ns:netconf"):
20
                name, version = splitted[7], splitted[8]
21
            else:
22
                name, version = splitted[5], splitted[6]
23
            return [ ":" + name, ":" + name + ":" + version ]
24
        elif ":base:" in uri:
25
            if uri.startswith("urn:ietf:params:xml:ns:netconf"):
26
                return [ ":base", ":base" + ":" + splitted[7] ]
27
            else:
28
                return [ ":base", ":base" + ":" + splitted[5] ]
29
    return []
30

    
31
def schemes(url_uri):
32
    "Given a URI that has a *scheme* query string (i.e. `:url` capability URI), will return a list of supported schemes."
33
    return url_uri.partition("?scheme=")[2].split(",")
34

    
35
class Capabilities:
36

    
37
    "Represents the set of capabilities available to a NETCONF client or server. It is initialized with a list of capability URI's."
38
    
39
    def __init__(self, capabilities):
40
        self._dict = {}
41
        for uri in capabilities:
42
            self._dict[uri] = _abbreviate(uri)
43

    
44
    def __contains__(self, key):
45
        if key in self._dict:
46
            return True
47
        for abbrs in self._dict.values():
48
            if key in abbrs:
49
                return True
50
        return False
51

    
52
    def __len__(self):
53
        return len(self._dict)
54

    
55
    def __iter__(self):
56
        return self._dict.iterkeys()
57

    
58
    def __repr__(self):
59
        return repr(self._dict.keys())
60

    
61
    def add(self, uri):
62
        "Add a capability."
63
        self._dict[uri] = _abbreviate(uri)
64

    
65
    def remove(self, uri):
66
        "Remove a capability."
67
        if key in self._dict:
68
            del self._dict[key]