xref: /aosp_15_r20/external/autotest/client/common_lib/metrics_mock_class.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# pylint: disable=missing-docstring
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
7from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10
11import six
12
13
14def any_call(*args, **kwargs):
15    """An empty method to handle any call.
16    """
17    pass
18
19
20def decorate(f):
21    """A noop decorator.
22    """
23    return f
24
25
26def decorate_wrapper(f):
27    """Wrapper of the noop decorator.
28
29    Calling this method with any args will return the noop decorator function.
30    """
31    return decorate
32
33
34class mock_class_type(type):
35    """Type class for the mock class to handle any class methods."""
36
37    def __getattr__(self, attr):
38        # This is to support decorators like "@metrics.SecondsTimerDecorator"
39        # In this case, the call returns a function which returns a noop
40        # decorator function ("decorate").
41        if 'Decorator' in attr:
42            return decorate_wrapper
43        else:
44            return mock_class_base
45
46
47class mock_class_base(six.with_metaclass(mock_class_type, object)):
48    """Base class for a mock es class."""
49
50    def __init__(self, *args, **kwargs):
51        pass
52
53
54    def __getattribute__(self, name):
55
56        # TODO(dshi): Remove this method after all reference of timer.get_client
57        # is removed.
58        def get_client(*args, **kwargs):
59            return self
60
61        # get_client is to support call like "timer.get_client", which returns
62        # a class supporting Context when being called.
63        if name == 'get_client':
64            return get_client
65        elif name == 'indices':
66            return mock_class_base()
67
68        return any_call
69
70
71    def __enter__(self, *args, **kwargs):
72        """Method to support Context class."""
73        return self
74
75
76    def __exit__(self, *args, **kwargs):
77        """Method to support Context class."""
78
79
80    def __getitem__(self, key):
81        """Method to override __getitem__."""
82        return self
83
84
85    def __setitem__(self, key, value):
86        """Method to override __setitem__."""
87