formats.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """ Helper functions for the various changes formats
  2. @contact: Debian FTPMaster <ftpmaster@debian.org>
  3. @copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org>
  4. @copyright: 2009 Chris Lamb <lamby@debian.org>
  5. @license: GNU General Public License version 2 or later
  6. """
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. ################################################################################
  19. # <mhy> !!!!11111iiiiiioneoneoneone
  20. # <dak> mhy: Error: "!!!11111iiiiiioneoneoneone" is not a valid command.
  21. # <mhy> dak: oh shut up
  22. # <dak> mhy: Error: "oh" is not a valid command.
  23. ################################################################################
  24. from .regexes import re_verwithext
  25. from .dak_exceptions import UnknownFormatError
  26. def parse_format(txt: str) -> tuple:
  27. """
  28. Parse a .changes Format string into a tuple representation for easy
  29. comparison.
  30. >>> parse_format('1.0')
  31. (1, 0)
  32. >>> parse_format('8.4 (hardy)')
  33. (8, 4, 'hardy')
  34. If the format doesn't match these forms, raises UnknownFormatError.
  35. :param txt: Format string to parse
  36. :return: Parsed format
  37. :raises UnknownFormatError: Unknown Format: line
  38. """
  39. format = re_verwithext.search(txt)
  40. if format is None:
  41. raise UnknownFormatError(txt)
  42. format = format.groups()
  43. if format[1] is None:
  44. format = int(float(format[0])), 0, format[2]
  45. else:
  46. format = int(format[0]), int(format[1]), format[2]
  47. if format[2] is None:
  48. format = format[:2]
  49. return format
  50. def validate_changes_format(format: tuple[int], field: str) -> None:
  51. """
  52. Validate a tuple-representation of a .changes Format: field. Raises
  53. UnknownFormatError if the field is invalid, otherwise return type is
  54. undefined.
  55. :raises UnknownFormatError: if the field is invalid
  56. """
  57. if (format < (1, 5) or format > (1, 8)):
  58. raise UnknownFormatError(repr(format))
  59. if field != 'files' and format < (1, 8):
  60. raise UnknownFormatError(repr(format))