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