Beispiel #1
0
 def __init__(self, filename):
     #Information is stored as follows:
     #self.albums is a dictionary with each key being an artist.
     #self.albums[key] is also a dictionary with album names as keys
     #and an integer specifying the index of the album in self.tracks
     #self.tracks being a list of lists, each of which contains the
     #track metadata for an album as dictionaries.
     self.tagval = ""
     self.name = ""
     self.stargetting = False
     self.values = {}
     self.current = "nothing"
     self.tracks = []
     self.albums = {}
     self.extravalues = []
     self.extras = False
     self.extratype = ""
     parser = make_parser()
     parser.setContentHandler(self)
     try:
         parser.parse(filename)
     except ValueError, detail:
         if not os.path.exists(filename):
             msg = "%s does not exist." % filename
         else:
             msg = "%s is not a valid Rhythmbox XML database." % filename
         raise musiclib.MusicLibError(0, msg)
Beispiel #2
0
 def parse_file(self, filename):
     parser = make_parser()
     parser.setContentHandler(self)
     try:
         parser.parse(filename)
     except ValueError, detail:
         if not os.path.exists(filename):
             msg = "%s does not exist." % filename
         else:
             msg = "%s is not a valid Rhythmbox XML database." % filename
         raise musiclib.MusicLibError(0, msg)
Beispiel #3
0
class DBParser(ContentHandler):
    indent = " " * 4

    def __init__(self):
        #Information is stored as follows:
        #self.albums is a dictionary with each key being an artist.
        #self.albums[key] is also a dictionary with album names as keys
        #and an integer specifying the index of the album in self.tracks
        #self.tracks being a list of lists, each of which contains the
        #track metadata for an album as dictionaries.
        self.tagval = ""
        self.name = ""
        self.stargetting = False
        self.values = {}
        self.current = "#nothing"
        self.tracks = []
        self.albums = defaultdict(lambda: {})
        self.extravalues = []
        self.extras = False
        self.extratype = ""

    def characters(self, ch):
        try:
            self.values[self.current] += ch
        except KeyError:
            self.values[self.current] = ch

    def endElement(self, name):
        if name == "entry" and self.stargetting:
            self.stargetting = False

            if not self.extras:
                tag = {}
                for field, value in self.values.items():
                    try:
                        tag.update(CONVERSION[field](value.strip()))
                    except TypeError:
                        tag[CONVERSION[field]] = value.strip()
                    except KeyError:
                        tag['#' + field] = value.strip()

                f = ((k, v.strip()) for k, v in tag.items())
                tag = dict((k, v) for k, v in f if v)

                album = tag.get('album', u'')
                artist = tag.get('artist', u'')

                albums = self.albums[artist]
                if album not in albums:
                    albums[album] = len(self.tracks)
                    self.tracks.append([tag])
                else:
                    self.tracks[albums[album]].append(tag)
            else:
                x = ((k, v.strip()) for k, v in self.values.items())
                x = dict((k, v) for k, v in x if v)
                x['name'] = self.extratype
                self.extratype = ""
                self.extravalues.append(x)
                self.extras = False
            self.values = {}

    def _escapedText(self, txt):
        result = txt
        result = result.replace("&", "&")
        result = result.replace("<", "&lt;")
        result = result.replace(">", "&gt;")
        return result

    def parse_file(self, filename):
        parser = make_parser()
        parser.setContentHandler(self)
        try:
            parser.parse(filename)
        except ValueError, detail:
            if not os.path.exists(filename):
                msg = "%s does not exist." % filename
            else:
                msg = "%s is not a valid Rhythmbox XML database." % filename
            raise musiclib.MusicLibError(0, msg)
        except (IOError, OSError), detail:
            if not os.path.exists(filename):
                msg = "%s does not exist." % filename
            else:
                msg = detail.strerror()
            raise musiclib.MusicLibError(0, msg)