1#!/usr/bin/env python3 2# 3# Copyright 2018 - 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 17 18class ConfigEntryMeta(object): 19 """An object that holds metadata about a type of Configuration Argument. 20 21 Attributes: 22 cli_flags: The CLI flags (e.g. -lp or --logpath for the ACTS logpath). 23 cli_metavar: The metavar for the CLI. See argparse.ArgumentParser. 24 cli_nargs: The number of expected CLI args. See argparse.ArgumentParser. 25 cli_required: True if the entry must be present in the CLI. 26 cli_action: The CLI action, if any. See argparse.ArgumentParser. 27 cli_const: The const value for the CLI. 28 cli_default: The default value for the CLI. 29 type: The type to expect, or None. Used for validation. 30 help: The user documentation for this configuration value. 31 acts_config_key: The key in the ACTS config this value corresponds to. 32 env_var_name: The environment variable that may store this config value. 33 """ 34 35 """The prefix, if any, to use cli_kwargs in parser.add_argument().""" 36 cli_prefix = 'cli_' 37 38 """The attributes corresponding to ArgumentParser.add_argument() kwargs.""" 39 cli_kwarg_attributes = [ 40 'cli_metavar', 41 'cli_nargs', 42 'cli_required', 43 'cli_action', 44 'cli_const', 45 'cli_default', 46 'cli_dest', 47 'type', 48 'help', 49 ] 50 51 @classmethod 52 def attr_to_cli_kwarg(cls, attribute): 53 """Converts an attribute name to the corresponding add_argument() kwarg. 54 55 Args: 56 attribute: The attribute name, preferably one found in 57 cli_kwarg_attributes. 58 59 Returns: 60 The kwarg name. 61 """ 62 kwarg = attribute 63 if attribute.startswith(ConfigEntryMeta.cli_prefix): 64 kwarg = attribute[len(ConfigEntryMeta.cli_prefix):] 65 return kwarg 66 67 def __init__(self, cli_flags=None, cli_metavar=None, cli_nargs=None, 68 cli_required=False, cli_action=None, cli_const=None, 69 cli_default=None, type=None, help=None, acts_config_key=None, 70 env_var_name=None): 71 """Initializes the ConfigEntryMeta object. 72 73 Args: 74 cli_flags: The CLI flags. Can either be a list or a single string. 75 Accessing the cli_flags from this object will always 76 return a list. 77 cli_metavar: The metavar for the CLI. See 78 https://docs.python.org/3/library/argparse.html#metavar 79 cli_nargs: The number of expected CLI args. See 80 https://docs.python.org/3/library/argparse.html#nargs 81 cli_const: The const value for the CLI. See 82 https://docs.python.org/3/library/argparse.html#const 83 cli_default: The const value for the CLI. See 84 https://docs.python.org/3/library/argparse.html#default 85 cli_required: True if the CLI must have this argument. See 86 https://docs.python.org/3/library/argparse.html#required 87 cli_action: The CLI action, if any. See 88 https://docs.python.org/3/library/argparse.html#action 89 type: The type to expect, or None. Used for validation. 90 help: The user documentation for this configuration value. 91 acts_config_key: The key in the ACTS config this value corresponds 92 to. 93 env_var_name: The environment variable that may store this config 94 value. 95 """ 96 if ConfigEntryMeta.__get_type(cli_flags) is str: 97 self.cli_flags = [cli_flags] 98 else: 99 self.cli_flags = cli_flags 100 self.cli_metavar = cli_metavar 101 self.cli_nargs = cli_nargs 102 self.cli_required = cli_required 103 self.cli_action = cli_action 104 self.cli_const = cli_const 105 self.cli_default = cli_default 106 self.type = type 107 self.help = help 108 self.acts_config_key = acts_config_key 109 self.cli_dest = acts_config_key 110 self.env_var_name = env_var_name 111 112 @staticmethod 113 def __get_type(obj): 114 """A wrapper function to use the type() keyword when shadowed.""" 115 return type(obj) 116