Source

VPlayer / fileloader.py


from PyQt4 import QtCore

from vplayer.track import Track
from vplayer.tagreader import TagReader

def flat(data):
    res = []
    for element in data:
        if isinstance(element,list):
            res += flat(element)
        else:
            res.append(element)
    return res

def resolve(d):
    if QtCore.QFileInfo(d).isDir():
        items = []
        for ent in QtCore.QDir(d).entryInfoList()[2:]:
            items.append(resolve(unicode(ent.absoluteFilePath())))
        return flat(items)
    else: return [d]

def read_files(filelist):
    data = []
    for name in filelist:
        data = flat(data + resolve(name))
    tagreader = TagReader()
    tracks = []
    for filename in data:
        realname = unicode(filename)
        tags = tagreader.read(unicode(realname))
        if tags["length"] != 0:
            tracks.append(Track(**tags))
    return tracks