1#pylint: disable-msg=C0111 2 3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7def any_call(*args, **kwargs): 8 """An empty method to handle any call. 9 """ 10 pass 11 12 13def decorate(f): 14 """A noop decorator. 15 """ 16 return f 17 18 19def decorate_wrapper(f): 20 """Wrapper of the noop decorator. 21 22 Calling this method with any args will return the noop decorator function. 23 """ 24 return decorate 25 26 27class mock_class_type(type): 28 """Type class for the mock class to handle any class methods.""" 29 30 def __getattr__(self, attr): 31 # This is to support decorators like "@metrics.SecondsTimerDecorator" 32 # In this case, the call returns a function which returns a noop 33 # decorator function ("decorate"). 34 if 'Decorator' in attr: 35 return decorate_wrapper 36 else: 37 return mock_class_base 38 39 40class mock_class_base(object): 41 """Base class for a mock statsd/es class.""" 42 43 __metaclass__ = mock_class_type 44 45 def __init__(self, *args, **kwargs): 46 pass 47 48 49 def __getattribute__(self, name): 50 51 # TODO(dshi): Remove this method after all reference of timer.get_client 52 # is removed. 53 def get_client(*args, **kwargs): 54 return self 55 56 # get_client is to support call like "timer.get_client", which returns 57 # a class supporting Context when being called. 58 if name == 'get_client': 59 return get_client 60 elif name == 'indices': 61 return mock_class_base() 62 63 return any_call 64 65 66 def __enter__(self, *args, **kwargs): 67 """Method to support Context class.""" 68 return self 69 70 71 def __exit__(self, *args, **kwargs): 72 """Method to support Context class.""" 73 74 75 def __getitem__(self, key): 76 """Method to override __getitem__.""" 77 return self 78 79 80 def __setitem__(self, key, value): 81 """Method to override __setitem__.""" 82