Browse Source

feat: Adding directory support

Matthias Ladkau 4 years ago
parent
commit
fca8aa53c1

+ 0 - 2
musikautomat/display.py

@@ -6,7 +6,6 @@ import yaml
 import pygame # type: ignore
 from typing import Dict
 import random
-import eyed3
 import tempfile
 
 # Color constants
@@ -74,4 +73,3 @@ class Display:
 
             if i == self.drawlines:
                 break
-

+ 2 - 2
musikautomat/handler/__init__.py

@@ -24,11 +24,11 @@ class BaseHandler:
         self.item = item
 
 
-    def update(self):
+    def update(self, text=None, highlight=-1, title=None, img=None):
         '''
         Called when the handler should update the display.
         '''
-        raise NotImplementedError("Update not implemented")
+        self.display.update(text, highlight, title, img)
 
 
     def stop(self):

+ 20 - 0
musikautomat/handler/dir.py

@@ -0,0 +1,20 @@
+#!./bin/python3
+
+import os
+
+from handler.filelist import FileListHandler
+
+class DirectoryHandler(FileListHandler):
+
+    def getPlaylistItems(self, item):
+        path = item["path"]
+        items = []
+
+        items = sorted([{
+            "name" : self.toDisplayName(f),
+            "path" : os.path.join(path, f)
+        } for f in os.listdir(path)], key=lambda i: i["name"])
+
+        print("GOT items:", items)
+
+        return items

+ 108 - 0
musikautomat/handler/filelist.py

@@ -0,0 +1,108 @@
+#!./bin/python3
+
+import threading
+import time
+
+from handler import EVENT_LEFT
+from handler.listhandler import BaseListHandler
+from handler.lib.pymplb import MPlayer
+
+class FileListHandler(BaseListHandler):
+
+    def __init__(self, config, display):
+        super().__init__(config, display)
+        self._mplayer = None
+        self._mplayer_watcher = None
+        self.is_playing = False
+        self._last_event = time.time()
+
+
+    def setPlaylistItem(self, item):
+        '''
+        Called with the selected item.
+        '''
+        super().setPlaylistItem(item)
+
+        if self._mplayer is None:
+            self._mplayer = MPlayer()
+            self.setItems(self.getPlaylistItems(item))
+            self._mplayer_watcher = threading.Thread(target=self._watch_mplayer, daemon=True)
+            self._mplayer_watcher.start()
+
+        self.is_playing = False
+
+
+    def getPlaylistItems(self, item):
+        raise NotImplementedError()
+
+
+    def update(self, text=None, highlight=-1, title=None, img=None):
+        '''
+        Called when the handler should update the display.
+        '''
+        super().update(title=[
+            "",
+            "Playlist:",
+            self.item["name"],
+            "[play]" if self.is_playing else "[stop]"
+        ], img=self.item.get("img"))
+
+
+    def itemSelected(self, item):
+        self._mplayer.loadfile(item["path"])
+        self.is_playing = True
+
+
+    def stop(self):
+        '''
+        Called when the handler should stop.
+        '''
+        if self._mplayer is not None:
+            self._mplayer.quit()
+            self._mplayer = None
+            self._mplayer_watcher = None
+
+
+    def emitEvent(self, event):
+        '''
+        Called when an event has happened.
+        '''
+
+        # Clock the time so the thread can hold off if there
+        # was a recent user event
+
+        self._last_event = time.time()
+
+        if event == EVENT_LEFT and self.is_playing:
+            self._mplayer.stop()
+            self.is_playing = False
+            return
+
+        super().emitEvent(event)
+
+
+    def _watch_mplayer(self):
+        '''
+        Thread to watch mplayer and advance the playlist.
+        '''
+
+        while self._mplayer_watcher is not None:
+
+            try:
+                time.sleep(0.5)
+
+                if self._mplayer:
+                    playing_path = self._mplayer.p_path
+
+                    # Only do something in the thread if the last user
+                    # input is at least 3 seconds ago
+
+                    if time.time() - self._last_event > 3:
+
+                        # Advance to the next item if we are playing music
+
+                        if self.is_playing and playing_path == "(null)":
+                            self.nextItem()
+
+            except Exception as e:
+                print("Error watching player:", e)

+ 8 - 3
musikautomat/handler/listhandler.py

@@ -13,18 +13,21 @@ class BaseListHandler(BaseHandler):
 
 
     def setItems(self, items):
+        '''
+        Set the items which should be displayed.
+        '''
         self.items = items
         self.item_pointer = 0
 
 
-    def update(self):
+    def update(self, text=None, highlight=-1, title=None, img=None):
         '''
         Called when the handler should update the display.
         '''
 
         # Show the slice of the main menu which is relevant
 
