3
# Copyright (c) 2009, Google Inc.
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are
10
# * Redistributions of source code must retain the above copyright
11
# notice, this list of conditions and the following disclaimer.
12
# * Redistributions in binary form must reproduce the above
13
# copyright notice, this list of conditions and the following disclaimer
14
# in the documentation and/or other materials provided with the
16
# * Neither the name of Google Inc. nor the names of its
17
# contributors may be used to endorse or promote products derived from
18
# this software without specific prior written permission.
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
"""Auxiliary module for testing flags.py.
34
The purpose of this module is to define a few flags, and declare some
35
other flags as being important. We want to make sure the unit tests
36
for flags.py involve more than one module.
39
__author__ = 'Alex Salcianu'
41
__pychecker__ = "no-local" # for unittest
43
# We use the name 'flags' internally in this test, for historical reasons.
44
# Don't do this yourself! :-) Just do 'import gflags; FLAGS=gflags.FLAGS; etc'
45
import gflags as flags
48
# For historical reasons we use the name module_bar instead of test_module_bar.
49
import test_module_bar as module_bar
51
DECLARED_KEY_FLAGS = ['tmod_bar_x', 'tmod_bar_z', 'tmod_bar_t']
55
"""Defines a few flags."""
56
module_bar.DefineFlags()
57
# The 'tmod_foo_' prefix (short for 'test_module_foo') ensures that we
58
# have no name clash with existing flags.
59
flags.DEFINE_boolean('tmod_foo_bool', True, 'Boolean flag from module foo.')
60
flags.DEFINE_string('tmod_foo_str', 'default', 'String flag.')
61
flags.DEFINE_integer('tmod_foo_int', 3, 'Sample int flag.')
64
def DeclareKeyFlags():
65
"""Declares a few key flags."""
66
for flag_name in DECLARED_KEY_FLAGS:
67
flags.DECLARE_key_flag(flag_name)
70
def DeclareExtraKeyFlags():
71
"""Declares some extra key flags."""
72
flags.ADOPT_module_key_flags(module_bar)
75
def NamesOfDefinedFlags():
76
"""Returns: list of names of flags defined by this module."""
77
return ['tmod_foo_bool', 'tmod_foo_str', 'tmod_foo_int']
80
def NamesOfDeclaredKeyFlags():
81
"""Returns: list of names of key flags for this module."""
82
return NamesOfDefinedFlags() + DECLARED_KEY_FLAGS
85
def NamesOfDeclaredExtraKeyFlags():
86
"""Returns the list of names of additional key flags for this module.
88
These are the flags that became key for this module only as a result
89
of a call to DeclareExtraKeyFlags() above. I.e., the flags declared
90
by module_bar, that were not already declared as key for this
94
The list of names of additional key flags for this module.
96
names_of_extra_key_flags = list(module_bar.NamesOfDefinedFlags())
97
for flag_name in NamesOfDeclaredKeyFlags():
98
while flag_name in names_of_extra_key_flags:
99
names_of_extra_key_flags.remove(flag_name)
100
return names_of_extra_key_flags
104
"""Deletes the flag definitions done by the above DefineFlags()."""
105
for flag_name in NamesOfDefinedFlags():
106
module_bar.RemoveOneFlag(flag_name)
107
module_bar.RemoveFlags()
111
"""Uses flags._GetCallingModule() to return the name of this module.
113
For checking that _GetCallingModule works as expected.
116
A string, the name of this module.
118
# Calling the protected _GetCallingModule generates a lint warning,
119
# but we do not have any other alternative to test that function.
120
return flags._GetCallingModule()