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 # 16 import logging 17 import time 18 19 def GetTimestamp(): 20 """Returns the current UTC time (unit: microseconds).""" 21 return int(time.time() * 1000000) 22 23 class Feature(object): 24 """Configuration object for a feature. 25 26 Stores configuration parameters and metadata. 27 28 Attributes: 29 enabled: boolean, True if the feature is enabled, False otherwise 30 """ 31 32 enabled = False 33 34 def ParseParameters(self, toggle_param_name=None, required_param_names=[], 35 optional_param_names=[], user_params={}): 36 """Creates a feature configuration object. 37 38 Args: 39 toggle_param_name: String, The name of the parameter used to toggle the feature 40 required_param_names: list, The list of parameter names that are required 41 optional_param_names: list, The list of parameter names that are optional 42 """ 43 self.enabled = False 44 if toggle_param_name: 45 if toggle_param_name not in user_params: 46 logging.info("Missing toggle parameter in configuration: %s", toggle_param_name) 47 return 48 if not user_params[toggle_param_name]: 49 logging.info("Feature disabled in configuration: %s=False", toggle_param_name) 50 return 51 52 for name in required_param_names: 53 if name not in user_params: 54 return 55 else: 56 setattr(self, name, user_params[name]) 57 58 self.enabled = True 59 60 for name in optional_param_names: 61 if name in user_params: 62 setattr(self, name, user_params[name]) 63