Skip to content

Commit

Permalink
mpd_cmdlist context manager stores return values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsufeki committed Jul 29, 2013
1 parent 70988f8 commit 8b15fa1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
20 changes: 12 additions & 8 deletions qygmy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ def not_callable(*args):
raise TypeError("Not callable")


class MPDCommandList:
def __init__(self, conn):
self.conn = conn
def __enter__(self):
self.conn.command_list_ok_begin()
def __exit__(self, *exc):
self.conn.command_list_end()
def mpd_cmdlist_context_manager(connection):
class MPDCommandList:
conn = connection

def __enter__(self):
self.conn.command_list_ok_begin()
return self

def __exit__(self, *exc):
self.retval = self.conn.command_list_end()
return MPDCommandList


class Connection:
Expand Down Expand Up @@ -169,7 +173,7 @@ def __init__(self, main):
super().__init__(main)
self.main = main
self.conn = mpd.MPDClient()
self.mpd_cmdlist = MPDCommandList(self.conn)
self.mpd_cmdlist = mpd_cmdlist_context_manager(self.conn)
State.create(self, 'state', 'disconnect', not_callable)

def update_state(self):
Expand Down
12 changes: 6 additions & 6 deletions qygmy/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def add(self, items, pos=None, **kwargs):
if pos is not None:
a = reversed(a)
last = len(self)
with self.mpd_cmdlist:
with self.mpd_cmdlist():
for i in a:
self.add_one(i, pos, last, **kwargs)
last += 1
Expand All @@ -188,7 +188,7 @@ def remove(self, positions):
i = self[pos].copy()
i['pos'] = str(pos)
items.append(i)
with self.mpd_cmdlist:
with self.mpd_cmdlist():
for i in reversed(sorted(items, key=lambda x: int(x['pos']))):
self.remove_one(i)
self.refresh()
Expand All @@ -213,7 +213,7 @@ def move(self, items, pos):
b = (i for i in items if int(i['pos']) > pos)
apos = pos
bpos = pos + (0 if len(a) == 0 else 1)
with self.mpd_cmdlist:
with self.mpd_cmdlist():
for i in reversed(a):
if apos != i['pos']:
self.move_one(i, apos)
Expand Down Expand Up @@ -310,14 +310,14 @@ def set_priority(self, positions, prio):
if self.can_set_priority(positions, prio):
ids = [self[i]['id'] for i in positions]
batch_size = self.MAX_MPD_ARGUMENTS - 1
with self.mpd_cmdlist:
with self.mpd_cmdlist():
for k in range((len(ids) - 1) // batch_size + 1):
b = ids[k * batch_size:(k+1) * batch_size]
self.conn.prioid(prio, *b)

@mpd_cmd
def reverse(self):
with mpd_cmdlist:
with self.mpd_cmdlist():
n = len(self)
for i in range(n//2 + 1):
self.conn.swap(i, n-i-1)
Expand All @@ -326,7 +326,7 @@ def item_chosen(self, pos):
self.current_pos.send(pos)

def _update_current_pos(self, new, old):
for i in {old, new}:
for i in (old, new):
if 0 <= i < len(self):
index1 = self.index(i, 0)
index2 = self.index(i, self.columnCount() - 1)
Expand Down
16 changes: 8 additions & 8 deletions qygmy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def start_timer(self, interval=500):

def update_state(self):
s, c = {'state': 'disconnect'}, {}
if self.state.value != 'disconnect':
try:
self.conn.command_list_ok_begin()
self.conn.status()
self.conn.currentsong()
s, c = self.conn.command_list_end()
except (mpd.MPDError, OSError) as e:
self.handle_error(e)
try:
if self.state.value != 'disconnect':
with self.mpd_cmdlist() as cl:
self.conn.status()
self.conn.currentsong()
s, c = cl.retval
except (mpd.MPDError, OSError) as e:
self.handle_error(e)

self.state.update(s['state'])
self.times.update(s.get('time', ':').split(':'))
Expand Down

0 comments on commit 8b15fa1

Please sign in to comment.