Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/flags.py: 57%
54 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
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# ==============================================================================
16"""Import router for absl.flags.
18See https://github.com/abseil/abseil-py.
19"""
21import logging as _logging
22import sys as _sys
24# go/tf-wildcard-import
25from absl.flags import * # pylint: disable=wildcard-import
28# Since we wrap absl.flags DEFINE functions, we need to declare this module
29# does not affect key flags.
30disclaim_key_flags() # pylint: disable=undefined-variable
33_RENAMED_ARGUMENTS = {
34 "flag_name": "name",
35 "default_value": "default",
36 "docstring": "help",
37}
40def _wrap_define_function(original_function):
41 """Wraps absl.flags's define functions so tf.flags accepts old names."""
43 def wrapper(*args, **kwargs):
44 """Wrapper function that turns old keyword names to new ones."""
45 has_old_names = False
46 for old_name, new_name in __RENAMED_ARGUMENTS.items():
47 if old_name in kwargs:
48 has_old_names = True
49 value = kwargs.pop(old_name)
50 kwargs[new_name] = value
51 if has_old_names:
52 _logging.warning(
53 "Use of the keyword argument names (flag_name, default_value, "
54 "docstring) is deprecated, please use (name, default, help) instead."
55 )
56 return original_function(*args, **kwargs)
58 return wrapper
61class _FlagValuesWrapper:
62 """Wrapper class for absl.flags.FLAGS.
64 The difference is that tf.compat.v1.flags.FLAGS implicitly parses
65 flags with sys.argv when accessing the FLAGS values before it's
66 explicitly parsed, while absl.flags.FLAGS raises an exception.
67 """
69 def __init__(self, flags_object):
70 self.__dict__["__wrapped"] = flags_object
72 def __getattribute__(self, name):
73 if name == "__dict__":
74 return super().__getattribute__(name)
75 return self.__dict__["__wrapped"].__getattribute__(name)
77 def __getattr__(self, name):
78 wrapped = self.__dict__["__wrapped"]
79 # To maintain backwards compatibility, implicitly parse flags when reading
80 # a flag.
81 if not wrapped.is_parsed():
82 wrapped(_sys.argv)
83 return wrapped.__getattr__(name)
85 def __setattr__(self, name, value):
86 return self.__dict__["__wrapped"].__setattr__(name, value)
88 def __delattr__(self, name):
89 return self.__dict__["__wrapped"].__delattr__(name)
91 def __dir__(self):
92 return self.__dict__["__wrapped"].__dir__()
94 def __getitem__(self, name):
95 return self.__dict__["__wrapped"].__getitem__(name)
97 def __setitem__(self, name, flag):
98 return self.__dict__["__wrapped"].__setitem__(name, flag)
100 def __len__(self):
101 return self.__dict__["__wrapped"].__len__()
103 def __iter__(self):
104 return self.__dict__["__wrapped"].__iter__()
106 def __str__(self):
107 return self.__dict__["__wrapped"].__str__()
109 def __call__(self, *args, **kwargs):
110 return self.__dict__["__wrapped"].__call__(*args, **kwargs)
113# pylint: disable=invalid-name,used-before-assignment
114# absl.flags APIs use `default` as the name of the default value argument.
115# Allow the following functions continue to accept `default_value`.
116DEFINE_string = _wrap_define_function(DEFINE_string)
117DEFINE_boolean = _wrap_define_function(DEFINE_boolean)
118DEFINE_bool = DEFINE_boolean
119DEFINE_float = _wrap_define_function(DEFINE_float)
120DEFINE_integer = _wrap_define_function(DEFINE_integer)
121# pylint: enable=invalid-name,used-before-assignment
123FLAGS = _FlagValuesWrapper(FLAGS) # pylint: disable=used-before-assignment