Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/absl_py-2.0.0-py3.8.egg/absl/flags/_exceptions.py: 96%

28 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:13 +0000

1# Copyright 2017 The Abseil Authors. 

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"""Exception classes in ABSL flags library. 

16 

17Do NOT import this module directly. Import the flags package and use the 

18aliases defined at the package level instead. 

19""" 

20 

21import sys 

22 

23from absl.flags import _helpers 

24 

25 

26_helpers.disclaim_module_ids.add(id(sys.modules[__name__])) 

27 

28 

29class Error(Exception): 

30 """The base class for all flags errors.""" 

31 

32 

33class CantOpenFlagFileError(Error): 

34 """Raised when flagfile fails to open. 

35 

36 E.g. the file doesn't exist, or has wrong permissions. 

37 """ 

38 

39 

40class DuplicateFlagError(Error): 

41 """Raised if there is a flag naming conflict.""" 

42 

43 @classmethod 

44 def from_flag(cls, flagname, flag_values, other_flag_values=None): 

45 """Creates a DuplicateFlagError by providing flag name and values. 

46 

47 Args: 

48 flagname: str, the name of the flag being redefined. 

49 flag_values: :class:`FlagValues`, the FlagValues instance containing the 

50 first definition of flagname. 

51 other_flag_values: :class:`FlagValues`, if it is not None, it should be 

52 the FlagValues object where the second definition of flagname occurs. 

53 If it is None, we assume that we're being called when attempting to 

54 create the flag a second time, and we use the module calling this one 

55 as the source of the second definition. 

56 

57 Returns: 

58 An instance of DuplicateFlagError. 

59 """ 

60 first_module = flag_values.find_module_defining_flag( 

61 flagname, default='<unknown>') 

62 if other_flag_values is None: 

63 second_module = _helpers.get_calling_module() 

64 else: 

65 second_module = other_flag_values.find_module_defining_flag( 

66 flagname, default='<unknown>') 

67 flag_summary = flag_values[flagname].help 

68 msg = ("The flag '%s' is defined twice. First from %s, Second from %s. " 

69 "Description from first occurrence: %s") % ( 

70 flagname, first_module, second_module, flag_summary) 

71 return cls(msg) 

72 

73 

74class IllegalFlagValueError(Error): 

75 """Raised when the flag command line argument is illegal.""" 

76 

77 

78class UnrecognizedFlagError(Error): 

79 """Raised when a flag is unrecognized. 

80 

81 Attributes: 

82 flagname: str, the name of the unrecognized flag. 

83 flagvalue: The value of the flag, empty if the flag is not defined. 

84 """ 

85 

86 def __init__(self, flagname, flagvalue='', suggestions=None): 

87 self.flagname = flagname 

88 self.flagvalue = flagvalue 

89 if suggestions: 

90 # Space before the question mark is intentional to not include it in the 

91 # selection when copy-pasting the suggestion from (some) terminals. 

92 tip = '. Did you mean: %s ?' % ', '.join(suggestions) 

93 else: 

94 tip = '' 

95 super(UnrecognizedFlagError, self).__init__( 

96 'Unknown command line flag \'%s\'%s' % (flagname, tip)) 

97 

98 

99class UnparsedFlagAccessError(Error): 

100 """Raised when accessing the flag value from unparsed :class:`FlagValues`.""" 

101 

102 

103class ValidationError(Error): 

104 """Raised when flag validator constraint is not satisfied.""" 

105 

106 

107class FlagNameConflictsWithMethodError(Error): 

108 """Raised when a flag name conflicts with :class:`FlagValues` methods."""