Ejemplo n.º 1
0
    def set_tag_raw(self, tag, values, notify_changed=True):
        """
            Set the raw value of a tag.

            :param tag: The name of the tag to set.
            :param values: The value or values to set the tag to.
            :param notify_changed: whether to send a signal to let other
                parts of Exaile know there has been an update. Only set
                this to False if you know that no other parts of Exaile
                need to be updated.
        """
        # handle values that aren't lists
        if not isinstance(values, list):
            if not tag.startswith("__"): # internal tags dont have to be lists
                values = [values]

        # TODO: is this needed? why?
        # for lists, filter out empty values and convert to unicode
        if isinstance(values, list):
            values = [common.to_unicode(x, self.__tags.get('__encoding'))
                for x in values if x not in (None, '')]

        # save some memory by not storing null values.
        if not values:
            try:
                del self.__tags[tag]
            except KeyError:
                pass
        else:
            self.__tags[tag] = values

        self._dirty = True
        if notify_changed:
            event.log_event("track_tags_changed", self, tag)
Ejemplo n.º 2
0
 def strip_leading(value):
     """
         Strip special chars off the beginning of a field. If
         stripping the chars leaves nothing the original field is
         returned with only whitespace removed.
     """
     stripped = common.to_unicode(value).lstrip(
         " `~!@#$%^&*()_+-={}|[]\\\";'<>?,./")
     if stripped:
         return stripped
     else:
         return value.lstrip()
Ejemplo n.º 3
0
 def strip_leading(value):
     """
         Strip special chars off the beginning of a field. If
         stripping the chars leaves nothing the original field is
         returned with only whitespace removed.
     """
     stripped = common.to_unicode(value).lstrip(
         " `~!@#$%^&*()_+-={}|[]\\\";'<>?,./")
     if stripped:
         return stripped
     else:
         return value.lstrip()
Ejemplo n.º 4
0
    def parse_stream_tags(track, tags):
        """
            Called when a tag is found in a stream.
        """
        newsong=False

        for key in tags.keys():
            value = tags[key]
            try:
                value = common.to_unicode(value)
            except UnicodeDecodeError:
                logger.debug('  ' + key + " [can't decode]: " + `str(value)`)
                continue # TODO: What encoding does gst give us?

            value = [value]

            if key == '__bitrate' or key == 'bitrate':
                track.set_tag_raw('__bitrate', int(value[0]))

            # if there's a comment, but no album, set album to the comment
            elif key == 'comment' and not track.get_tag_raw('album'):
                track.set_tag_raw('album', value)

            elif key == 'album': track.set_tag_raw('album', value)
            elif key == 'artist': track.set_tag_raw('artist', value)
            elif key == 'duration': track.set_tag_raw('__length',
                    float(value[0])/1000000000)
            elif key == 'track-number': track.set_tag_raw('tracknumber', value)
            elif key == 'genre': track.set_tag_raw('genre', value)

            elif key == 'title':
                try:
                    if track.get_tag_raw('__rawtitle') != value:
                        track.set_tag_raw('__rawtitle', value)
                        newsong = True
                except AttributeError:
                    track.set_tag_raw('__rawtitle', value)
                    newsong = True

                title_array = value[0].split(' - ', 1)
                if len(title_array) == 1 or \
                        track.get_loc_for_io().lower().endswith(".mp3"):
                    track.set_tag_raw('title', value)
                else:
                    track.set_tag_raw('artist', [title_array[0]])
                    track.set_tag_raw('title', [title_array[1]])



        return newsong
Ejemplo n.º 5
0
 def _set_tag(self, raw, tag, value):
     if tag == 'metadata_block_picture':
         new_value = []
         for v in value:
             picture = Picture()
             picture.type = v.type
             picture.desc = v.desc
             picture.mime = v.mime
             picture.data = v.data
             new_value.append(base64.b64encode(picture.write()))
         value = new_value
     else:
         # vorbis has text based attributes, so convert everything to unicode
         value = [common.to_unicode(v) for v in value]
     CaseInsensitveBaseFormat._set_tag(self, raw, tag, value)
Ejemplo n.º 6
0
    def _set_tag(self, raw, tag, value):
        if tag == '__cover':
            raw.clear_pictures()
            for v in value:
                picture = Picture()
                picture.type = v.type
                picture.desc = v.desc
                picture.mime = v.mime
                picture.data = v.data
                raw.add_picture(picture)
            return

        # flac has text based attributes, so convert everything to unicode
        value = [common.to_unicode(v) for v in value]
        CaseInsensitveBaseFormat._set_tag(self, raw, tag, value)
