TA的每日心情 | 无聊 昨天 09:08 |
---|
签到天数: 531 天 连续签到: 1 天 [LV.9]测试副司令
|
1测试积点
最近在看uiautomator的Python封装,代码中使用param_to_property的装饰器,就可以实现
d.open.notification(),这样的转换不知该如何理解。
google了一天了,帮小弟解惑,谢谢啦
- def param_to_property(*props, **kwprops):
- if props and kwprops:
- raise SyntaxError("Can not set both props and kwprops at the same time.")
- class Wrapper(object):
- def __init__(self, func):
- self.func = func
- self.kwargs, self.args = {}, []
- def __getattr__(self, attr):
- if kwprops:
- for prop_name, prop_values in kwprops.items():
- if attr in prop_values and prop_name not in self.kwargs:
- self.kwargs[prop_name] = attr
- return self
- elif attr in props:
- self.args.append(attr)
- return self
- raise AttributeError("%s parameter is duplicated or not allowed!" % attr)
- def __call__(self, *args, **kwargs):
- if kwprops:
- kwargs.update(self.kwargs)
- self.kwargs = {}
- return self.func(*args, **kwargs)
- else:
- new_args, self.args = self.args + list(args), []
- return self.func(*new_args, **kwargs)
- return Wrapper
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- ...
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- @property
- def open(self):
- '''
- Open notification or quick settings.
- Usage:
- d.open.notification()
- d.open.quick_settings()
- '''
- @param_to_property(action=["notification", "quick_settings"])
- def _open(action):
- if action == "notification":
- return self.server.jsonrpc.openNotification()
- else:
- return self.server.jsonrpc.openQuickSettings()
- return _open
复制代码
|
|