Revision 216bb34c ncclient/capabilities.py

b/ncclient/capabilities.py
12 12
# See the License for the specific language governing permissions and
13 13
# limitations under the License.
14 14

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

  
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]
15
def _abbreviate(uri):
16
    if uri.startswith('urn:ietf:params:netconf:'):
17
        splitted = uri.split(':')
18
        if ':capability:' in uri:
19
            return [ ':' + splitted[5], ':' + splitted[5] + ':' + splitted[6] ]
20
        elif ':base:' in uri:
21
            return [ ':base', ':base' + ':'+ splitted[5] ]
22
        else:
23
            return []
24
    else:
25
        return []
26

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

  
27 33
class Capabilities:
28 34

  
29
    def __init__(self, capabilities=None):
35
    """Represents the set of capabilities for a NETCONF client or server.
36
    Initialised with a list of capability URI's.
37

  
38
    Presence of a capability can be checked with the *in* operations. In addition
39
    to the URI, for capabilities of the form
40
    *urn:ietf:params:netconf:capability:$name:$version* their shorthand can be
41
    used as a key. For example, for
42
    *urn:ietf:params:netconf:capability:candidate:1.0* the shorthand would be
43
    *:candidate*. If version is significant, use *:candidate:1.0* as key.
44
    """
45

  
46
    def __init__(self, capabilities):
30 47
        self._dict = {}
31
        if isinstance(capabilities, dict):
32
            self._dict = capabilities
33
        elif isinstance(capabilities, list):
34
            for uri in capabilities:
35
                self._dict[uri] = (abbreviate(uri), version(uri))
48
        for uri in capabilities:
49
            self._dict[uri] = _abbreviate(uri)
36 50

  
37 51
    def __contains__(self, key):
38 52
        if key in self._dict:
39 53
            return True
40
        for info in self._dict.values():
41
            if key == info[0]:
54
        for abbrs in self._dict.values():
55
            if key in abbrs:
42 56
                return True
43 57
        return False
44 58

  
59
    def __len__(self):
60
        return len(self._dict)
61

  
45 62
    def __iter__(self):
46 63
        return self._dict.keys().__iter__()
47 64

  
......
51 68
    def __list__(self):
52 69
        return self._dict.keys()
53 70

  
54
    def add(self, uri, info=None):
55
        if info is None:
56
            info = (abbreviate(uri), version(uri))
57
        self._dict[uri] = info
71
    def add(self, uri):
72
        "Add a capability"
73
        self._dict[uri] = _abbreviate(uri)
58 74

  
59
    set = add
60

  
61
    def remove(self, key):
75
    def remove(self, uri):
76
        "Remove a capability"
62 77
        if key in self._dict:
63 78
            del self._dict[key]
64
        else:
65
            for uri in self._dict:
66
                if key in self._dict[uri]:
67
                    del self._dict[uri]
68
                    break
69

  
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 79

  
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]
80
    def check(self, key):
81
        """Whether specified capability is present.
82

  
83
        :arg key: URI or shorthand
84

  
85
        .. note:
86
            The *in* operation is the preferred form.
87
        """
88
        return key in self
89 89

  
90
    def get_uris(self, shorthand):
91
        return [uri for uri, abbrs in self._dict.items() if shorthand in abbrs]
90 92

  
91
#: the capabilities supported by NCClient
93
#: :class:`Capabilities` object representing the capabilities currently supported by NCClient
92 94
CAPABILITIES = Capabilities([
93 95
    'urn:ietf:params:netconf:base:1.0',
94 96
    'urn:ietf:params:netconf:capability:writable-running:1.0',

Also available in: Unified diff