Permalink
Browse files

persistent current playlist

  • Loading branch information...
1 parent 5e3d835 commit fe2e93f1b2659f7af3cfc1144cfcea214481a178 @wendlers committed Oct 5, 2012
Showing with 40 additions and 4 deletions.
  1. +5 −4 src/pyscmpd/pyscmpdctrl.py
  2. +35 −0 src/pyscmpd/scmpd.py
@@ -28,17 +28,18 @@
import argparse
import os
-USER_WORK_DIR = ("%s/.pyscmpd" % os.getenv("HOME"))
-DEF_PID_FILE = ("%s/pyscmpd.pid" % USER_WORK_DIR)
-DEF_CONF_FILE = ("%s/pyscmpd.conf" % USER_WORK_DIR)
-DEF_LOG_FILE = ("%s/pyscmpd.log" % USER_WORK_DIR)
+from config import *
def prepare():
if not os.path.exists(USER_WORK_DIR):
print("Note: pyscmpd working dir created: %s" % USER_WORK_DIR)
os.makedirs(USER_WORK_DIR)
+ if not os.path.exists(PLAYLIST_DIR):
+ print("Note: pyscmpd playlist dir created: %s" % PLAYLIST_DIR)
+ os.makedirs(PLAYLIST_DIR)
+
if not os.path.exists(DEF_CONF_FILE):
print("Note: pyscmpd default config created: %s" % DEF_CONF_FILE)
View
@@ -21,12 +21,16 @@
This file is part of the pyscmpd project.
'''
+import signal
import logging
import mpdserver
import soundcloud
import pyscmpd.scprovider as provider
import pyscmpd.gstplayer as gstplayer
+import pyscmpd.respersist as persist
+
+from config import *
class ScMpdServerDaemon(mpdserver.MpdServerDaemon):
@@ -37,6 +41,24 @@ class ScMpdServerDaemon(mpdserver.MpdServerDaemon):
def __init__(self, favoriteUsers, favoriteGroups, favoriteFavorites, serverPort = 9900):
ScMpdServerDaemon.player = gstplayer.GstPlayer()
+
+ # try to read last playlist
+ try:
+
+ logging.info("Restoring last playlist")
+ p = persist.ResourceFilePersistence(PLAYLIST_DIR)
+ c = p.retrive(CURR_PLAYLIST_KEY)
+
+ if not c == None:
+ ScMpdServerDaemon.player.children = c
+ ScMpdServerDaemon.player.playlistVersion = 1
+
+ logging.info("Done restoring last playlist: %s" % `c`)
+
+ except Exception as e:
+
+ logging.warn("Unable to read last playlist: %s" % `e`)
+
ScMpdServerDaemon.scp = provider.ResourceProvider(favoriteUsers, favoriteGroups,
favoriteFavorites)
ScMpdServerDaemon.scroot = ScMpdServerDaemon.scp.getRoot()
@@ -62,6 +84,19 @@ def __init__(self, favoriteUsers, favoriteGroups, favoriteFavorites, serverPort
self.requestHandler.Playlist = MpdPlaylist
+ signal.signal(signal.SIGTERM, self.exitHandler)
+
+ def exitHandler(self, signal, func = None):
+
+ logging.info("Ending server daemon. Persisting current playlist")
+
+ c = ScMpdServerDaemon.player.getAllChildren()
+ p = persist.ResourceFilePersistence(PLAYLIST_DIR)
+ p.store(CURR_PLAYLIST_KEY, c)
+
+ logging.info("Done persisting current playlist")
+ exit(0)
+
class Play(mpdserver.Play):
def handle_args(self, songPos=0):

0 comments on commit fe2e93f

Please sign in to comment.