Last active
August 29, 2015 14:07
-
-
Save guykisel/479b7e39bce966257dbd to your computer and use it in GitHub Desktop.
RF autocast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# No autocast: | |
# https://github.com/guykisel/robotframework-faker/issues/3 | |
# Does not work: ${WordsList} = Words nb=10 | |
# Works: ${WordsList} = Words nb=${10} | |
# Libdoc introspects accurate args/kwargs | |
def __getattr__(self, name): | |
func = None | |
if name in self._fake.__dict__.keys(): | |
return self._fake.__dict__[name].im_func) | |
elif name in faker.generator.Generator.__dict__.keys(): | |
return faker.generator.Generator.__dict__[name] | |
raise AttributeError('Non-existing keyword "{0}"'.format(name)) | |
# with autocast | |
# Works: ${WordsList} = Words nb=10 | |
# Works: ${WordsList} = Words nb=${10} | |
# Libdoc shows *args, **kwargs for all keywords | |
def __getattr__(self, name): | |
if name in _fake.__dict__.keys(): | |
return _str_vars_to_data(_fake.__dict__[name].im_func) | |
elif name in faker.generator.Generator.__dict__.keys(): | |
return _str_vars_to_data(faker.generator.Generator.__dict__[name]) | |
raise AttributeError('Non-existing keyword "{0}"'.format(name)) | |
def _str_to_data(string): | |
try: | |
return ast.literal_eval(str(string).strip()) | |
except Exception: | |
return string | |
@decorator.decorator | |
def _str_vars_to_data(f, *args, **kwargs): | |
args = [_str_to_data(arg) for arg in args] | |
kwargs = dict((arg_name, _str_to_data(arg)) for arg_name, arg in kwargs.items()) | |
result = f(*args, **kwargs) | |
logger.debug(result) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment