|
| 1 | +# robotframework-tools |
| 2 | +# |
| 3 | +# Python Tools for Robot Framework and Test Libraries. |
| 4 | +# |
| 5 | +# Copyright (C) 2013-2016 Stefan Zimmermann <zimmermann.code@gmail.com> |
| 6 | +# |
| 7 | +# robotframework-tools is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# robotframework-tools is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with robotframework-tools. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +"""robottools.testrobot.handler |
| 21 | +
|
| 22 | +Making ``robot.running.handlers`` work better with |
| 23 | +:class:`robottools.TestRobot`. |
| 24 | +
|
| 25 | +.. moduleauthor:: Stefan Zimmermann <zimmermann.code@gmail.com> |
| 26 | +""" |
| 27 | +__all__ = ['Handler'] |
| 28 | + |
| 29 | +from moretools import isstring, dictitems |
| 30 | + |
| 31 | + |
| 32 | +class Handler(object): |
| 33 | + """A wrapper for instances of classes from ``robot.running.handlers``, |
| 34 | + implementing a custom :meth:`.resolve_arguments`, |
| 35 | + which supports passing arbitrary python objects |
| 36 | + as keyword argument values. |
| 37 | + """ |
| 38 | + def __init__(self, handler): |
| 39 | + """Create with the `handler` instance to wrap. |
| 40 | + """ |
| 41 | + self._handler = handler |
| 42 | + |
| 43 | + def __getattr__(self, name): |
| 44 | + return getattr(self._handler, name) |
| 45 | + |
| 46 | + # HACK |
| 47 | + def resolve_arguments(self, args_and_kwargs, variables): |
| 48 | + """More Pythonic argument handling for interactive |
| 49 | + :class:`robottools.testrobot.keyword.Keyword` calls. |
| 50 | +
|
| 51 | + Original ``resolve_arguments`` methods from ``robot.running.handlers`` |
| 52 | + expect as first argument a single list of Keyword arguments |
| 53 | + coming from an RFW script:: |
| 54 | +
|
| 55 | + ['arg0', 'arg1', ..., 'name=value', ...] |
| 56 | +
|
| 57 | + So there is no chance to pass unstringified named argument values. |
| 58 | + Only unstringified positional arguments are possible. |
| 59 | +
|
| 60 | + This wrapper method takes a normal Python `args_and_kwargs` pair |
| 61 | + instead as first argument:: |
| 62 | +
|
| 63 | + (arg0, arg1, ...), {name: value, ...} |
| 64 | +
|
| 65 | + It resolves the named arguments stringified via the original method |
| 66 | + but returns the original Python values:: |
| 67 | +
|
| 68 | + [arg0, arg1, ...], [(name, value), ...] |
| 69 | +
|
| 70 | + Only strings are untouched. |
| 71 | + So RFW ``'...${variable}...'`` substitution still works. |
| 72 | + """ |
| 73 | + posargs, kwargs = args_and_kwargs |
| 74 | + rfwargs = list(posargs) |
| 75 | + # prepare 'key=value' strings for original RFW method |
| 76 | + for name, value in dictitems(kwargs): |
| 77 | + if not isstring(value): |
| 78 | + value = repr(value) |
| 79 | + rfwargs.append(u'%s=%s' % (name, value)) |
| 80 | + posargs, rfwkwargslist \ |
| 81 | + = self._handler.resolve_arguments(rfwargs, variables) |
| 82 | + # and replace values with original non-string objects after resolving |
| 83 | + kwargslist = [] |
| 84 | + for name, rfwvalue in rfwkwargslist: |
| 85 | + value = kwargs[name] |
| 86 | + if isstring(value): |
| 87 | + value = rfwvalue |
| 88 | + kwargslist.append(value) |
| 89 | + return posargs, kwargslist |
0 commit comments