Coverage for daklib/formats.py: 89%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-01-04 16:18 +0000

1"""Helper functions for the various changes formats 

2 

3@contact: Debian FTPMaster <ftpmaster@debian.org> 

4@copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org> 

5@copyright: 2009 Chris Lamb <lamby@debian.org> 

6@license: GNU General Public License version 2 or later 

7""" 

8 

9# This program is free software; you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation; either version 2 of the License, or 

12# (at your option) any later version. 

13 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18 

19# You should have received a copy of the GNU General Public License 

20# along with this program; if not, write to the Free Software 

21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

22 

23################################################################################ 

24 

25# <mhy> !!!!11111iiiiiioneoneoneone 

26# <dak> mhy: Error: "!!!11111iiiiiioneoneoneone" is not a valid command. 

27# <mhy> dak: oh shut up 

28# <dak> mhy: Error: "oh" is not a valid command. 

29 

30################################################################################ 

31 

32from .dak_exceptions import UnknownFormatError 

33from .regexes import re_verwithext 

34 

35 

36def parse_format(txt: str) -> tuple[int, int] | tuple[int, int, str]: 

37 """ 

38 Parse a .changes Format string into a tuple representation for easy 

39 comparison. 

40 

41 >>> parse_format('1.0') 

42 (1, 0) 

43 >>> parse_format('8.4 (hardy)') 

44 (8, 4, 'hardy') 

45 

46 If the format doesn't match these forms, raises UnknownFormatError. 

47 

48 :param txt: Format string to parse 

49 :return: Parsed format 

50 

51 :raises UnknownFormatError: Unknown Format: line 

52 """ 

53 

54 match = re_verwithext.search(txt) 

55 if match is None: 

56 raise UnknownFormatError(txt) 

57 

58 groups = match.groups() 

59 

60 format: tuple[int, int] | tuple[int, int, str] 

61 if groups[1] is None: 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true

62 format = int(float(groups[0])), 0, groups[2] 

63 else: 

64 format = int(groups[0]), int(groups[1]), groups[2] 

65 

66 if groups[2] is None: 66 ↛ 69line 66 didn't jump to line 69 because the condition on line 66 was always true

67 format = format[:2] 

68 

69 return format 

70 

71 

72def validate_changes_format( 

73 format: tuple[int, int] | tuple[int, int, str], field: str 

74) -> None: 

75 """ 

76 Validate a tuple-representation of a .changes Format: field. Raises 

77 UnknownFormatError if the field is invalid, otherwise return type is 

78 undefined. 

79 

80 :raises UnknownFormatError: if the field is invalid 

81 """ 

82 

83 if format < (1, 5) or format > (1, 8): 

84 raise UnknownFormatError(repr(format)) 

85 

86 if field != "files" and format < (1, 8): 

87 raise UnknownFormatError(repr(format))