-        playlist_display = []
+        playlist_display = text or []
 
         maxlines = self.display.drawlines
 
@@ -35,7 +38,9 @@ class BaseListHandler(BaseHandler):
             else:
                 break
 
-        self.display.update(playlist_display, min(self.item_pointer, maxlines))
+        super().update(playlist_display,
+            highlight if highlight != -1 else min(self.item_pointer, maxlines),
+            title, img)
 
 
     def emitEvent(self, event):

+ 4 - 87
musikautomat/handler/m3u.py

@@ -1,28 +1,12 @@
 #!./bin/python3
 
 import os
-import threading
-import time
 
-from handler import EVENT_LEFT
-from handler.listhandler import BaseListHandler
-from handler.lib.pymplb import MPlayer
+from handler.filelist import FileListHandler
 
-class M3UHandler(BaseListHandler):
-
-    def __init__(self, config, display):
-        super().__init__(config, display)
-        self._mplayer = None
-        self._mplayer_watcher = None
-        self.is_playing = False
-        self._last_event = time.time()
-
-    def setPlaylistItem(self, item):
-        '''
-        Called with the selected item.
-        '''
-        self.item = item
+class M3UHandler(FileListHandler):
 
+    def getPlaylistItems(self, item):
         path = item["path"]
         items = []
 
@@ -35,71 +19,4 @@ class M3UHandler(BaseListHandler):
                         "path" : os.path.join(os.path.dirname(path), item_path)
                     })
 
-        if self._mplayer is None:
-            self._mplayer = MPlayer()
-            self.setItems(items)
-            self._mplayer_watcher = threading.Thread(target=self._watch_mplayer, daemon=True)
-            self._mplayer_watcher.start()
-
-        self.is_playing = False
-
-
-    def itemSelected(self, item):
-        self._mplayer.loadfile(item["path"])
-        self.is_playing = True
-
-
-    def stop(self):
-        '''
-        Called when the handler should stop.
-        '''
-        if self._mplayer is not None:
-            self._mplayer.quit()
-            self._mplayer = None
-            self._mplayer_watcher = None
-
-
-    def emitEvent(self, event):
-        '''
-        Called when an event has happened.
-        '''
-
-        # Clock the time so the thread can hold off if there
-        # was a recent user event
-
-        self._last_event = time.time()
-
-        if event == EVENT_LEFT and self.is_playing:
-            self._mplayer.stop()
-            self.is_playing = False
-            return
-
-        super().emitEvent(event)
-
-
-    def _watch_mplayer(self):
-        '''
-        Thread to watch mplayer and advance the playlist.
-        '''
-
-        while self._mplayer_watcher is not None:
-
-            try:
-                time.sleep(0.5)
-
-                if self._mplayer:
-                    playing_path = self._mplayer.p_path
-
-                    # Only do something in the thread if the last user
-                    # input is at least 3 seconds ago
-
-                    if time.time() - self._last_event > 3:
-
-                        # Advance to the next item if we are playing music
-
-                        if self.is_playing and playing_path == "(null)":
-                            self.nextItem()
-
-            except Exception as e:
-                print("Error watching player:", e)
-
+        return items

+ 13 - 6
musikautomat/handler/stream.py

@@ -10,20 +10,27 @@ class StreamHandler(BaseHandler):
         self._mplayer = None
 
 
-    def update(self):
+    def setPlaylistItem(self, item):
+        '''
+        Called with the selected item.
+        '''
+        super().setPlaylistItem(item)
+
+        if self._mplayer is None:
+            self._mplayer = MPlayer()
+            self._mplayer.loadfile(self.item["url"])
+
+
+    def update(self, text=None, highlight=-1, title=None, img=None):
         '''
         Called when the handler should update the display.
         '''
-        self.display.update(title=[
+        super().update(title=[
             "",
             "Stream:",
             self.item["name"]
         ], img=self.item.get("img"))
 
-        if self._mplayer is None:
-            self._mplayer = MPlayer()
-            self._mplayer.loadfile(self.item["url"])
-
 
     def stop(self):
         '''

BIN
musikautomat/img/c64giana.png


+ 3 - 3
musikautomat/menu.py

@@ -6,10 +6,9 @@ import yaml
 import pygame # type: ignore
 from typing import Dict
 import random
-import eyed3
 import tempfile
 import handler
-from handler import stream, m3u
+from handler import stream, m3u, dir
 
 class Menu:
     '''
@@ -24,7 +23,8 @@ class Menu:
 
         self.handler = {
             "stream" : stream.StreamHandler(config, display),
-            "m3u" : m3u.M3UHandler(config, display)
+            "m3u" : m3u.M3UHandler(config, display),
+            "dir" : dir.DirectoryHandler(config, display)
         }
 
         # Playlist data