cmus - Make Cmus._query_cmus more pythonic

- Use string.partition instead of excessive slicing
- Use splitlines instead of split('\n'). This also reduces the times the
  for-loop is run.
This commit is contained in:
Raphael Scholer 2015-08-19 06:15:44 +02:00
parent e943831b12
commit 37c30f645d

View File

@ -52,19 +52,20 @@ class Cmus(IntervalModule):
return run_through_shell(cmdline, enable_shell=True) return run_through_shell(cmdline, enable_shell=True)
def _query_cmus(self): def _query_cmus(self):
status_dict = {} response = {}
cmd = self._cmus_command('query') cmd = self._cmus_command('query')
if not cmd.rc: if not cmd.rc:
status = cmd.out.split('\n') for line in cmd.out.splitlines():
for item in status: category, _, category_value = line.partition(' ')
split_item = item.split(' ') if category in ('set', 'tag'):
if split_item[0] in ['tag', 'set']: key, _, value = category_value.partition(' ')
key = '_'.join(split_item[:2]) key = '_'.join((category, key))
val = ' '.join([x for x in split_item[2:]]) response[key] = value
status_dict[key] = val
else: else:
status_dict[split_item[0]] = ' '.join(split_item[1:]) response[category] = category_value
return status_dict
return response
def run(self): def run(self):
status = self._query_cmus() status = self._query_cmus()