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# ============================================================================== 15 16"""Import router for absl.flags. See https://github.com/abseil/abseil-py.""" 17import logging as _logging 18import sys as _sys 19 20# go/tf-wildcard-import 21 22from absl.flags import * # pylint: disable=wildcard-import 23 24from tensorflow.python.util import tf_decorator 25 26 27# Since we wrap absl.flags DEFINE functions, we need to declare this module 28# does not affect key flags. 29disclaim_key_flags() # pylint: disable=undefined-variable 30 31 32_RENAMED_ARGUMENTS = { 33 'flag_name': 'name', 34 'default_value': 'default', 35 'docstring': 'help', 36} 37 38 39def _wrap_define_function(original_function): 40 """Wraps absl.flags's define functions so tf.flags accepts old names.""" 41 42 def wrapper(*args, **kwargs): 43 """Wrapper function that turns old keyword names to new ones.""" 44 has_old_names = False 45 for old_name, new_name in _RENAMED_ARGUMENTS.items(): 46 if old_name in kwargs: 47 has_old_names = True 48 value = kwargs.pop(old_name) 49 kwargs[new_name] = value 50 if has_old_names: 51 _logging.warning( 52 'Use of the keyword argument names (flag_name, default_value, ' 53 'docstring) is deprecated, please use (name, default, help) instead.') 54 return original_function(*args, **kwargs) 55 56 return tf_decorator.make_decorator(original_function, wrapper) 57 58 59class _FlagValuesWrapper: 60 """Wrapper class for absl.flags.FLAGS. 61 62 The difference is that tf.flags.FLAGS implicitly parses flags with sys.argv 63 when accessing the FLAGS values before it's explicitly parsed, 64 while absl.flags.FLAGS raises an exception. 65 """ 66 67 def __init__(self, flags_object): 68 self.__dict__['__wrapped'] = flags_object 69 70 def __getattribute__(self, name): 71 if name == '__dict__': 72 return super().__getattribute__(name) 73 return self.__dict__['__wrapped'].__getattribute__(name) 74 75 def __getattr__(self, name): 76 wrapped = self.__dict__['__wrapped'] 77 # To maintain backwards compatibility, implicitly parse flags when reading 78 # a flag. 79 if not wrapped.is_parsed(): 80 wrapped(_sys.argv) 81 return wrapped.__getattr__(name) 82 83 def __setattr__(self, name, value): 84 return self.__dict__['__wrapped'].__setattr__(name, value) 85 86 def __delattr__(self, name): 87 return self.__dict__['__wrapped'].__delattr__(name) 88 89 def __dir__(self): 90 return self.__dict__['__wrapped'].__dir__() 91 92 def __getitem__(self, name): 93 return self.__dict__['__wrapped'].__getitem__(name) 94 95 def __setitem__(self, name, flag): 96 return self.__dict__['__wrapped'].__setitem__(name, flag) 97 98 def __len__(self): 99 return self.__dict__['__wrapped'].__len__() 100 101 def __iter__(self): 102 return self.__dict__['__wrapped'].__iter__() 103 104 def __str__(self): 105 return self.__dict__['__wrapped'].__str__() 106 107 def __call__(self, *args, **kwargs): 108 return self.__dict__['__wrapped'].__call__(*args, **kwargs) 109 110 111# pylint: disable=invalid-name,used-before-assignment 112# absl.flags APIs use `default` as the name of the default value argument. 113# Allow the following functions continue to accept `default_value`. 114DEFINE_string = _wrap_define_function(DEFINE_string) 115DEFINE_boolean = _wrap_define_function(DEFINE_boolean) 116DEFINE_bool = DEFINE_boolean 117DEFINE_float = _wrap_define_function(DEFINE_float) 118DEFINE_integer = _wrap_define_function(DEFINE_integer) 119# pylint: enable=invalid-name,used-before-assignment 120 121FLAGS = _FlagValuesWrapper(FLAGS) # pylint: disable=used-before-assignment 122