1# Copyright 2017 The Abseil Authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Auxiliary module for testing flags.py. 16 17The purpose of this module is to define a few flags, and declare some 18other flags as being important. We want to make sure the unit tests 19for flags.py involve more than one module. 20""" 21 22from absl import flags 23from absl.flags import _helpers 24from absl.flags.tests import module_bar 25 26FLAGS = flags.FLAGS 27 28 29DECLARED_KEY_FLAGS = ['tmod_bar_x', 'tmod_bar_z', 'tmod_bar_t', 30 # Special (not user-defined) flag: 31 'flagfile'] 32 33 34def define_flags(flag_values=FLAGS): 35 """Defines a few flags.""" 36 module_bar.define_flags(flag_values=flag_values) 37 # The 'tmod_foo_' prefix (short for 'test_module_foo') ensures that we 38 # have no name clash with existing flags. 39 flags.DEFINE_boolean('tmod_foo_bool', True, 'Boolean flag from module foo.', 40 flag_values=flag_values) 41 flags.DEFINE_string('tmod_foo_str', 'default', 'String flag.', 42 flag_values=flag_values) 43 flags.DEFINE_integer('tmod_foo_int', 3, 'Sample int flag.', 44 flag_values=flag_values) 45 46 47def declare_key_flags(flag_values=FLAGS): 48 """Declares a few key flags.""" 49 for flag_name in DECLARED_KEY_FLAGS: 50 flags.declare_key_flag(flag_name, flag_values=flag_values) 51 52 53def declare_extra_key_flags(flag_values=FLAGS): 54 """Declares some extra key flags.""" 55 flags.adopt_module_key_flags(module_bar, flag_values=flag_values) 56 57 58def names_of_defined_flags(): 59 """Returns: list of names of flags defined by this module.""" 60 return ['tmod_foo_bool', 'tmod_foo_str', 'tmod_foo_int'] 61 62 63def names_of_declared_key_flags(): 64 """Returns: list of names of key flags for this module.""" 65 return names_of_defined_flags() + DECLARED_KEY_FLAGS 66 67 68def names_of_declared_extra_key_flags(): 69 """Returns the list of names of additional key flags for this module. 70 71 These are the flags that became key for this module only as a result 72 of a call to declare_extra_key_flags() above. I.e., the flags declared 73 by module_bar, that were not already declared as key for this 74 module. 75 76 Returns: 77 The list of names of additional key flags for this module. 78 """ 79 names_of_extra_key_flags = list(module_bar.names_of_defined_flags()) 80 for flag_name in names_of_declared_key_flags(): 81 while flag_name in names_of_extra_key_flags: 82 names_of_extra_key_flags.remove(flag_name) 83 return names_of_extra_key_flags 84 85 86def remove_flags(flag_values=FLAGS): 87 """Deletes the flag definitions done by the above define_flags().""" 88 for flag_name in names_of_defined_flags(): 89 module_bar.remove_one_flag(flag_name, flag_values=flag_values) 90 module_bar.remove_flags(flag_values=flag_values) 91 92 93def get_module_name(): 94 """Uses get_calling_module() to return the name of this module. 95 96 For checking that _get_calling_module works as expected. 97 98 Returns: 99 A string, the name of this module. 100 """ 101 return _helpers.get_calling_module() 102 103 104def duplicate_flags(flagnames=None): 105 """Returns a new FlagValues object with the requested flagnames. 106 107 Used to test DuplicateFlagError detection. 108 109 Args: 110 flagnames: str, A list of flag names to create. 111 112 Returns: 113 A FlagValues object with one boolean flag for each name in flagnames. 114 """ 115 flag_values = flags.FlagValues() 116 for name in flagnames: 117 flags.DEFINE_boolean(name, False, 'Flag named %s' % (name,), 118 flag_values=flag_values) 119 return flag_values 120 121 122def define_bar_flags(flag_values=FLAGS): 123 """Defines flags from module_bar.""" 124 module_bar.define_flags(flag_values) 125