Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/app.py: 32%

59 statements  

« 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# ============================================================================== 

15 

16"""Generic entry point script.""" 

17 

18import errno as _errno 

19import sys as _sys 

20 

21from . import flags 

22 

23 

24def _usage(shorthelp): 

25 """Writes __main__'s docstring to stdout with some help text. 

26 

27 Args: 

28 shorthelp: bool, if True, prints only flags from the main module, 

29 rather than all flags. 

30 """ 

31 doc = _sys.modules["__main__"].__doc__ 

32 if not doc: 

33 doc = "\nUSAGE: %s [flags]\n" % _sys.argv[0] 

34 doc = flags.text_wrap(doc, indent=" ", firstline_indent="") 

35 else: 

36 # Replace all '%s' with sys.argv[0], and all '%%' with '%'. 

37 num_specifiers = doc.count("%") - 2 * doc.count("%%") 

38 try: 

39 doc %= (_sys.argv[0],) * num_specifiers 

40 except (OverflowError, TypeError, ValueError): 

41 # Just display the docstring as-is. 

42 pass 

43 if shorthelp: 

44 flag_str = flags.FLAGS.main_module_help() 

45 else: 

46 flag_str = str(flags.FLAGS) 

47 try: 

48 _sys.stdout.write(doc) 

49 if flag_str: 

50 _sys.stdout.write("\nflags:\n") 

51 _sys.stdout.write(flag_str) 

52 _sys.stdout.write("\n") 

53 except IOError as e: 

54 # We avoid printing a huge backtrace if we get EPIPE, because 

55 # "foo.par --help | less" is a frequent use case. 

56 if e.errno != _errno.EPIPE: 

57 raise 

58 

59 

60class _HelpFlag(flags.BooleanFlag): 

61 """Special boolean flag that displays usage and raises SystemExit.""" 

62 

63 NAME = "help" 

64 SHORT_NAME = "h" 

65 

66 def __init__(self): 

67 super().__init__( 

68 self.NAME, False, "show this help", short_name=self.SHORT_NAME 

69 ) 

70 

71 def parse(self, arg): 

72 if arg: 

73 _usage(shorthelp=True) 

74 print() 

75 print("Try --helpfull to get a list of all flags.") 

76 _sys.exit(1) 

77 

78 

79class _HelpshortFlag(_HelpFlag): 

80 """--helpshort is an alias for --help.""" 

81 

82 NAME = "helpshort" 

83 SHORT_NAME = None 

84 

85 

86class _HelpfullFlag(flags.BooleanFlag): 

87 """Display help for flags in main module and all dependent modules.""" 

88 

89 def __init__(self): 

90 super().__init__("helpfull", False, "show full help") 

91 

92 def parse(self, arg): 

93 if arg: 

94 _usage(shorthelp=False) 

95 _sys.exit(1) 

96 

97 

98_define_help_flags_called = False 

99 

100 

101def _define_help_flags(): 

102 global _define_help_flags_called 

103 if not _define_help_flags_called: 

104 flags.DEFINE_flag(_HelpFlag()) 

105 flags.DEFINE_flag(_HelpfullFlag()) 

106 flags.DEFINE_flag(_HelpshortFlag()) 

107 _define_help_flags_called = True 

108 

109 

110# @tf_export('app.run') 

111def run(main=None, argv=None): 

112 """Runs the program with an optional 'main' function and 'argv' list.""" 

113 

114 # Define help flags. 

115 _define_help_flags() 

116 

117 # Parse known flags. 

118 argv = flags.FLAGS(_sys.argv if argv is None else argv, known_only=True) 

119 

120 main = main or _sys.modules["__main__"].main 

121 

122 # Call the main function, passing through any arguments 

123 # to the final program. 

124 _sys.exit(main(argv))