1"""A Bluetooth Client Decorator util for an Android Device. 2 3This utility allows the user to decorate an device with a custom decorator from 4the blueberry/decorators directory. 5""" 6 7from __future__ import absolute_import 8from __future__ import division 9from __future__ import print_function 10 11import importlib 12import re 13from mobly.controllers.android_device import AndroidDevice 14 15 16def decorate(ad, decorator): 17 """Utility to decorate an AndroidDevice. 18 19 Args: 20 ad: Device, must be of type AndroidDevice. 21 decorator: String, class name of the decorator to use. 22 Returns: 23 AndroidDevice object. 24 """ 25 26 if not isinstance(ad, AndroidDevice): 27 raise TypeError('Must apply AndroidBluetoothClientDecorator to an ' 28 'AndroidDevice') 29 decorator_module = camel_to_snake(decorator) 30 module = importlib.import_module( 31 'blueberry.decorators.%s' % decorator_module) 32 cls = getattr(module, decorator) 33 ad = cls(ad) 34 35 return ad 36 37 38def camel_to_snake(cls_name): 39 """Utility to convert a class name from camel case to snake case. 40 41 Args: 42 cls_name: string 43 Returns: 44 string 45 """ 46 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', cls_name) 47 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() 48