Revision 68ac4439 ncclient/manager.py

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)

Also available in: Unified diff