@@ -140,8 +140,6 @@ def get_keyword_documentation(self, name):
140
140
141
141
142
142
class RemoteLibrary (object ):
143
- _generic_exceptions = (AssertionError , RuntimeError , Exception )
144
- _fatal_exceptions = (SystemExit , KeyboardInterrupt )
145
143
146
144
def __init__ (self , library , stop_remote_server = None ):
147
145
self ._library = library
@@ -163,11 +161,57 @@ def _is_function_or_method(self, item):
163
161
return inspect .isfunction (item ) or inspect .ismethod (item )
164
162
165
163
def run_keyword (self , name , args , kwargs = None ):
164
+ kw = self ._get_keyword (name )
165
+ return KeywordRunner (kw ).run_keyword (args , kwargs )
166
+
167
+ def _get_keyword (self , name ):
168
+ if name == 'stop_remote_server' :
169
+ return self ._stop_remote_server
170
+ kw = getattr (self ._library , name , None )
171
+ if not self ._is_function_or_method (kw ):
172
+ return None
173
+ return kw
174
+
175
+ def get_keyword_arguments (self , name ):
176
+ kw = self ._get_keyword (name )
177
+ if not kw :
178
+ return []
179
+ return self ._arguments_from_kw (kw )
180
+
181
+ def _arguments_from_kw (self , kw ):
182
+ args , varargs , kwargs , defaults = inspect .getargspec (kw )
183
+ if inspect .ismethod (kw ):
184
+ args = args [1 :] # drop 'self'
185
+ if defaults :
186
+ args , names = args [:- len (defaults )], args [- len (defaults ):]
187
+ args += ['%s=%s' % (n , d ) for n , d in zip (names , defaults )]
188
+ if varargs :
189
+ args .append ('*%s' % varargs )
190
+ if kwargs :
191
+ args .append ('**%s' % kwargs )
192
+ return args
193
+
194
+ def get_keyword_documentation (self , name ):
195
+ if name == '__intro__' :
196
+ return inspect .getdoc (self ._library ) or ''
197
+ if name == '__init__' and inspect .ismodule (self ._library ):
198
+ return ''
199
+ return inspect .getdoc (self ._get_keyword (name )) or ''
200
+
201
+
202
+ class KeywordRunner (object ):
203
+ _generic_exceptions = (AssertionError , RuntimeError , Exception )
204
+ _fatal_exceptions = (SystemExit , KeyboardInterrupt )
205
+
206
+ def __init__ (self , keyword ):
207
+ self ._keyword = keyword
208
+
209
+ def run_keyword (self , args , kwargs = None ):
166
210
args , kwargs = self ._handle_binary_args (args , kwargs or {})
167
211
result = {'status' : 'FAIL' }
168
212
self ._intercept_std_streams ()
169
213
try :
170
- return_value = self ._get_keyword ( name ) (* args , ** kwargs )
214
+ return_value = self ._keyword (* args , ** kwargs )
171
215
except :
172
216
exc_type , exc_value , exc_tb = sys .exc_info ()
173
217
if exc_type in self ._fatal_exceptions :
@@ -210,14 +254,6 @@ def _add_to_result(self, result, key, value, default=''):
210
254
if value != default :
211
255
result [key ] = value
212
256
213
- def _get_keyword (self , name ):
214
- if name == 'stop_remote_server' :
215
- return self ._stop_remote_server
216
- kw = getattr (self ._library , name , None )
217
- if not self ._is_function_or_method (kw ):
218
- return None
219
- return kw
220
-
221
257
def _get_error_message (self , exc_type , exc_value ):
222
258
name = exc_type .__name__
223
259
message = self ._get_message_from_exception (exc_value )
@@ -307,32 +343,6 @@ def _restore_std_streams(self):
307
343
stdout += '\n '
308
344
return self ._handle_binary_result (stdout + stderr )
309
345
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 )
315
-
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 ''
335
-
336
346
337
347
if __name__ == '__main__' :
338
348
0 commit comments