Revision 216bb34c ncclient/manager.py

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

  
15
"Thin layer of abstraction around NCClient"
16

  
15 17
import capabilities
16 18
from operations import OPERATIONS
17 19
import transport
18 20

  
19

  
20
def ssh_connect(*args, **kwds):
21
def connect_ssh(*args, **kwds):
22
    """Connect to NETCONF server over SSH. See :meth:`SSHSession.connect()
23
    <ncclient.transport.SSHSession.connect>` for function signature."""
21 24
    session = transport.SSHSession(capabilities.CAPABILITIES)
22
    session.load_system_host_keys()
25
    session.load_known_hosts()
23 26
    session.connect(*args, **kwds)
24 27
    return Manager(session)
25 28

  
26
connect = ssh_connect # default session type
29
#: Same as :meth:`connect_ssh`
30
connect = connect_ssh
27 31

  
28
#: Raise all errors
32
#: Raise all :class:`~ncclient.operations.rpc.RPCError`
29 33
RAISE_ALL = 0
30
#:
34
#: Only raise when *error-severity* is "error" i.e. no warnings
31 35
RAISE_ERR = 1
32
#:
36
#: Don't raise any
33 37
RAISE_NONE = 2
34 38

  
35 39
class Manager:
36 40

  
37
    "Thin layer of abstraction for the ncclient API."
41
    """API for NETCONF operations. Currently only supports making synchronous
42
    RPC requests.
43

  
44
    It is also a context manager, so a :class:`Manager` instance can be used
45
    with the *with* statement. The session is closed when the context ends. """
38 46

  
39 47
    def __init__(self, session):
40 48
        self._session = session
41
        self._rpc_error_action = RAISE_ALL
49
        self._raise = RAISE_ALL
42 50

  
43 51
    def set_rpc_error_action(self, action):
44
        self._rpc_error_handling = option
52
        """Specify the action to take when an *<rpc-error>* element is encountered.
53

  
54
        :arg action: one of :attr:`RAISE_ALL`, :attr:`RAISE_ERR`, :attr:`RAISE_NONE`
55
        """
56
        self._raise = action
57

  
58
    def __enter__(self):
59
        return self
60

  
61
    def __exit__(self, *args):
62
        self.close()
63
        return False
45 64

  
46 65
    def do(self, op, *args, **kwds):
47 66
        op = OPERATIONS[op](self._session)
......
55 74
                        raise error
56 75
        return reply
57 76

  
58
    def __enter__(self):
59
        pass
77
    #: :see: :meth:`Get.request() <ncclient.operations.Get.request>`
78
    get = lambda self, *args, **kwds: self.do('get', *args, **kwds)
60 79

  
61
    def __exit__(self, *args):
62
        self.close()
63
        return False
80
    #: :see: :meth:`GetConfig.request() <ncclient.operations.GetConfig.request>`
81
    get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds)
64 82

  
65
    def locked(self, target):
66
        """Returns a context manager for use with the 'with' statement.
83
    #: :see: :meth:`EditConfig.request() <ncclient.operations.EditConfig.request>`
84
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
67 85

  
68
        :arg target: name of the datastore to lock
69
        :type target: `string`
70
        """
71
        return operations.LockContext(self._session, target)
86
    #: :see: :meth:`CopyConfig.request() <ncclient.operations.CopyConfig.request>`
87
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
72 88

  
73
    def get(self, filter=None):
74
        pass
89
    #: :see: :meth:`GetConfig.request() <ncclient.operations.Validate.request>`
90
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
75 91

  
76
    def get_config(self, source, filter=None):
77
        pass
92
    #: :see: :meth:`Commit.request() <ncclient.operations.Commit.request>`
93
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
78 94

  
79
    def copy_config(self, source, target):
80
        pass
95
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.DiscardChanges.request>`
96
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
81 97

  
82
    def validate(self, source):
83
        pass
84

  
85
    def commit(self, target):
86
        pass
87

  
88
    def discard_changes(self):
89
        pass
90

  
91
    def delete_config(self, target):
92
        pass
93

  
94
    def lock(self, target):
95
        pass
96

  
97
    def unlock(self, target):
98
        pass
99

  
100
    def close_session(self):
101
        pass
102

  
103
    def kill_session(self, session_id):
104
        pass
105

  
106
    def confirmed_commit(self, timeout=None):
107
        pass
108

  
109
    def confirm(self):
110
        # give confirmation
111
        pass
112

  
113
    def discard_changes(self):
114
        pass
98
    #: :see: :meth:`DeleteConfig.request() <ncclient.operations.DeleteConfig.request>`
99
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
115 100

  
101
    #: :see: :meth:`Lock.request() <ncclient.operations.Lock.request>`
116 102
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
117 103

  
104
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.Unlock.request>`
118 105
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
119 106

  
107
    #: :see: :meth:`CloseSession.request() <ncclient.operations.CloseSession.request>`
120 108
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
121 109

  
110
    #: :see: :meth:`KillSession.request() <ncclient.operations.KillSession.request>`
122 111
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
123 112

  
113
    def locked(self, target):
114
        """Returns a context manager for the *with* statement.
115

  
116
        :arg target: name of the datastore to lock
117
        :type target: `string`
118
        :rtype: :class:`operations.LockContext`
119
        """
120
        return operations.LockContext(self._session, target)
121

  
124 122
    def close(self):
123
        """Closes the NETCONF session. First does *<close-session>* RPC."""
125 124
        try: # try doing it clean
126 125
            self.close_session()
127 126
        except Exception as e:
......
131 130

  
132 131
    @property
133 132
    def session(self, session):
133
        ":class:`~ncclient.transport.Session` instance"
134 134
        return self._session
135 135

  
136 136
    @property
137 137
    def client_capabilities(self):
138
        ":class:`~ncclient.capabilities.Capabilities` object for client"
138 139
        return self._session._client_capabilities
139 140

  
140 141
    @property
141 142
    def server_capabilities(self):
143
        ":class:`~ncclient.capabilities.Capabilities` object for server"
142 144
        return self._session._server_capabilities
143 145

  
144 146
    @property
145 147
    def session_id(self):
148
        "*<session-id>* as assigned by NETCONF server"
146 149
        return self._session.id
150

  
151
    @property
152
    def connected(self):
153
        "Whether currently connected to NETCONF server"
154
        return self._session.connected

Also available in: Unified diff