1#!/usr/bin/env python3.4 2# 3# Copyright 2016 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import enum 18 19"""This module has the global key values that are used across framework 20modules. 21""" 22class Config(enum.Enum): 23 """Enum values for test config related lookups. 24 """ 25 # Keys used to look up values from test config files. 26 # These keys define the wording of test configs and their internal 27 # references. 28 key_log_path = "logpath" 29 key_testbed = "testbed" 30 key_testbed_name = "name" 31 key_test_paths = "testpaths" 32 key_port = "Port" 33 key_address = "Address" 34 # Config names for controllers packaged in ACTS. 35 key_android_device = "AndroidDevice" 36 key_native_android_device = "NativeAndroidDevice" 37 key_access_point = "AP" 38 key_attenuator = "Attenuator" 39 key_iperf_server = "IPerfServer" 40 key_monsoon = "Monsoon" 41 key_sniffer = "Sniffer" 42 # Internal keys, used internally, not exposed to user's config files. 43 ikey_user_param = "user_params" 44 ikey_testbed_name = "testbed_name" 45 ikey_logger = "log" 46 ikey_logpath = "log_path" 47 ikey_cli_args = "cli_args" 48 # module name of controllers packaged in ACTS. 49 m_key_monsoon = "monsoon" 50 m_key_android_device = "android_device" 51 m_key_native_android_device = "native_android_device" 52 m_key_access_point = "access_point" 53 m_key_attenuator = "attenuator" 54 m_key_iperf_server = "iperf_server" 55 m_key_sniffer = "sniffer" 56 57 # A list of keys whose values in configs should not be passed to test 58 # classes without unpacking first. 59 reserved_keys = (key_testbed, key_log_path, key_test_paths) 60 61 # Controller names packaged with ACTS. 62 builtin_controller_names = [ 63 key_android_device, 64 key_native_android_device, 65 key_access_point, 66 key_attenuator, 67 key_iperf_server, 68 key_monsoon, 69 key_sniffer 70 ] 71 72def get_name_by_value(value): 73 for name, member in Config.__members__.items(): 74 if member.value == value: 75 return name 76 return None 77 78def get_internal_value(external_value): 79 """Translates the value of an external key to the value of its 80 corresponding internal key. 81 """ 82 return value_to_value(external_value, "i%s") 83 84def get_module_name(name_in_config): 85 """Translates the name of a controller in config file to its module name. 86 """ 87 return value_to_value(name_in_config, "m_%s") 88 89def value_to_value(ref_value, pattern): 90 """Translates the value of a key to the value of its corresponding key. The 91 corresponding key is chosen based on the variable name pattern. 92 """ 93 ref_key_name = get_name_by_value(ref_value) 94 if not ref_key_name: 95 return None 96 target_key_name = pattern % ref_key_name 97 try: 98 return getattr(Config, target_key_name).value 99 except AttributeError: 100 return None 101