1# Copyright 2016 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"""TensorFlow Debugger (tfdbg) User-Interface Factory.""" 16import copy 17 18 19SUPPORTED_UI_TYPES = ["curses", "readline"] 20 21 22def get_ui(ui_type, 23 on_ui_exit=None, 24 available_ui_types=None, 25 config=None): 26 """Create a `base_ui.BaseUI` subtype. 27 28 This factory method attempts to fallback to other available ui_types on 29 ImportError. For example, if `ui_type` is `curses`, but `curses` cannot be 30 imported properly, e.g., on Windows, will fallback to `readline`. 31 32 Args: 33 ui_type: (`str`) requested UI type. Currently supported: 34 (curses | readline) 35 on_ui_exit: (`Callable`) the callback to be called when the UI exits. 36 available_ui_types: (`None` or `list` of `str`) Manually-set available 37 ui_types. 38 config: An instance of `cli_config.CLIConfig()` carrying user-facing 39 configurations. 40 41 Returns: 42 A `base_ui.BaseUI` subtype object. 43 44 Raises: 45 ValueError: on invalid ui_type or on exhausting or fallback ui_types. 46 """ 47 if available_ui_types is None: 48 available_ui_types = copy.deepcopy(SUPPORTED_UI_TYPES) 49 50 if ui_type and (ui_type not in available_ui_types): 51 raise ValueError("Invalid ui_type: '%s'" % ui_type) 52 53 try: 54 # pylint: disable=g-import-not-at-top 55 if not ui_type or ui_type == "curses": 56 from tensorflow.python.debug.cli import curses_ui 57 return curses_ui.CursesUI(on_ui_exit=on_ui_exit, config=config) 58 elif ui_type == "readline": 59 from tensorflow.python.debug.cli import readline_ui 60 return readline_ui.ReadlineUI(on_ui_exit=on_ui_exit, config=config) 61 # pylint: enable=g-import-not-at-top 62 except ImportError: 63 available_ui_types.remove(ui_type) 64 if not available_ui_types: 65 raise ValueError("Exhausted all fallback ui_types.") 66 return get_ui(available_ui_types[0], 67 available_ui_types=available_ui_types) 68