Revision 4a2351a5 src/session.py
b/src/session.py | ||
---|---|---|
35 | 35 |
self._id = None # session-id |
36 | 36 |
self._connected = False |
37 | 37 |
self._initialised = False |
38 |
self._error = False |
|
38 | 39 |
self._q = Queue.Queue() |
39 | 40 |
|
40 | 41 |
def _init(self, id, capabilities): |
41 |
if isinstance(id, int) and isinstance(capabilities, Capabilities): |
|
42 |
self.id = id |
|
43 |
self.capabilities[SERVER] = capabilities |
|
44 |
self.initialised = True |
|
45 |
else: # there was an error in parsing or such |
|
46 |
raise ValueError |
|
42 |
self.id = id |
|
43 |
self.capabilities[SERVER] = capabilities |
|
44 |
self.initialised = True |
|
47 | 45 |
|
48 | 46 |
def _greet(self): |
49 | 47 |
hello = Creator() |
... | ... | |
57 | 55 |
raise NotImplementedError |
58 | 56 |
|
59 | 57 |
def send(self, message): |
60 |
if self.ready: |
|
61 |
self._q.add(message) |
|
58 |
'Blocks if session not initialised yet' |
|
59 |
while not (self.ready or self._error): |
|
60 |
time.sleep(0.1) |
|
61 |
if self._error: |
|
62 |
raise SessionError |
|
62 | 63 |
else: |
63 |
raise SessionError('Session not ready') |
|
64 |
|
|
65 |
### Thread methods |
|
64 |
self._q.add(message) |
|
66 | 65 |
|
67 | 66 |
def run(self): |
68 | 67 |
raise NotImplementedError |
69 | 68 |
|
70 |
### Listener methods - these are relevant for the initial greeting only
|
|
69 |
### Listener methods - relevant for the initial greeting
|
|
71 | 70 |
|
72 |
def reply(self, data): |
|
71 |
def reply(self, data, *args, **kwds):
|
|
73 | 72 |
id, capabilities = None, None |
74 | 73 |
try: |
75 | 74 |
p = Parser() |
76 | 75 |
# ... |
77 | 76 |
self._init(id, capabilities) |
78 |
except: # ...
|
|
79 |
pass
|
|
77 |
except: |
|
78 |
self._error = True
|
|
80 | 79 |
finally: |
81 | 80 |
self.remove_listener(self) |
82 | 81 |
|
83 |
def error(self, data): |
|
82 |
def error(self, data, *args, **kwds):
|
|
84 | 83 |
self._close() |
85 |
raise SSHError('Session initialization failed') |
|
86 |
|
|
87 |
### Getter methods and properties |
|
88 |
|
|
89 |
def get_capabilities(self, whose): |
|
90 |
return self._capabilities[whose] |
|
84 |
self.remove_listener(self) |
|
85 |
self._error = True |
|
91 | 86 |
|
92 |
ready = property(lambda self: self._connected and self._initialised) |
|
87 |
### Properties |
|
88 |
|
|
89 |
@property |
|
90 |
def client_capabilities(self): return self._capabilities[CLIENT] |
|
93 | 91 |
|
94 |
id = property(lambda self: self._id) |
|
92 |
@property |
|
93 |
def serve_capabilities(self): return self._capabilities[SERVER] |
|
95 | 94 |
|
96 |
client_capabilities = property(lambda self: self._capabilities[CLIENT]) |
|
95 |
@property |
|
96 |
def ready(self): return (self._connected and self._initialised) |
|
97 | 97 |
|
98 |
server_capabilities = property(lambda self: self._capabilities[SERVER]) |
|
98 |
@property |
|
99 |
def id(self): return self._id |
|
100 |
|
Also available in: Unified diff