Revision 68ac4439

b/ncclient/capabilities.py
36 36

  
37 37
class Capabilities:
38 38

  
39
    """Represents the set of capabilities for a NETCONF client or server.
40
    Initialised with a list of capability URI's.
41

  
42
    Presence of a capability can be checked with the *in* operations. In addition
39
    """Represents the set of capabilities available to a NETCONF client or
40
    server.
41
    
42
    Presence of a capability can be checked with the *in* operation. In addition
43 43
    to the URI, for capabilities of the form
44 44
    *urn:ietf:params:netconf:capability:$name:$version* their shorthand can be
45 45
    used as a key. For example, for
46 46
    *urn:ietf:params:netconf:capability:candidate:1.0* the shorthand would be
47 47
    *:candidate*. If version is significant, use *:candidate:1.0* as key.
48 48
    """
49

  
49
    
50 50
    def __init__(self, capabilities):
51
        "Initializes with a list of capability URI's"
51 52
        self._dict = {}
52 53
        for uri in capabilities:
53 54
            self._dict[uri] = _abbreviate(uri)
......
80 81
        "Remove a capability"
81 82
        if key in self._dict:
82 83
            del self._dict[key]
83

  
84
    def check(self, key):
85
        """Whether specified capability is present.
86

  
87
        :arg key: URI or shorthand
88

  
89
        .. note:
90
            The *in* operation is the preferred form.
91
        """
92
        return key in self
93

  
84
    
94 85
    def get_uri(self, shorthand):
95 86
        for uri, abbrs in self._dict.items():
96 87
            if shorthand in abbrs:
97 88
                return uri
98

  
99
#: :class:`Capabilities` object representing the capabilities currently supported by NCClient
100
CAPABILITIES = Capabilities([
101
    "urn:ietf:params:netconf:base:1.0",
102
    "urn:ietf:params:netconf:capability:writable-running:1.0",
103
    "urn:ietf:params:netconf:capability:candidate:1.0",
104
    "urn:ietf:params:netconf:capability:confirmed-commit:1.0",
105
    "urn:ietf:params:netconf:capability:rollback-on-error:1.0",
106
    "urn:ietf:params:netconf:capability:startup:1.0",
107
    "urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp",
108
    "urn:ietf:params:netconf:capability:validate:1.0",
109
    "urn:ietf:params:netconf:capability:xpath:1.0",
110
    "urn:liberouter:params:netconf:capability:power-control:1.0"
111
    "urn:ietf:params:netconf:capability:interleave:1.0"
112
    #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
113
    
114
])
b/ncclient/manager.py
21 21
import logging
22 22
logger = logging.getLogger('ncclient.manager')
23 23

  
24
def connect_ssh(*args, **kwds):
25
    """Connect to NETCONF server over SSH. See :meth:`SSHSession.connect()
26
    <ncclient.transport.SSHSession.connect>` for argument details.
27 24

  
28
    :rtype: :class:`Manager`
29
    """
30
    session = transport.SSHSession(capabilities.CAPABILITIES)
31
    session.load_known_hosts()
32
    session.connect(*args, **kwds)
33
    return Manager(session)
25
#: :class:`Capabilities` object representing the capabilities currently supported by NCClient
26
CAPABILITIES = capabilities.Capabilities([
27
    "urn:ietf:params:netconf:base:1.0",
28
    "urn:ietf:params:netconf:capability:writable-running:1.0",
29
    "urn:ietf:params:netconf:capability:candidate:1.0",
30
    "urn:ietf:params:netconf:capability:confirmed-commit:1.0",
31
    "urn:ietf:params:netconf:capability:rollback-on-error:1.0",
32
    "urn:ietf:params:netconf:capability:startup:1.0",
33
    "urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp",
34
    "urn:ietf:params:netconf:capability:validate:1.0",
35
    "urn:ietf:params:netconf:capability:xpath:1.0",
36
    "urn:liberouter:params:netconf:capability:power-control:1.0"
37
    "urn:ietf:params:netconf:capability:interleave:1.0"
38
    #'urn:ietf:params:netconf:capability:notification:1.0', # TODO    
39
])
34 40

  
35
#: Same as :meth:`connect_ssh`
36
connect = connect_ssh
37 41

  
38 42
OPERATIONS = {
39 43
    "get": operations.Get,
......
52 56
    "reboot_machine": operations.RebootMachine
53 57
}
54 58

  
55
class Manager(object):
56 59

  
57
    """API for NETCONF operations.
