Permalink
Browse files

Refactored trackinfo.

* It now returns a generator of text instead of printing stuff directly.
* It also raises an exception to let the main block handle printing to
  stderr.
* Main block has been changed to accept a generator instead of a
  single string return value -- this allows incremental printing of
  "trackinfo *" output instead of having to wait until the one giant
  string of output is constructed.
* Added tests for the three different forms of trackinfo.
  • Loading branch information...
1 parent 6122c60 commit 62245e67b2ce9d3c5c54e13857cf85af6a589b42 @mackstann committed Feb 21, 2011
Showing with 54 additions and 19 deletions.
  1. +1 −1 mock/dbus.py
  2. +27 −17 mpris-remote
  3. +26 −1 tests.py
View
@@ -43,7 +43,7 @@ def recorder(*args):
key = (self.obj.path, methodname)
_method_calls.append(key + args)
if key in _mocked_methods:
- return _mocked_methods[key]()
+ return _mocked_methods[key](*args)
return recorder
class exceptions(object):
View
@@ -201,10 +201,11 @@ def format_time(rawms):
def playstatus_from_int(n):
return ['playing', 'paused', 'stopped'][n]
-def print_metadata(dct):
- if not dct:
- print >>sys.stderr, "No track is currently selected."
- return
+class NoTrackCurrentlySelected(Exception):
+ pass
+
+def format_metadata(dct):
+ lines = []
for k in sorted(dct.keys()):
v = dct[k]
@@ -221,7 +222,8 @@ def print_metadata(dct):
if k == 'mtime':
v = "%s (%s)" % (v, format_time(int(v)))
- print "%s: %s" % (k, v)
+ lines.append("%s: %s" % (k, v))
+ return '\n'.join(lines) + '\n'
class RequestedPlayerNotRunning(Exception):
pass
@@ -330,22 +332,28 @@ class MPRISRemote(object):
@explain_numargs(0)
def playstatus(self):
status = self.player.GetStatus()
- return ("playing: %s\n" % playstatus_from_int(status[0])
- + "random/shuffle: %s\n" % ("true" if status[1] else "false")
- + "repeat track: %s\n" % ("true" if status[2] else "false")
- + "repeat list: %s\n" % ("true" if status[3] else "false"))
+ yield ("playing: %s\n" % playstatus_from_int(status[0])
+ + "random/shuffle: %s\n" % ("true" if status[1] else "false")
+ + "repeat track: %s\n" % ("true" if status[2] else "false")
+ + "repeat list: %s\n" % ("true" if status[3] else "false"))
@explain_numargs(0, 1)
@explain_argtype(0, is_track_num_or_star, optional=True)
def trackinfo(self, track=None):
if track == '*':
for i in range(self.tracklist_len):
- print_metadata(self.tracklist.GetMetadata(i))
- print
- elif track is not None:
- print_metadata(self.tracklist.GetMetadata(int(track)))
+ meta = self.tracklist.GetMetadata(i)
+ if meta is not None:
+ yield format_metadata(self.tracklist.GetMetadata(i))
+ yield '\n'
else:
- print_metadata(self.player.GetMetadata())
+ if track is not None:
+ meta = self.tracklist.GetMetadata(int(track))
+ else:
+ meta = self.player.GetMetadata()
+ if meta is None:
+ raise NoTrackCurrentlySelected()
+ yield format_metadata(meta)
# tracklist
@@ -567,12 +575,14 @@ if __name__ == '__main__':
args = sys.argv[2:]
try:
- ret = getattr(remote, method_name)(*args)
- if ret:
- sys.stdout.write(ret.encode(encoding, 'replace'))
+ output_generator = getattr(remote, method_name)(*args) or []
+ for chunk in output_generator:
+ sys.stdout.write(chunk.encode(encoding, 'replace'))
except BadUserInput, e:
print >>sys.stderr, e
raise SystemExit(1)
+ except NoTrackCurrentlySelected:
+ print >>sys.stderr, "No track is currently selected."
except KeyboardInterrupt:
raise SystemExit(2)
View
@@ -108,7 +108,32 @@ def test_playstatus(self):
+ "repeat list: true\n")
r = mprisremote.MPRISRemote()
r.find_player('foo')
- self.assertEquals(expected_output, r.playstatus())
+ self.assertEquals(expected_output, ''.join(r.playstatus()))
+
+ def test_trackinfo(self):
+ dbus.mock_method('/Player', 'GetMetadata', lambda: { 'a': 'b', 'c': 1000 })
+ expected_output = 'a: b\nc: 1000\n'
+ r = mprisremote.MPRISRemote()
+ r.find_player('foo')
+ self.assertEquals(expected_output, ''.join(r.trackinfo()))
+
+ def test_trackinfo_with_star(self):
+ dbus.mock_method('/TrackList', 'GetLength', lambda: 3)
+ dbus.mock_method('/TrackList', 'GetMetadata', lambda tracknum: { 'a': 'b', 'c': tracknum })
+ expected_output = ('a: b\nc: 0\n\n'
+ + 'a: b\nc: 1\n\n'
+ + 'a: b\nc: 2\n\n')
+ r = mprisremote.MPRISRemote()
+ r.find_player('foo')
+ self.assertEquals(expected_output, ''.join(r.trackinfo('*')))
+
+ def test_trackinfo_with_track_number(self):
+ dbus.mock_method('/TrackList', 'GetLength', lambda: 9)
+ dbus.mock_method('/TrackList', 'GetMetadata', lambda tracknum: { 'a': 'b', 'c': tracknum })
+ expected_output = 'a: b\nc: 8\n'
+ r = mprisremote.MPRISRemote()
+ r.find_player('foo')
+ self.assertEquals(expected_output, ''.join(r.trackinfo('8')))
def test_find_player_success(self):
r = mprisremote.MPRISRemote()

0 comments on commit 62245e6

Please sign in to comment.