1#!/usr/bin/env python
2# Copyright 2014 Google Inc. All Rights Reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31"""Auxiliary module for testing gflags.py.
32
33The purpose of this module is to define a few flags.  We want to make
34sure the unit tests for gflags.py involve more than one module.
35"""
36
37__author__ = 'salcianu@google.com (Alex Salcianu)'
38
39import gflags
40from gflags import _helpers
41
42FLAGS = gflags.FLAGS
43
44
45def DefineFlags(flag_values=FLAGS):
46  """Defines some flags.
47
48  Args:
49    flag_values: The FlagValues object we want to register the flags
50      with.
51  """
52  # The 'tmod_bar_' prefix (short for 'test_module_bar') ensures there
53  # is no name clash with the existing flags.
54  gflags.DEFINE_boolean('tmod_bar_x', True, 'Boolean flag.',
55                       flag_values=flag_values)
56  gflags.DEFINE_string('tmod_bar_y', 'default', 'String flag.',
57                      flag_values=flag_values)
58  gflags.DEFINE_boolean('tmod_bar_z', False,
59                       'Another boolean flag from module bar.',
60                       flag_values=flag_values)
61  gflags.DEFINE_integer('tmod_bar_t', 4, 'Sample int flag.',
62                       flag_values=flag_values)
63  gflags.DEFINE_integer('tmod_bar_u', 5, 'Sample int flag.',
64                       flag_values=flag_values)
65  gflags.DEFINE_integer('tmod_bar_v', 6, 'Sample int flag.',
66                       flag_values=flag_values)
67
68
69def RemoveOneFlag(flag_name, flag_values=FLAGS):
70  """Removes the definition of one flag from gflags.FLAGS.
71
72  Note: if the flag is not defined in gflags.FLAGS, this function does
73  not do anything (in particular, it does not raise any exception).
74
75  Motivation: We use this function for cleanup *after* a test: if
76  there was a failure during a test and not all flags were declared,
77  we do not want the cleanup code to crash.
78
79  Args:
80    flag_name: A string, the name of the flag to delete.
81    flag_values: The FlagValues object we remove the flag from.
82  """
83  if flag_name in flag_values.FlagDict():
84    flag_values.__delattr__(flag_name)
85
86
87def NamesOfDefinedFlags():
88  """Returns: List of names of the flags declared in this module."""
89  return ['tmod_bar_x',
90          'tmod_bar_y',
91          'tmod_bar_z',
92          'tmod_bar_t',
93          'tmod_bar_u',
94          'tmod_bar_v']
95
96
97def RemoveFlags(flag_values=FLAGS):
98  """Deletes the flag definitions done by the above DefineFlags().
99
100  Args:
101    flag_values: The FlagValues object we remove the flags from.
102  """
103  for flag_name in NamesOfDefinedFlags():
104    RemoveOneFlag(flag_name, flag_values=flag_values)
105
106
107def GetModuleName():
108  """Uses GetCallingModule() to return the name of this module.
109
110  For checking that _GetCallingModule works as expected.
111
112  Returns:
113    A string, the name of this module.
114  """
115  return _helpers.GetCallingModule()
116
117
118def ExecuteCode(code, global_dict):
119  """Executes some code in a given global environment.
120
121  For testing of _GetCallingModule.
122
123  Args:
124    code: A string, the code to be executed.
125    global_dict: A dictionary, the global environment that code should
126      be executed in.
127  """
128  # Indeed, using exec generates a lint warning.  But some user code
129  # actually uses exec, and we have to test for it ...
130  exec(code, global_dict)  # pylint: disable=exec-used
131
132
133def DisclaimKeyFlags():
134  """Disclaims flags declared in this module."""
135  gflags.DISCLAIM_key_flags()
136