From 8683537816f380234f105e8da8f5c6fe6aac5da5 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Wed, 17 Oct 2012 01:43:22 +0200 Subject: [PATCH] Don't use command_list as both bool and list (#211) --- mopidy/frontends/mpd/dispatcher.py | 6 +++--- mopidy/frontends/mpd/protocol/command_list.py | 12 +++++++----- .../mpd/protocol/command_list_test.py | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index ae51d270..6f91c491 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -22,8 +22,9 @@ class MpdDispatcher(object): def __init__(self, session=None, core=None): self.authenticated = False - self.command_list = False + self.command_list_receiving = False self.command_list_ok = False + self.command_list = [] self.command_list_index = None self.context = MpdContext(self, session=session, core=core) @@ -108,8 +109,7 @@ class MpdDispatcher(object): def _is_receiving_command_list(self, request): return ( - self.command_list is not False and - request != u'command_list_end') + self.command_list_receiving and request != u'command_list_end') def _is_processing_command_list(self, request): return ( diff --git a/mopidy/frontends/mpd/protocol/command_list.py b/mopidy/frontends/mpd/protocol/command_list.py index a58c11e2..d422f97e 100644 --- a/mopidy/frontends/mpd/protocol/command_list.py +++ b/mopidy/frontends/mpd/protocol/command_list.py @@ -19,18 +19,19 @@ def command_list_begin(context): returned. If ``command_list_ok_begin`` is used, ``list_OK`` is returned for each successful command executed in the command list. """ - context.dispatcher.command_list = [] + context.dispatcher.command_list_receiving = True context.dispatcher.command_list_ok = False + context.dispatcher.command_list = [] @handle_request(r'^command_list_end$') def command_list_end(context): """See :meth:`command_list_begin()`.""" - if context.dispatcher.command_list is False: - # Test for False exactly, and not e.g. empty list + if not context.dispatcher.command_list_receiving: raise MpdUnknownCommand(command='command_list_end') + context.dispatcher.command_list_receiving = False (command_list, context.dispatcher.command_list) = ( - context.dispatcher.command_list, False) + context.dispatcher.command_list, []) (command_list_ok, context.dispatcher.command_list_ok) = ( context.dispatcher.command_list_ok, False) command_list_response = [] @@ -49,5 +50,6 @@ def command_list_end(context): @handle_request(r'^command_list_ok_begin$') def command_list_ok_begin(context): """See :meth:`command_list_begin()`.""" - context.dispatcher.command_list = [] + context.dispatcher.command_list_receiving = True context.dispatcher.command_list_ok = True + context.dispatcher.command_list = [] diff --git a/tests/frontends/mpd/protocol/command_list_test.py b/tests/frontends/mpd/protocol/command_list_test.py index 64ef8688..dbd7f9c9 100644 --- a/tests/frontends/mpd/protocol/command_list_test.py +++ b/tests/frontends/mpd/protocol/command_list_test.py @@ -18,13 +18,18 @@ class CommandListsTest(protocol.BaseTestCase): def test_command_list_with_ping(self): self.sendRequest(u'command_list_begin') + self.assertTrue(self.dispatcher.command_list_receiving) + self.assertFalse(self.dispatcher.command_list_ok) self.assertEqual([], self.dispatcher.command_list) - self.assertEqual(False, self.dispatcher.command_list_ok) + self.sendRequest(u'ping') self.assertIn(u'ping', self.dispatcher.command_list) + self.sendRequest(u'command_list_end') self.assertInResponse(u'OK') - self.assertEqual(False, self.dispatcher.command_list) + self.assertFalse(self.dispatcher.command_list_receiving) + self.assertFalse(self.dispatcher.command_list_ok) + self.assertEqual([], self.dispatcher.command_list) def test_command_list_with_error_returns_ack_with_correct_index(self): self.sendRequest(u'command_list_begin') @@ -39,15 +44,19 @@ class CommandListsTest(protocol.BaseTestCase): def test_command_list_ok_with_ping(self): self.sendRequest(u'command_list_ok_begin') + self.assertTrue(self.dispatcher.command_list_receiving) + self.assertTrue(self.dispatcher.command_list_ok) self.assertEqual([], self.dispatcher.command_list) - self.assertEqual(True, self.dispatcher.command_list_ok) + self.sendRequest(u'ping') self.assertIn(u'ping', self.dispatcher.command_list) + self.sendRequest(u'command_list_end') self.assertInResponse(u'list_OK') self.assertInResponse(u'OK') - self.assertEqual(False, self.dispatcher.command_list) - self.assertEqual(False, self.dispatcher.command_list_ok) + self.assertFalse(self.dispatcher.command_list_receiving) + self.assertFalse(self.dispatcher.command_list_ok) + self.assertEqual([], self.dispatcher.command_list) # FIXME this should also include the special handling of idle within a # command list. That is that once a idle/noidle command is found inside a