Revision dd225c7a ncclient/manager.py

b/ncclient/manager.py
23 23

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

  
28
    :rtype: :class:`Manager`
29
    """
27 30
    session = transport.SSHSession(capabilities.CAPABILITIES)
28 31
    session.load_known_hosts()
29 32
    session.connect(*args, **kwds)
......
32 35
#: Same as :meth:`connect_ssh`
33 36
connect = connect_ssh
34 37

  
35
#: Raise all :class:`~ncclient.operations.rpc.RPCError`
36
RAISE_ALL = 0
37
#: Only raise when *error-severity* is "error" i.e. no warnings
38
RAISE_ERR = 1
39
#: Don't raise any
40
RAISE_NONE = 2
41

  
42
class Manager:
38
class Manager(object):
43 39

  
44
    """API for NETCONF operations. Currently only supports making synchronous
45
    RPC requests.
40
    """API for NETCONF operations.
46 41

  
47 42
    It is also a context manager, so a :class:`Manager` instance can be used
48 43
    with the *with* statement. The session is closed when the context ends. """
49 44

  
50 45
    def __init__(self, session):
51 46
        self._session = session
52
        self._raise = RAISE_ALL
53

  
54
    def set_rpc_error_action(self, action):
55
        """Specify the action to take when an *<rpc-error>* element is encountered.
56

  
57
        :arg action: one of :attr:`RAISE_ALL`, :attr:`RAISE_ERR`, :attr:`RAISE_NONE`
58
        """
59
        self._raise = action
60 47

  
61 48
    def __enter__(self):
62 49
        return self
......
65 52
        self.close()
66 53
        return False
67 54

  
68
    def do(self, op, *args, **kwds):
69
        op = operations.OPERATIONS[op](self._session)
70
        reply = op.request(*args, **kwds)
71
        if not reply.ok:
72
            if self._raise == RAISE_ALL:
73
                raise reply.error
74
            elif self._raise == RAISE_ERR:
75
                for error in reply.errors:
76
                    if error.severity == 'error':
77
                        raise error
78
        return reply
79

  
80
    #: :see: :meth:`Get.request() <ncclient.operations.Get.request>`
81
    get = lambda self, *args, **kwds: self.do('get', *args, **kwds)
82

  
83
    #: :see: :meth:`GetConfig.request() <ncclient.operations.GetConfig.request>`
84
    get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds)
85

  
86
    #: :see: :meth:`EditConfig.request() <ncclient.operations.EditConfig.request>`
87
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
88

  
89
    #: :see: :meth:`CopyConfig.request() <ncclient.operations.CopyConfig.request>`
90
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
91

  
92
    #: :see: :meth:`GetConfig.request() <ncclient.operations.Validate.request>`
93
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
94

  
95
    #: :see: :meth:`Commit.request() <ncclient.operations.Commit.request>`
96
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
97

  
98
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.DiscardChanges.request>`
99
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
100

  
101
    #: :see: :meth:`DeleteConfig.request() <ncclient.operations.DeleteConfig.request>`
102
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
103

  
104
    #: :see: :meth:`Lock.request() <ncclient.operations.Lock.request>`
105
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
106

  
107
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.Unlock.request>`
108
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
109

  
110
    #: :see: :meth:`CloseSession.request() <ncclient.operations.CloseSession.request>`
111
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
112

  
113
    #: :see: :meth:`KillSession.request() <ncclient.operations.KillSession.request>`
114
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
55
    def __getattr__(self, name):
56
        try:
57
            return operations.INDEX[name](self.session).request
58
        except KeyError:
59
            raise AttributeError
115 60

  
116 61
    def locked(self, target):
117 62
        """Returns a context manager for the *with* statement.

Also available in: Unified diff