60
def connect_ssh(*args, **kwds):
61
    session = transport.SSHSession(capabilities.CAPABILITIES)
62
    session.load_known_hosts()
63
    session.connect(*args, **kwds)
64
    return Manager(session)
65

  
66
#: Same as :meth:`connect_ssh`
67
connect = connect_ssh
68

  
58 69

  
59
    It is also a context manager, so a :class:`Manager` instance can be used
60
    with the *with* statement. The session is closed when the context ends. """
70
class Manager(object):
61 71

  
62 72
    def __init__(self, session):
63 73
        self._session = session
......
69 79
        return self
70 80

  
71 81
    def __exit__(self, *argss):
72
        self.close()
82
        self.close_session()
73 83
        return False
74 84

  
75 85
    def __getattr__(self, name):
......
83 93
                      raise_mode=self._raise_mode).request
84 94
    
85 95
    def locked(self, target):
86
        """Returns a context manager for the *with* statement.
87

  
88
        :arg target: name of the datastore to lock
89
        :type target: `string`
90
        :rtype: :class:`~ncclient.operations.LockContext`
91
        """
92 96
        return operations.LockContext(self._session, target)
93

  
94
    def close(self):
95
        """Closes the NETCONF session. First does *<close-session>* RPC."""
96
        try: # try doing it clean
97
            self._async_mode = False
98
            self.close_session()
99
        except Exception as e:
100
            logger.debug('error doing <close-session> -- %r' % e)
101
        if self._session.connected: # if that didn't work, this sure will :)
102
            self._session.close()
103

  
104
    @property
105
    def session(self):
106
        ":class:`~ncclient.transport.Session` instance"
107
        return self._session
108

  
97
    
109 98
    @property
110 99
    def client_capabilities(self):
111
        ":class:`~ncclient.capabilities.Capabilities` object for client"
112 100
        return self._session._client_capabilities
113 101

  
114 102
    @property
115 103
    def server_capabilities(self):
116
        ":class:`~ncclient.capabilities.Capabilities` object for server"
117 104
        return self._session._server_capabilities
118 105

  
119 106
    @property
120 107
    def session_id(self):
121
        "*<session-id>* as assigned by NETCONF server"
122 108
        return self._session.id
123 109

  
124 110
    @property
125 111
    def connected(self):
126
        "Whether currently connected to NETCONF server"
127 112
        return self._session.connected
128 113

  
129
    def set_async_mode(self, bool=True):
130
        self._async_mode = bool
114
    def set_async_mode(self, mode):
115
        self._async_mode = mode
131 116

  
132 117
    def set_raise_mode(self, mode):
133
        assert(choice in ('all', 'errors', 'none'))
118
        assert(choice in ("all", "errors", "none"))
134 119
        self._raise_mode = mode
135 120

  
136 121
    async_mode = property(fget=lambda self: self._async_mode, fset=set_async_mode)
137 122

  
138
    raise_mode = property(fget=set_raise_mode, fset=set_raise_mode)
123
    raise_mode = property(fget=lambda self: self._raise_mode, fset=set_raise_mode)
b/ncclient/transport/ssh.py
178 178
        """
179 179

  
180 180
        if username is None:
181
            raise SSHError("No username specified")
182

  
181
            username = getpass.getuser()
182
        
183 183
        sock = None
184 184
        for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
185 185
            af, socktype, proto, canonname, sa = res
......
340 340
        object. This makes it possible to call methods like set_keepalive on it.
341 341
        """
342 342
        return self._transport
343

  

Also available in: Unified diff