Ejemplo n.º 7
0
    def set_tag_raw(self, tag, values, notify_changed=True):
        """
            Set the raw value of a tag.

            :param tag: The name of the tag to set.
            :param values: The value or values to set the tag to.
            :param notify_changed: whether to send a signal to let other
                parts of Exaile know there has been an update. Only set
                this to False if you know that no other parts of Exaile
                need to be updated.
        """
        if tag == '__loc':
            logger.warning('Setting "__loc" directly is forbidden, '
                           'use set_loc() instead.')
            return

        if tag in ('__basename',):
            logger.warning('Setting "%s" directly is forbidden.' % tag)
            return

        # Handle values that aren't lists
        if not isinstance(values, list):
            if not tag.startswith("__"): # internal tags dont have to be lists
                values = [values]

        # For lists, filter out empty values and convert string values to Unicode
        if isinstance(values, list):
            values = [
                common.to_unicode(v, self.__tags.get('__encoding'), 'replace')
                    if isinstance(v, basestring) else v
                for v in values
                    if v not in (None, '')
            ]

        # Save some memory by not storing null values.
        if not values:
            try:
                del self.__tags[tag]
            except KeyError:
                pass
        else:
            self.__tags[tag] = values

        self._dirty = True
        if notify_changed:
            event.log_event("track_tags_changed", self, tag)
Ejemplo n.º 8
0
    def set_tag_raw(self, tag, values, notify_changed=True):
        """
            Set the raw value of a tag.

            :param tag: The name of the tag to set.
            :param values: The value or values to set the tag to.
            :param notify_changed: whether to send a signal to let other
                parts of Exaile know there has been an update. Only set
                this to False if you know that no other parts of Exaile
                need to be updated.
        """
        if tag == '__loc':
            logger.warning('Setting "__loc" directly is forbidden, '
                           'use set_loc() instead.')
            return

        if tag in ('__basename',):
            logger.warning('Setting "%s" directly is forbidden.' % tag)
            return

        # Handle values that aren't lists
        if not isinstance(values, list):
            if not tag.startswith("__"): # internal tags dont have to be lists
                values = [values]

        # For lists, filter out empty values and convert string values to Unicode
        if isinstance(values, list):
            values = [
                common.to_unicode(v, self.__tags.get('__encoding'), 'replace')
                    if isinstance(v, basestring) else v
                for v in values
                    if v not in (None, '')
            ]

        # Save some memory by not storing null values.
        if not values:
            try:
                del self.__tags[tag]
            except KeyError:
                pass
        else:
            self.__tags[tag] = values

        self._dirty = True
        if notify_changed:
            event.log_event("track_tags_changed", self, tag)
Ejemplo n.º 9
0
    def _xform_set_values(self, tag, values):
        # Handle values that aren't lists
        if not isinstance(values, list):
            if not tag.startswith("__"):  # internal tags dont have to be lists
                values = [values]

        # For lists, filter out empty values and convert string values to Unicode
        if isinstance(values, list):
            values = [
                common.to_unicode(v, self.__tags.get('__encoding'), 'replace')
                if isinstance(v, basestring) else v for v in values
                if v not in (None, '')
            ]

        if values:
            return values

        return None
Ejemplo n.º 10
0
    def set_tag_raw(self, tag, values, notify_changed=True):
        """
            Set the raw value of a tag.

            :param tag: The name of the tag to set.
            :param values: The value or values to set the tag to.
            :param notify_changed: whether to send a signal to let other
                parts of Exaile know there has been an update. Only set
                this to False if you know that no other parts of Exaile
                need to be updated.
        """
        # handle values that aren't lists
        if not isinstance(values, list):
            if not tag.startswith("__"):  # internal tags dont have to be lists
                values = [values]

        # TODO: is this needed? why?
        # for lists, filter out empty values and convert to unicode
        if isinstance(values, list):
            values = [
                common.to_unicode(x, self.__tags.get('__encoding'))
                for x in values if x not in (None, '')
            ]

        # save some memory by not storing null values.
        if not values:
            try:
                del self.__tags[tag]
            except KeyError:
                pass
        else:
            self.__tags[tag] = values

        self._dirty = True
        if notify_changed:
            event.log_event("track_tags_changed", self, tag)
Ejemplo n.º 11
0
 def _set_tag(self, raw, tag, value):
     # vorbis has text based attributes, so convert everything to unicode
     BaseFormat._set_tag(self, raw, tag,
                         [common.to_unicode(v) for v in value])
