Skip to content

Commit 98a3a03

Browse files
committed
Split keyword running to separate class
1 parent 1072558 commit 98a3a03

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

src/robotremoteserver.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ def get_keyword_documentation(self, name):
140140

141141

142142
class RemoteLibrary(object):
143-
_generic_exceptions = (AssertionError, RuntimeError, Exception)
144-
_fatal_exceptions = (SystemExit, KeyboardInterrupt)
145143

146144
def __init__(self, library, stop_remote_server=None):
147145
self._library = library
@@ -163,11 +161,57 @@ def _is_function_or_method(self, item):
163161
return inspect.isfunction(item) or inspect.ismethod(item)
164162

165163
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):
166210
args, kwargs = self._handle_binary_args(args, kwargs or {})
167211
result = {'status': 'FAIL'}
168212
self._intercept_std_streams()
169213
try:
170-
return_value = self._get_keyword(name)(*args, **kwargs)
214+
return_value = self._keyword(*args, **kwargs)
171215
except:
172216
exc_type, exc_value, exc_tb = sys.exc_info()
173217
if exc_type in self._fatal_exceptions:
@@ -210,14 +254,6 @@ def _add_to_result(self, result, key, value, default=''):
210254
if value != default:
211255
result[key] = value
212256

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-
221257
def _get_error_message(self, exc_type, exc_value):
222258
name = exc_type.__name__
223259
message = self._get_message_from_exception(exc_value)
@@ -307,32 +343,6 @@ def _restore_std_streams(self):
307343
stdout += '\n'
308344
return self._handle_binary_result(stdout + stderr)
309345

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-
336346

337347
if __name__ == '__main__':
338348

0 commit comments

Comments
 (0)