You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. import re
  2. class InvalidVersionError(Exception):
  3. pass
  4. def get_version(filename):
  5. """
  6. Determines what version the ND2 is.
  7. :param filename: the path (absolute or relative) to the ND2
  8. :type filename: str
  9. """
  10. with open(filename, 'rb') as f:
  11. # the first 16 bytes seem to have no meaning, so we skip them
  12. f.seek(16)
  13. # the next 38 bytes contain the string that we want to parse. Unlike most of the ND2, this is in UTF-8
  14. data = f.read(38).decode("utf8")
  15. return parse_version(data)
  16. def parse_version(data):
  17. """
  18. Parses a string with the version data in it.
  19. :param data: the 19th through 54th byte of the ND2, representing the version
  20. :type data: unicode
  21. """
  22. match = re.search(r"""^ND2 FILE SIGNATURE CHUNK NAME01!Ver(?P<major>\d)\.(?P<minor>\d)$""", data)
  23. if match:
  24. # We haven't seen a lot of ND2s but the ones we have seen conform to this
  25. return int(match.group('major')), int(match.group('minor'))
  26. raise InvalidVersionError("The version of the ND2 you specified is not supported.")