Revision 2f8bc438 ncclient/manager.py
b/ncclient/manager.py | ||
---|---|---|
16 | 16 |
import operations |
17 | 17 |
import transport |
18 | 18 |
|
19 |
SESSION_TYPES = { |
|
20 |
'ssh': transport.SSHSession |
|
21 |
} |
|
22 |
|
|
23 | 19 |
OPERATIONS = { |
24 | 20 |
'get': operations.Get, |
25 | 21 |
'get-config': operations.GetConfig, |
... | ... | |
35 | 31 |
'kill-session': operations.KillSession, |
36 | 32 |
} |
37 | 33 |
|
38 |
class Manager(type): |
|
34 |
def connect_ssh(*args, **kwds): |
|
35 |
session = transport.SSHSession(capabilities.CAPABILITIES) |
|
36 |
session.load_system_host_keys() |
|
37 |
session.connect(*args, **kwds) |
|
38 |
return Manager(session) |
|
39 |
|
|
40 |
connect = connect_ssh # default |
|
41 |
|
|
42 |
class Manager: |
|
39 | 43 |
|
40 | 44 |
'Facade for the API' |
41 | 45 |
|
42 |
def connect(self, session_type, *args, **kwds): |
|
43 |
self._session = SESSION_TYPES[session_type](capabilities.CAPABILITIES) |
|
44 |
self._session.connect(*args, **kwds) |
|
45 |
|
|
46 |
def __getattr__(self, name): |
|
47 |
name = name.replace('_', '-') |
|
48 |
if name in OPERATIONS: |
|
49 |
return OPERATIONS[name](self._session).request |
|
50 |
else: |
|
51 |
raise AttributeError |
|
52 |
|
|
53 |
def get(self, *args, **kwds): |
|
54 |
g = operations.Get(self._session) |
|
55 |
reply = g.request(*args, **kwds) |
|
56 |
if reply.errors: |
|
57 |
raise RPCError(reply.errors) |
|
58 |
else: |
|
59 |
return reply.data |
|
46 |
def __init__(self, session): |
|
47 |
self._session = session |
|
60 | 48 |
|
61 |
def get_config(self, *args, **kwds):
|
|
62 |
gc = operations.GetConfig(self._session)
|
|
63 |
reply = gc.request(*args, **kwds)
|
|
64 |
if reply.errors:
|
|
65 |
raise RPCError(reply.errors)
|
|
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]
|
|
66 | 54 |
else: |
67 | 55 |
return reply.data |
68 | 56 |
|
57 |
def request(op, *args, **kwds): |
|
58 |
op = OPERATIONS[op](self._session) |
|
59 |
reply = op.request(*args, **kwds) |
|
60 |
if not reply.ok: |
|
61 |
raise reply.errors[0] |
|
62 |
return reply |
|
63 |
|
|
69 | 64 |
def locked(self, target='running'): |
70 | 65 |
return LockContext(self._session, target) |
66 |
|
|
67 |
get = lambda self, *args, **kwds: self._get('get') |
|
68 |
|
|
69 |
get_config = lambda self, *args, **kwds: self._get('get-config') |
|
70 |
|
|
71 |
edit_config = lambda self, *args, **kwds: self.request('edit-config', *args, **kwds) |
|
72 |
|
|
73 |
copy_config = lambda self, *args, **kwds: self.request('copy-config', *args, **kwds) |
|
74 |
|
|
75 |
validate = lambda self, *args, **kwds: self.request('validate', *args, **kwds) |
|
76 |
|
|
77 |
commit = lambda self, *args, **kwds: self.request('commit', *args, **kwds) |
|
78 |
|
|
79 |
discard_changes = lambda self, *args, **kwds: self.request('discard-changes', *args, **kwds) |
|
80 |
|
|
81 |
delete_config = lambda self, *args, **kwds: self.request('delete-config', *args, **kwds) |
|
82 |
|
|
83 |
lock = lambda self, *args, **kwds: self.request('lock', *args, **kwds) |
|
84 |
|
|
85 |
unlock = lambda self, *args, **kwds: self.request('unlock', *args, **kwds) |
|
86 |
|
|
87 |
close_session = lambda self, *args, **kwds: self.request('close-session', *args, **kwds) |
|
88 |
|
|
89 |
kill_session = lambda self, *args, **kwds: self.request('kill-session', *args, **kwds) |
|
90 |
|
|
91 |
def close(self): |
|
92 |
try: |
|
93 |
self.close_session() |
|
94 |
except: |
|
95 |
self._session.expect_close() |
|
96 |
self._session.close() |
Also available in: Unified diff