cmus.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import sys
  4. def _int_from_bytes_sys(data_bytes):
  5. return int.from_bytes(data_bytes, byteorder=sys.byteorder)
  6. class Track:
  7. RESERVED_PAD_REGEX = rb'\xff{16,}'
  8. STRING_TERMINATOR = b'\x00'
  9. def __init__(self, cache_size, cache_bytes):
  10. """
  11. struct cache_entry {
  12. // size of this struct including size itself
  13. uint32_t size;
  14. int32_t play_count;
  15. int64_t mtime;
  16. int32_t duration;
  17. int32_t bitrate;
  18. int32_t bpm;
  19. uint8_t _reserved[CACHE_ENTRY_RESERVED_SIZE];
  20. // filename, codec, codec_profile and N * (key, val)
  21. char strings[];
  22. };
  23. """
  24. assert len(cache_bytes) + 4 == cache_size
  25. self._play_count = _int_from_bytes_sys(cache_bytes[0:4])
  26. # self._mtime = _int_from_bytes_sys(cache_bytes[4:12])
  27. # self._duration_seconds = _int_from_bytes_sys(cache_bytes[12:16])
  28. # self._bitrate = _int_from_bytes_sys(cache_bytes[16:20])
  29. # self._bpm = _int_from_bytes_sys(cache_bytes[20:24])
  30. strings = re.split(self.RESERVED_PAD_REGEX, cache_bytes)[1] \
  31. .split(self.STRING_TERMINATOR)
  32. self._path = strings[0]
  33. @property
  34. def path(self):
  35. return self._path
  36. @property
  37. def play_count(self):
  38. return self._play_count