]> git.decadent.org.uk Git - dak.git/blob - daklib/formats.py
merge from master
[dak.git] / daklib / formats.py
1 from regexes import re_verwithext
2 from dak_exceptions import UnknownFormatError
3
4 def parse_format(txt):
5     """
6     Parse a .changes Format string into a tuple representation for easy
7     comparison.
8
9     >>> parse_format('1.0')
10     (1, 0)
11     >>> parse_format('8.4 (hardy)')
12     (8, 4, 'hardy')
13
14     If the format doesn't match these forms, raises UnknownFormatError.
15     """
16
17     format = re_verwithext.search(txt)
18
19     if format is None:
20         raise UnknownFormatError, txt
21
22     format = format.groups()
23
24     if format[1] is None:
25         format = int(float(format[0])), 0, format[2]
26     else:
27         format = int(format[0]), int(format[1]), format[2]
28
29     if format[2] is None:
30         format = format[:2]
31
32     return format
33
34 def validate_changes_format(format, field):
35     """
36     Validate a tuple-representation of a .changes Format: field. Raises
37     UnknownFormatError if the field is invalid, otherwise return type is
38     undefined.
39     """
40
41     if (format < (1, 5) or format > (1, 8)):
42         raise UnknownFormatError, repr(format)
43
44     if field != 'files' and format < (1, 8):
45         raise UnknownFormatError, repr(format)