1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import logging
17import time
18
19
20def GetTimestamp():
21    """Returns the current UTC time (unit: microseconds)."""
22    return int(time.time() * 1000000)
23
24
25class Feature(object):
26    """Configuration object for a feature.
27
28    Stores configuration parameters and metadata.
29
30    Attributes:
31        enabled: boolean, True if the feature is enabled, False otherwise
32    """
33
34    enabled = False
35
36    def ParseParameters(self,
37                        toggle_param_name=None,
38                        required_param_names=[],
39                        optional_param_names=[],
40                        user_params={}):
41        """Creates a feature configuration object.
42
43        Args:
44            toggle_param_name: String, The name of the parameter used to toggle the feature
45            required_param_names: list, The list of parameter names that are required
46            optional_param_names: list, The list of parameter names that are optional
47        """
48        self.enabled = False
49        if toggle_param_name:
50            if toggle_param_name not in user_params:
51                logging.debug("Missing toggle parameter in configuration: %s",
52                              toggle_param_name)
53                return
54            if not user_params[toggle_param_name]:
55                logging.debug("Feature disabled in configuration: %s=False",
56                              toggle_param_name)
57                return
58
59        for name in required_param_names:
60            if name not in user_params:
61                return
62            else:
63                setattr(self, name, user_params[name])
64
65        self.enabled = True
66
67        for name in optional_param_names:
68            if name in user_params:
69                setattr(self, name, user_params[name])
70