24
24
import yaml
25
25
26
26
from six .moves .urllib .parse import urlencode , quote_plus , urlparse , urlunparse
27
+ from six import StringIO
27
28
28
29
from websocket import WebSocket , ABNF , enableTrace
29
30
33
34
ERROR_CHANNEL = 3
34
35
RESIZE_CHANNEL = 4
35
36
37
+ class _IgnoredIO :
38
+ def write (self , _x ):
39
+ pass
40
+
41
+ def getvalue (self ):
42
+ raise TypeError ("Tried to read_all() from a WSClient configured to not capture. Did you mean `capture_all=True`?" )
43
+
36
44
37
45
class WSClient :
38
- def __init__ (self , configuration , url , headers ):
46
+ def __init__ (self , configuration , url , headers , capture_all ):
39
47
"""A websocket client with support for channels.
40
48
41
49
Exec command uses different channels for different streams. for
@@ -47,7 +55,10 @@ def __init__(self, configuration, url, headers):
47
55
header = []
48
56
self ._connected = False
49
57
self ._channels = {}
50
- self ._all = ""
58
+ if capture_all :
59
+ self ._all = StringIO ()
60
+ else :
61
+ self ._all = _IgnoredIO ()
51
62
52
63
# We just need to pass the Authorization, ignore all the other
53
64
# http headers we get from the generated code
@@ -157,8 +168,8 @@ def read_all(self):
157
168
TODO: Maybe we can process this and return a more meaningful map with
158
169
channels mapped for each input.
159
170
"""
160
- out = self ._all
161
- self ._all = ""
171
+ out = self ._all . getvalue ()
172
+ self ._all = self . _all . __class__ ()
162
173
self ._channels = {}
163
174
return out
164
175
@@ -195,7 +206,7 @@ def update(self, timeout=0):
195
206
if channel in [STDOUT_CHANNEL , STDERR_CHANNEL ]:
196
207
# keeping all messages in the order they received
197
208
# for non-blocking call.
198
- self ._all += data
209
+ self ._all . write ( data )
199
210
if channel not in self ._channels :
200
211
self ._channels [channel ] = data
201
212
else :
@@ -257,6 +268,7 @@ def websocket_call(configuration, *args, **kwargs):
257
268
url = args [1 ]
258
269
_request_timeout = kwargs .get ("_request_timeout" , 60 )
259
270
_preload_content = kwargs .get ("_preload_content" , True )
271
+ capture_all = kwargs .get ("capture_all" , True )
260
272
headers = kwargs .get ("headers" )
261
273
262
274
# Expand command parameter list to indivitual command params
@@ -272,7 +284,7 @@ def websocket_call(configuration, *args, **kwargs):
272
284
url += '?' + urlencode (query_params )
273
285
274
286
try :
275
- client = WSClient (configuration , get_websocket_url (url ), headers )
287
+ client = WSClient (configuration , get_websocket_url (url ), headers , capture_all )
276
288
if not _preload_content :
277
289
return client
278
290
client .run_forever (timeout = _request_timeout )
0 commit comments