46
46
47
47
class RobotRemoteServer (SimpleXMLRPCServer ):
48
48
allow_reuse_address = True
49
- _generic_exceptions = (AssertionError , RuntimeError , Exception )
50
- _fatal_exceptions = (SystemExit , KeyboardInterrupt )
51
49
52
50
def __init__ (self , library , host = '127.0.0.1' , port = 8270 , port_file = None ,
53
51
allow_stop = True ):
@@ -65,7 +63,7 @@ def __init__(self, library, host='127.0.0.1', port=8270, port_file=None,
65
63
``Stop Remote Server`` keyword.
66
64
"""
67
65
SimpleXMLRPCServer .__init__ (self , (host , int (port )), logRequests = False )
68
- self ._library = library
66
+ self ._library = RemoteLibrary ( library , self . stop_remote_server )
69
67
self ._allow_stop = allow_stop
70
68
self ._shutdown = False
71
69
self ._register_functions ()
@@ -117,6 +115,38 @@ def stop_remote_server(self):
117
115
self ._log (prefix + 'does not allow stopping.' , 'WARN' )
118
116
return self ._shutdown
119
117
118
+ def _log (self , msg , level = None ):
119
+ if level :
120
+ msg = '*%s* %s' % (level .upper (), msg )
121
+ self ._write_to_stream (msg , sys .stdout )
122
+ if sys .__stdout__ is not sys .stdout :
123
+ self ._write_to_stream (msg , sys .__stdout__ )
124
+
125
+ def _write_to_stream (self , msg , stream ):
126
+ stream .write (msg + '\n ' )
127
+ stream .flush ()
128
+
129
+ def get_keyword_names (self ):
130
+ return self ._library .get_keyword_names ()
131
+
132
+ def run_keyword (self , name , args , kwargs = None ):
133
+ return self ._library .run_keyword (name , args , kwargs )
134
+
135
+ def get_keyword_arguments (self , name ):
136
+ return self ._library .get_keyword_arguments (name )
137
+
138
+ def get_keyword_documentation (self , name ):
139
+ return self ._library .get_keyword_documentation (name )
140
+
141
+
142
+ class RemoteLibrary (object ):
143
+ _generic_exceptions = (AssertionError , RuntimeError , Exception )
144
+ _fatal_exceptions = (SystemExit , KeyboardInterrupt )
145
+
146
+ def __init__ (self , library , stop_remote_server = None ):
147
+ self ._library = library
148
+ self ._stop_remote_server = stop_remote_server
149
+
120
150
def get_keyword_names (self ):
121
151
get_kw_names = (getattr (self ._library , 'get_keyword_names' , None ) or
122
152
getattr (self ._library , 'getKeywordNames' , None ))
@@ -125,7 +155,9 @@ def get_keyword_names(self):
125
155
else :
126
156
names = [attr for attr in dir (self ._library ) if attr [0 ] != '_' and
127
157
self ._is_function_or_method (getattr (self ._library , attr ))]
128
- return names + ['stop_remote_server' ]
158
+ if self ._stop_remote_server :
159
+ names .append ('stop_remote_server' )
160
+ return names
129
161
130
162
def _is_function_or_method (self , item ):
131
163
return inspect .isfunction (item ) or inspect .ismethod (item )
@@ -178,35 +210,9 @@ def _add_to_result(self, result, key, value, default=''):
178
210
if value != default :
179
211
result [key ] = value
180
212
181
- def get_keyword_arguments (self , name ):
182
- kw = self ._get_keyword (name )
183
- if not kw :
184
- return []
185
- return self ._arguments_from_kw (kw )
186
-
187
- def _arguments_from_kw (self , kw ):
188
- args , varargs , kwargs , defaults = inspect .getargspec (kw )
189
- if inspect .ismethod (kw ):
190
- args = args [1 :] # drop 'self'
191
- if defaults :
192
- args , names = args [:- len (defaults )], args [- len (defaults ):]
193
- args += ['%s=%s' % (n , d ) for n , d in zip (names , defaults )]
194
- if varargs :
195
- args .append ('*%s' % varargs )
196
- if kwargs :
197
- args .append ('**%s' % kwargs )
198
- return args
199
-
200
- def get_keyword_documentation (self , name ):
201
- if name == '__intro__' :
202
- return inspect .getdoc (self ._library ) or ''
203
- if name == '__init__' and inspect .ismodule (self ._library ):
204
- return ''
205
- return inspect .getdoc (self ._get_keyword (name )) or ''
206
-
207
213
def _get_keyword (self , name ):
208
214
if name == 'stop_remote_server' :
209
- return self .stop_remote_server
215
+ return self ._stop_remote_server
210
216
kw = getattr (self ._library , name , None )
211
217
if not self ._is_function_or_method (kw ):
212
218
return None
@@ -301,19 +307,35 @@ def _restore_std_streams(self):
301
307
stdout += '\n '
302
308
return self ._handle_binary_result (stdout + stderr )
303
309
304
- def _log (self , msg , level = None ):
305
- if level :
306
- msg = '*%s* %s' % (level .upper (), msg )
307
- self ._write_to_stream (msg , sys .stdout )
308
- if sys .__stdout__ is not sys .stdout :
309
- self ._write_to_stream (msg , sys .__stdout__ )
310
+ def get_keyword_arguments (self , name ):
311
+ kw = self ._get_keyword (name )
312
+ if not kw :
313
+ return []
314
+ return self ._arguments_from_kw (kw )
310
315
311
- def _write_to_stream (self , msg , stream ):
312
- stream .write (msg + '\n ' )
313
- stream .flush ()
316
+ def _arguments_from_kw (self , kw ):
317
+ args , varargs , kwargs , defaults = inspect .getargspec (kw )
318
+ if inspect .ismethod (kw ):
319
+ args = args [1 :] # drop 'self'
320
+ if defaults :
321
+ args , names = args [:- len (defaults )], args [- len (defaults ):]
322
+ args += ['%s=%s' % (n , d ) for n , d in zip (names , defaults )]
323
+ if varargs :
324
+ args .append ('*%s' % varargs )
325
+ if kwargs :
326
+ args .append ('**%s' % kwargs )
327
+ return args
328
+
329
+ def get_keyword_documentation (self , name ):
330
+ if name == '__intro__' :
331
+ return inspect .getdoc (self ._library ) or ''
332
+ if name == '__init__' and inspect .ismodule (self ._library ):
333
+ return ''
334
+ return inspect .getdoc (self ._get_keyword (name )) or ''
314
335
315
336
316
337
if __name__ == '__main__' :
338
+
317
339
def stop (uri ):
318
340
server = test (uri , log_success = False )
319
341
if server is not None :
0 commit comments