4e705e66c1c5 — Oben Sonne 10 years ago
server: make bong player ready for control by client
1 files changed, 21 insertions(+), 12 deletions(-)

M server/src/tests/bong.py
M server/src/tests/bong.py +21 -12
@@ 19,6 19,9 @@ IFACE = "org.remuco.Player"
 
 ml = None
 
+class PlayerException(Exception):
+    pass
+
 class Bong(dbus.service.Object):
 
     def __init__(self, fname):

          
@@ 43,9 46,10 @@ class Bong(dbus.service.Object):
         log("new content: %r" % content)
         x = yaml.load(content)
         for k, v in x.items():
+            log('check %s, %s' % (k, v))
             if k.startswith("info_"):
                 continue
-            if self.x.get(k, None) != v:
+            if self.x.get(k, None) != v:  # FIXME: fails on playlist changes
                 log("%s changed: %s" % (k, v))
                 self.x[k] = v
                 if k == "library":

          
@@ 57,11 61,14 @@ class Bong(dbus.service.Object):
                 signal = "%sChanged" % k.capitalize()
                 v = v if isinstance(v, list) else [v]
                 getattr(self, signal)(*v)
+            else:
+                log('no change')
 
     def changestate(self, field, value):
-        self.x[field] = value
+        x = self.x.copy()
+        x[field] = value
         with open(self.fname, 'w') as fp:
-            yaml.dump(self.x, fp)
+            yaml.dump(x, fp)
 
     # -------------------------------------------------------------------------
     # signal methods

          
@@ 179,7 186,7 @@ class Bong(dbus.service.Object):
             newpos = (position + n) % len(playlist)
         else:
             newpos = max(0, min(len(playlist) - 1, position + n))
-        self.changestate('playlistposition', newpos)
+        self.changestate('position', newpos)
 
     @dbus.service.method(IFACE, 'u', '')
     def SeekTo(self, position):

          
@@ 187,28 194,30 @@ class Bong(dbus.service.Object):
         position = min(length, max(0, position))
         self.changestate('progress', (position, length))
 
+    @dbus.service.method(IFACE, 'u', '')
+    def SetVolume(self, level):
+        self.changestate('volume', level)
+
     @dbus.service.method(IFACE, 's', '')
     def SetRepeat(self, mode):
         if mode not in self.features['repeatmodes']:
-            raise Exception("invalid repeat mode")
+            raise PlayerException("invalid repeat mode")
         self.changestate('repeat', mode)
 
     @dbus.service.method(IFACE, 's', '')
     def SetShuffle(self, mode):
         if mode not in self.features['shufflemodes']:
-            raise Exception("invalid repeat mode")
+            raise PlayerException("invalid repeat mode")
         self.changestate('shuffle', mode)
 
     @dbus.service.method(IFACE, 'u', '')
     def SetRating(self, value):
-        if not self.x.get('playlist', []):
+        playlist = self.x.get('playlist', [])
+        if not playlist:
             return
-        playlist = self.x['playlist'].copy()
-        track = playlist[self.x.get('position', 0)].copy()
-        track['rating'] = value # TODO: raise some error when there is no rating field
-        playlist[self.x.get('position', 0)] = track
+        position = self.x.get('position', 0)
+        playlist[position]['rating'] = value
         self.changestate('playlist', playlist)
-        # TODO: check bad values?
 
     # -------------------------------------------------------------------------
     # action methods