Code: Alles auswählen
#!/usr/bin/env python
def parse_mpd(path):
with open(path) as mpdstate:
parsed = _parse(mpdstate)
return parsed
def _parse(file):
parsed = dict()
parsed['playlist'] = list()
parsed['current_name'] = None
pl = False
for line in file:
line = line.rstrip()
if not pl:
if ':' in line:
k,v = line.split(':', 1)
try:
v = int(v)
except ValueError:
v = v.lstrip()
parsed[k] = v
elif line == 'playlist_begin':
pl = True
else:
if line == 'playlist_end':
pl = False
else:
parsed['playlist'].append(line.split(':', 1)[1])
current = parsed.get('current', False)
if current:
parsed['current_name'] = parsed['playlist'][int(current)]
return parsed
if __name__ == '__main__':
from os.path import expanduser, join
user = expanduser('~')
print parse_mpd(join(user, '.mpd/mpdstate'))