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