Kaynağa Gözat

symuid/library/cmus.py: class Cache

Fabian Peter Hammerle 6 yıl önce
ebeveyn
işleme
3c2aeaa795
2 değiştirilmiş dosya ile 46 ekleme ve 34 silme
  1. 6 34
      symuid-import-cmus
  2. 40 0
      symuid/library/cmus.py

+ 6 - 34
symuid-import-cmus

@@ -6,42 +6,14 @@ import os
 import symuid
 import symuid.library.cmus
 
-FILE_PREFIX = b'CTC'
-VERSION_LENGTH = 1
-SUPPORTED_VERSION = b'\x0c'
-# always big endian, see cache_init()
-FLAGS_BYTEORDER = 'big'
-FLAGS_LENGTH = 4
-FLAG_64_BIT = 0x01
-
-
-def import_track(cmus_track):
-    if cmus_track.play_count > 0:
-        print(cmus_track.play_count, cmus_track.path)
-
 
 def symuid_import_cmus(cache_path):
-    with open(os.path.expanduser(cache_path), 'rb') as cache:
-        # see cache.c cache_init()
-        assert cache.read(len(FILE_PREFIX)) == FILE_PREFIX
-        cache_version = cache.read(VERSION_LENGTH)
-        assert cache_version == SUPPORTED_VERSION, cache_version
-        flags = int.from_bytes(
-            cache.read(FLAGS_LENGTH),
-            byteorder=FLAGS_BYTEORDER,  # persistent
-        )
-        assert flags & FLAG_64_BIT
-        # support no other flags
-        assert flags & ~FLAG_64_BIT == 0, flags
-        # size includes size itself
-        while True:
-            size = symuid.library.cmus._int_from_bytes_sys(cache.read(4))
-            if size == 0:
-                break
-            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)
+    lib = symuid.library.cmus.Cache(
+        path=os.path.expanduser(cache_path),
+    )
+    for cmus_track in lib.get_tracks():
+        if cmus_track.play_count > 0:
+            print(cmus_track.play_count, cmus_track.path)
 
 
 def _init_argparser():

+ 40 - 0
symuid/library/cmus.py

@@ -45,3 +45,43 @@ class Track:
     @property
     def play_count(self):
         return self._play_count
+
+
+class Cache:
+
+    FILE_PREFIX = b'CTC'
+    VERSION_LENGTH = 1
+    SUPPORTED_VERSION = b'\x0c'
+    # always big endian, see cache_init()
+    FLAGS_BYTEORDER = 'big'
+    FLAGS_LENGTH = 4
+    FLAG_64_BIT = 0x01
+
+    HEADER_LENGTH = len(FILE_PREFIX) + VERSION_LENGTH + FLAGS_LENGTH
+
+    def __init__(self, path):
+        self._path = path
+        with open(self._path, 'rb') as stream:
+            # see cache.c cache_init()
+            assert stream.read(len(self.FILE_PREFIX)) == self.FILE_PREFIX
+            cache_version = stream.read(self.VERSION_LENGTH)
+            assert cache_version == self.SUPPORTED_VERSION, cache_version
+            flags = int.from_bytes(
+                stream.read(self.FLAGS_LENGTH),
+                byteorder=self.FLAGS_BYTEORDER,  # persistent
+            )
+            assert flags & self.FLAG_64_BIT
+            # support no other flags
+            assert flags & ~self.FLAG_64_BIT == 0, flags
+
+    def get_tracks(self):
+        with open(self._path, 'rb') as stream:
+            stream.seek(self.HEADER_LENGTH)
+            # size includes size itself
+            while True:
+                size = _int_from_bytes_sys(stream.read(4))
+                if size == 0:
+                    break
+                yield Track(size, stream.read(size - 4))
+                # see cache.c write_ti ALIGN
+                stream.read((-size) % 8)