Skip to content
Browse files

Refactored playstatus command.

1. It no longer prints directly -- it returns a string, and the main
block takes care of printing.
2. This also makes it easily testable, so I added a unit test.
  • Loading branch information...
1 parent 155c563 commit 6122c60d231f17ae0d9c4d7bdbf7517696f598dc @mackstann committed Feb 19, 2011
Showing with 25 additions and 12 deletions.
  1. +15 −12 mpris-remote
  2. +10 −0 tests.py
View
27 mpris-remote
@@ -330,10 +330,10 @@ class MPRISRemote(object):
@explain_numargs(0)
def playstatus(self):
status = self.player.GetStatus()
- print "playing: %s" % playstatus_from_int(status[0])
- print "random/shuffle: %s" % ("true" if status[1] else "false")
- print "repeat track: %s" % ("true" if status[2] else "false")
- print "repeat list: %s" % ("true" if status[3] else "false")
+ 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"))
@explain_numargs(0, 1)
@explain_argtype(0, is_track_num_or_star, optional=True)
@@ -560,16 +560,19 @@ if __name__ == '__main__':
encoding = sys.stdout.encoding or locale.getpreferredencoding() or 'ascii'
if len(sys.argv) == 1:
- sys.stdout.write(remote.verbose_status().encode(encoding, 'replace'))
+ method_name = 'verbose_status'
+ args = []
else:
method_name = sys.argv[1]
args = sys.argv[2:]
- try:
- getattr(remote, method_name)(*args)
- except BadUserInput, e:
- print >>sys.stderr, e
- raise SystemExit(1)
- except KeyboardInterrupt:
- raise SystemExit(2)
+ try:
+ ret = getattr(remote, method_name)(*args)
+ if ret:
+ sys.stdout.write(ret.encode(encoding, 'replace'))
+ except BadUserInput, e:
+ print >>sys.stderr, e
+ raise SystemExit(1)
+ except KeyboardInterrupt:
+ raise SystemExit(2)
View
10 tests.py
@@ -100,6 +100,16 @@ def test_print_verbose_status_typical(self):
r.find_player('foo')
self.assertEquals(expected_output, r.verbose_status())
+ def test_playstatus(self):
+ dbus.mock_method('/Player', 'GetStatus', lambda: [0, 1, 0, 1])
+ expected_output = ("playing: playing\n"
+ + "random/shuffle: true\n"
+ + "repeat track: false\n"
+ + "repeat list: true\n")
+ r = mprisremote.MPRISRemote()
+ r.find_player('foo')
+ self.assertEquals(expected_output, r.playstatus())
+
def test_find_player_success(self):
r = mprisremote.MPRISRemote()
r.find_player('foo')

0 comments on commit 6122c60

Please sign in to comment.
Something went wrong with that request. Please try again.