Revision 179b00d4 ncclient/manager.py

b/ncclient/manager.py
37 37
    session.connect(*args, **kwds)
38 38
    return Manager(session)
39 39

  
40
connect = connect_ssh # default
40
connect = connect_ssh # default session type
41 41

  
42 42
class Manager:
43 43
    
44 44
    'Facade for the API'
45 45
    
46
    def __init__(self, session):
47
        self._session = session
46
    RAISE_ALL = 0
47
    RAISE_ERROR = 1
48
    RAISE_NONE = 2
48 49
    
49
    def _get(self, type, *args, **kwds):
50
        op = OPERATIONS[type](self._session)
51
        reply = op.request(*args, **kwds)
52
        if not reply.ok:
53
            raise reply.errors[0]
54
        else:
55
            return reply.data
50
    def __init__(self, session, rpc_error=Manager.RAISE_ERROR):
51
        self._session = session
52
        self._raise = rpc_error
56 53

  
57 54
    def do(self, op, *args, **kwds):
58 55
        op = OPERATIONS[op](self._session)
59 56
        reply = op.request(*args, **kwds)
60 57
        if not reply.ok:
61
            raise reply.errors[0]
58
            if self._raise == Manager.RAISE_ALL:
59
                raise reply.error
60
            elif self._raise == Manager.RAISE_ERROR:
61
                for error in reply.errors:
62
                    if error.severity == 'error':
63
                        raise error
62 64
        return reply
63

  
64
    def locked(self, target='running'):
65
    
66
    def __enter__(self):
67
        pass
68
    
69
    def __exit__(self, *args):
70
        self.close()
71
        return False
72
    
73
    def _get(self, type, *args, **kwds):
74
        reply = self.do(type)
75
        return reply.data
76
    
77
    def locked(self, target):
78
        "For use with 'with'. target is the datastore, e.g. 'candidate'"
65 79
        return operations.LockContext(self._session, target)
66 80
    
67 81
    get = lambda self, *args, **kwds: self._get('get')
......
89 103
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
90 104
    
91 105
    def close(self):
92
        try:
106
        try: # try doing it clean
93 107
            self.close_session()
94 108
        except:
95
            self._session.expect_close()
109
            pass
110
        if self._session.connected: # if that didn't work...
96 111
            self._session.close()

Also available in: Unified diff