Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process empty value in cmus information output #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lyvi/players/cmus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ def running(self):
except OSError:
return False

def get_info_value(self, info_line, index):
"""Extract 'A value' from line 'ValueName: A value'."""
try:
return info_line.split(maxsplit=index)[index]
except IndexError:
# Empty value.
return ''

def get_status(self):
data = {'artist': None, 'album': None, 'title': None, 'file': None, 'length': None}

for line in check_output('cmus-remote -Q').splitlines():
if line.startswith('status '):
data['state'] = line.split()[1].replace('playing', 'play')
data['state'] = (line.split()[1] or '').replace('playing', 'play')
for x, y in (('playing', 'play'), ('paused', 'pause'), ('stopped', 'stop')):
data['state'] = data['state'].replace(x, y)
elif line.startswith('tag artist '):
data['artist'] = line.split(maxsplit=2)[2]
data['artist'] = self.get_info_value(line, 2)
elif line.startswith('tag album '):
data['album'] = line.split(maxsplit=2)[2]
data['album'] = self.get_info_value(line, 2)
elif line.startswith('tag title '):
data['title'] = line.split(maxsplit=2)[2]
data['title'] = self.get_info_value(line, 2)
elif line.startswith('file '):
data['file'] = line.split(maxsplit=1)[1]
data['file'] = self.get_info_value(line, 1)
elif line.startswith('duration '):
data['length'] = int(line.split(maxsplit=1)[1])
data['length'] = int(self.get_info_value(line, 1) or '0')

for k in data:
setattr(self, k, data[k])
Expand Down
10 changes: 5 additions & 5 deletions lyvi/players/moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_info_value(self, info_line):
return info_line.split(maxsplit=1)[1]
except IndexError:
# Empty value.
return None
return ''

def get_status(self):
data = {'artist': None, 'album': None, 'title': None, 'file': None, 'length': None}
Expand All @@ -33,15 +33,15 @@ def get_status(self):
if line.startswith('State: '):
data['state'] = info_value.lower()
elif line.startswith('Artist: '):
data['artist'] = info_value or ''
data['artist'] = info_value
elif line.startswith('Album: '):
data['album'] = info_value or ''
data['album'] = info_value
elif line.startswith('SongTitle: '):
data['title'] = info_value or ''
data['title'] = info_value
elif line.startswith('File: '):
data['file'] = info_value
elif line.startswith('TotalSec: '):
data['length'] = int(info_value)
data['length'] = int(info_value or '0')

for k in data:
setattr(self, k, data[k])
Expand Down