Bladeren bron

Only put space after status if any data follows

Alois Mahdal 9 jaren geleden
bovenliggende
commit
23235f12a3
1 gewijzigde bestanden met toevoegingen van 26 en 16 verwijderingen
  1. 26
    16
      plugins/moc.py

+ 26
- 16
plugins/moc.py Bestand weergeven

@@ -11,19 +11,27 @@ class TrackData(object):
11 11
     _field_names = ['state', 'artist', 'album', 'song', 'ct', 'tt']
12 12
 
13 13
     def __init__(self):
14
-        self.state = ""
15
-        self.artist = ""
16
-        self.album = ""
17
-        self.song = ""
18
-        self.ct = ""    # current time
19
-        self.tt = ""    # total time
14
+        self.state = None
15
+        self.artist = None
16
+        self.album = None
17
+        self.song = None
18
+        self.ct = None  # current time
19
+        self.tt = None  # total time
20
+        self._is_loaded = False
21
+
22
+    def is_playing(self):
23
+        if not self._is_loaded:
24
+            self.load()
25
+        return any((self.artist, self.album, self.song, self.ct, self.tt))
20 26
 
21 27
     def load(self):
22 28
         FNULL = open(os.devnull, 'w+')
23 29
         for fieldname in self.__class__._field_names:
24 30
             value = subprocess.check_output(['mocp', '-Q', "%" + fieldname],
25
-                                            stderr=FNULL)
26
-            setattr(self, fieldname, value.strip())
31
+                                            stderr=FNULL).strip()
32
+            if value:
33
+                setattr(self, fieldname, value)
34
+        self._is_loaded = True
27 35
 
28 36
 
29 37
 class MocTitler(object):
@@ -36,14 +44,16 @@ class MocTitler(object):
36 44
         """Assemble the data to human-readable format"""
37 45
         d = self.td
38 46
         title = ""
39
-        title += "(%s) " % d.state
40
-        title += d.artist if d.artist else ""
41
-        title += " " if d.artist and d.album else ""
42
-        title += "[%s]" % d.album if d.album else ""
43
-        if (d.artist or d.album) and (d.song or d.tt):
44
-            title += ": "
45
-        title += d.song if d.song else ""
46
-        title += " (%s/%s)" % (d.ct, d.tt) if d.tt else ""
47
+        title += "(%s)" % d.state
48
+        if d.is_playing():
49
+            title += " "
50
+            title += d.artist if d.artist else ""
51
+            title += " " if d.artist and d.album else ""
52
+            title += "[%s]" % d.album if d.album else ""
53
+            if (d.artist or d.album) and (d.song or d.tt):
54
+                title += ": "
55
+            title += d.song if d.song else ""
56
+            title += " (%s/%s)" % (d.ct, d.tt) if d.tt else ""
47 57
 
48 58
         return title
49 59