MPDScrobbler: Merge Player and Dæmon objects
authorkaliko <kaliko.jack@azylum.org>
Thu, 14 Mar 2013 19:22:11 +0000 (20:22 +0100)
committerkaliko <kaliko.jack@azylum.org>
Thu, 14 Mar 2013 19:22:11 +0000 (20:22 +0100)
misc/MPDScrobbler/mpdscrobbler.py

index 36c5220..94b472d 100755 (executable)
@@ -78,16 +78,16 @@ class QueuManagement(threading.Thread):
                 return
 
 
-class MpdScrobbler(Player):
+class MpdScrobbler(Daemon, Player):
 
-    def __init__(self):
+    def __init__(self, pid, log):
         Player.__init__(self, MPDSERVER, MPDPORT)
+        Daemon.__init__(self, pid, stdout=log, stderr=log)
         self.track_format = True
         self.current_track = None
         self.failed_sc_cache = deque()
-        self.start()
 
-    def start(self):
+    def run(self):
         try:
             self.connect()
             logging.info("Connected to %s" % MPDSERVER)
@@ -98,7 +98,7 @@ class MpdScrobbler(Player):
             logging.error('player ukn err: {0}'.format(err))
             sys.exit(1)
         try:
-            self.run()
+            self.main()
         except KeyboardInterrupt:
             self.disconnect()
             sys.exit(0)
@@ -156,9 +156,7 @@ class MpdScrobbler(Player):
             self.current_track = current
             self.scrobble()
 
-    def run(self):
-        # TODO: handle lost connection with MPD
-        #       tries reconnection once and exit.
+    def main(self):
         if self.status().get('state') == 'play':
             self.log_track()  # scrobble at startup
         while True:
@@ -176,23 +174,13 @@ class MpdScrobbler(Player):
             self.log_track()
             sys.stdout.flush()
 
-class Scrobbling(Daemon):
-
-    def __init__(self, pid, log):
-      Daemon.__init__(self, pid, stdout=log, stderr=log)
-
-    def run(self):
-        """Overrides Daemon().run() with actions you want to daemonize.
-        MyDaemon.run() is then called within MyDaemon().start()"""
-        logging.info(u"Starting Deamon!")
-        MpdScrobbler()
-
     def shutdown(self):
-        """Overrides Daemon().shutdown() with some clean up"""
+        """Called on Dæmon stop"""
         logging.info(u"Stopping Daemon!")
 
+
 def main():
-    daemon = Scrobbling('/tmp/mpd_scrobbler.pid', LOGFILE)
+    daemon = MpdScrobbler('/tmp/mpd_scrobbler.pid', LOGFILE)
     if len(sys.argv) == 2:
         if 'start' == sys.argv[1]:
             daemon.start()