Ejemplo n.º 12
0
    def get_tag_display(self, tag, join=True, artist_compilations=False,
            extend_title=True):
        """
            Get a tag value in a form suitable for display.

            :param tag: The name of the tag to get
            :param join: If True, joins lists of values into a
                single value.
            :param artist_compilations: If True, automatically handle
                albumartist and other compilations detections when
                tag=="albumartist".
            :param extend_title: If the title tag is unknown, try to
                add some identifying information to it.
        """
        if tag == '__loc':
            uri = Gio.File.new_for_uri(self.__tags['__loc']).get_parse_name()
            return uri.decode('utf-8')

        value = None
        if tag == "albumartist":
            if artist_compilations and self.__tags.get('__compilation'):
                value = self.__tags.get('albumartist', _VARIOUSARTISTSSTR)
            else:
                value = self.__tags.get('artist', _UNKNOWNSTR)
        elif tag in ('tracknumber', 'discnumber'):
            value = self.split_numerical(self.__tags.get(tag))[0] or u""
        elif tag in ('__length', '__startoffset', '__stopoffset'):
            value = self.__tags.get(tag, u"")
        elif tag in ('__rating', '__playcount'):
            value = self.__tags.get(tag, u"0")
        elif tag == '__bitrate':
            try:
                value = int(self.__tags['__bitrate']) // 1000
                if value == -1:
                    value = u""
                else:
                    #TRANSLATORS: Bitrate (k here is short for kbps).
                    value = _("%dk") % value
            except (KeyError, ValueError):
                value = u""
        elif tag == '__basename':
            value = self.get_basename_display()
        else:
            value = self.__tags.get(tag)

        if value is None:
            value = ''
            if tag == 'title':
                basename = self.get_basename_display()
                value = u"%s (%s)" % (_UNKNOWNSTR, basename)

        # Convert value to unicode or List[unicode]
        if isinstance(value, list):
            value = [common.to_unicode(x, errors='replace') for x in value]
        else:
            value = common.to_unicode(value, errors='replace')

        if join:
            value = self.join_values(value, _JOINSTR)

        return value
Ejemplo n.º 13
0
    def get_tag_display(self,
                        tag,
                        join=True,
                        artist_compilations=False,
                        extend_title=True):
        """
            Get a tag value in a form suitable for display.

            :param tag: The name of the tag to get
            :param join: If True, joins lists of values into a
                single value.
            :param artist_compilations: If True, automatically handle
                albumartist and other compilations detections when
                tag=="albumartist".
            :param extend_title: If the title tag is unknown, try to
                add some identifying information to it.
        """
        if tag == '__loc':
            uri = Gio.File.new_for_uri(self.__tags['__loc']).get_parse_name()
            return uri.decode('utf-8')

        value = None
        if tag == "albumartist":
            if artist_compilations and self.__tags.get('__compilation'):
                value = self.__tags.get('albumartist', _VARIOUSARTISTSSTR)
            else:
                value = self.__tags.get('artist', _UNKNOWNSTR)
        elif tag in ('tracknumber', 'discnumber'):
            value = self.split_numerical(self.__tags.get(tag))[0] or u""
        elif tag in ('__length', '__startoffset', '__stopoffset'):
            value = self.__tags.get(tag, u"")
        elif tag in ('__rating', '__playcount'):
            value = self.__tags.get(tag, u"0")
        elif tag == '__bitrate':
            try:
                value = int(self.__tags['__bitrate']) // 1000
                if value == -1:
                    value = u""
                else:
                    #TRANSLATORS: Bitrate (k here is short for kbps).
                    value = _("%dk") % value
            except (KeyError, ValueError):
                value = u""
        elif tag == '__basename':
            value = self.get_basename_display()
        else:
            value = self.__tags.get(tag)

        if value is None:
            value = ''
            if tag == 'title':
                basename = self.get_basename_display()
                value = u"%s (%s)" % (_UNKNOWNSTR, basename)

        # Convert value to unicode or List[unicode]
        if isinstance(value, list):
            value = [common.to_unicode(x, errors='replace') for x in value]
        else:
            value = common.to_unicode(value, errors='replace')

        if join:
            value = self.join_values(value, _JOINSTR)

        return value
Ejemplo n.º 14
0
 def _set_tag(self, raw, tag, value):
     # flac has text based attributes, so convert everything to unicode
     BaseFormat._set_tag(self, raw, tag, [common.to_unicode(v) for v in value])