Parcourir la source

symuid/library/cmus.py: class Track

Fabian Peter Hammerle il y a 6 ans
Parent
commit
fae4eaa62a
2 fichiers modifiés avec 51 ajouts et 47 suppressions
  1. 4 47
      symuid-import-cmus
  2. 47 0
      symuid/library/cmus.py

+ 4 - 47
symuid-import-cmus

@@ -3,10 +3,8 @@
 
 import datetime as dt
 import os
-import re
 import symuid
-# import symuid.library.cmus
-import sys
+import symuid.library.cmus
 
 FILE_PREFIX = b'CTC'
 VERSION_LENGTH = 1
@@ -15,48 +13,6 @@ SUPPORTED_VERSION = b'\x0c'
 FLAGS_BYTEORDER = 'big'
 FLAGS_LENGTH = 4
 FLAG_64_BIT = 0x01
-RESERVED_PAD_REGEX = rb'\xff{16,}'
-STRING_TERMINATOR = b'\x00'
-
-
-def _int_from_bytes_sys(data_bytes):
-    return int.from_bytes(data_bytes, byteorder=sys.byteorder)
-
-
-class Track:
-
-    def __init__(self, cache_size, cache_bytes):
-        """
-        struct cache_entry {
-            // size of this struct including size itself
-            uint32_t size;
-            int32_t play_count;
-            int64_t mtime;
-            int32_t duration;
-            int32_t bitrate;
-            int32_t bpm;
-            uint8_t _reserved[CACHE_ENTRY_RESERVED_SIZE];
-            // filename, codec, codec_profile and N * (key, val)
-            char strings[];
-        };
-        """
-        assert len(cache_bytes) + 4 == cache_size
-        self._play_count = _int_from_bytes_sys(cache_bytes[0:4])
-        # self._mtime = _int_from_bytes_sys(cache_bytes[4:12])
-        # self._duration_seconds = _int_from_bytes_sys(cache_bytes[12:16])
-        # self._bitrate = _int_from_bytes_sys(cache_bytes[16:20])
-        # self._bpm = _int_from_bytes_sys(cache_bytes[20:24])
-        strings = re.split(RESERVED_PAD_REGEX, cache_bytes)[1] \
-            .split(STRING_TERMINATOR)
-        self._path = strings[0]
-
-    @property
-    def path(self):
-        return self._path
-
-    @property
-    def play_count(self):
-        return self._play_count
 
 
 def import_track(cmus_track):
@@ -79,10 +35,10 @@ def symuid_import_cmus(cache_path):
         assert flags & ~FLAG_64_BIT == 0, flags
         # size includes size itself
         while True:
-            size = _int_from_bytes_sys(cache.read(4))
+            size = symuid.library.cmus._int_from_bytes_sys(cache.read(4))
             if size == 0:
                 break
-            cmus_track = Track(size, cache.read(size - 4))
+            cmus_track = symuid.library.cmus.Track(size, cache.read(size - 4))
             import_track(cmus_track)
             # see cache.c write_ti ALIGN
             cache.read((-size) % 8)
@@ -107,4 +63,5 @@ def main(argv):
     return 0
 
 if __name__ == "__main__":
+    import sys
     sys.exit(main(sys.argv))

+ 47 - 0
symuid/library/cmus.py

@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+
+import re
+import sys
+
+
+def _int_from_bytes_sys(data_bytes):
+    return int.from_bytes(data_bytes, byteorder=sys.byteorder)
+
+
+class Track:
+
+    RESERVED_PAD_REGEX = rb'\xff{16,}'
+    STRING_TERMINATOR = b'\x00'
+
+    def __init__(self, cache_size, cache_bytes):
+        """
+        struct cache_entry {
+            // size of this struct including size itself
+            uint32_t size;
+            int32_t play_count;
+            int64_t mtime;
+            int32_t duration;
+            int32_t bitrate;
+            int32_t bpm;
+            uint8_t _reserved[CACHE_ENTRY_RESERVED_SIZE];
+            // filename, codec, codec_profile and N * (key, val)
+            char strings[];
+        };
+        """
+        assert len(cache_bytes) + 4 == cache_size
+        self._play_count = _int_from_bytes_sys(cache_bytes[0:4])
+        # self._mtime = _int_from_bytes_sys(cache_bytes[4:12])
+        # self._duration_seconds = _int_from_bytes_sys(cache_bytes[12:16])
+        # self._bitrate = _int_from_bytes_sys(cache_bytes[16:20])
+        # self._bpm = _int_from_bytes_sys(cache_bytes[20:24])
+        strings = re.split(self.RESERVED_PAD_REGEX, cache_bytes)[1] \
+            .split(self.STRING_TERMINATOR)
+        self._path = strings[0]
+
+    @property
+    def path(self):
+        return self._path
+
+    @property
+    def play_count(self):
+        return self._play_count