예제 #1
0
파일: apev2.py 프로젝트: mattbun/sift
    def __setitem__(self, key, value):
        """'Magic' value setter.

        This function tries to guess at what kind of value you want to
        store. If you pass in a valid UTF-8 or Unicode string, it
        treats it as a text value. If you pass in a list, it treats it
        as a list of string/Unicode values.  If you pass in a string
        that is not valid UTF-8, it assumes it is a binary value.

        If you need to force a specific type of value (e.g. binary
        data that also happens to be valid UTF-8, or an external
        reference), use the APEValue factory and set the value to the
        result of that:
            from mutagen.apev2 import APEValue, EXTERNAL
            tag['Website'] = APEValue('http://example.org', EXTERNAL)
        """

        if not is_valid_apev2_key(key):
            raise KeyError("%r is not a valid APEv2 key" % key)

        if not isinstance(value, _APEValue):
            # let's guess at the content if we're not already a value...
            if isinstance(value, text_type):
                # unicode? we've got to be text.
                value = APEValue(utf8(value), TEXT)
            elif isinstance(value, list):
                # list? text.
                value = APEValue(bytearray([0]).join(map(utf8, value)), TEXT)
            else:
                try:
                    value.decode("utf-8")
                except UnicodeError:
                    # invalid UTF8 text, probably binary
                    value = APEValue(value, BINARY)
                else:
                    # valid UTF8, probably text
                    value = APEValue(utf8(value), TEXT)
        self.__casemap[key.lower()] = key
        self.__dict[key.lower()] = value
예제 #2
0
파일: apev2.py 프로젝트: Sophist-UK/mutagen
    def __setitem__(self, key, value):
        """'Magic' value setter.

        This function tries to guess at what kind of value you want to
        store. If you pass in a valid UTF-8 or Unicode string, it
        treats it as a text value. If you pass in a list, it treats it
        as a list of string/Unicode values.  If you pass in a string
        that is not valid UTF-8, it assumes it is a binary value.

        If you need to force a specific type of value (e.g. binary
        data that also happens to be valid UTF-8, or an external
        reference), use the APEValue factory and set the value to the
        result of that:
            from mutagen.apev2 import APEValue, EXTERNAL
            tag['Website'] = APEValue('http://example.org', EXTERNAL)
        """

        if not is_valid_apev2_key(key):
            raise KeyError("%r is not a valid APEv2 key" % key)

        if not isinstance(value, _APEValue):
            # let's guess at the content if we're not already a value...
            if isinstance(value, text_type):
                # unicode? we've got to be text.
                value = APEValue(utf8(value), TEXT)
            elif isinstance(value, list):
                # list? text.
                value = APEValue(bytearray([0]).join(map(utf8, value)), TEXT)
            else:
                try: value.decode("utf-8")
                except UnicodeError:
                    # invalid UTF8 text, probably binary
                    value = APEValue(value, BINARY)
                else:
                    # valid UTF8, probably text
                    value = APEValue(utf8(value), TEXT)
        self.__casemap[key.lower()] = key
        self.__dict[key.lower()] = value
예제 #3
0
 def test_low_unicode(self):
     value = utf8(u"1234")
     self.failUnlessEqual(value, b"1234")
     self.failUnless(isinstance(value, bytes))
예제 #4
0
 def test_bad_str(self):
     value = utf8(b"\xab\xde")
     # Two '?' symbols.
     self.failUnlessEqual(value, b"\xef\xbf\xbd\xef\xbf\xbd")
     self.failUnless(isinstance(value, bytes))
예제 #5
0
 def test_str(self):
     value = utf8("1234")
     self.failUnlessEqual(value, "1234")
     self.failUnless(isinstance(value, str))
예제 #6
0
파일: mp4.py 프로젝트: fourth-4/mutagen
 def __render_text(self, key, value, flags=1):
     if isinstance(value, (str, bytes)):
         value = [value]
     return self.__render_data(
         key, flags, [utf8(v) for v in value])
예제 #7
0
파일: apev2.py 프로젝트: mattbun/sift
def is_valid_apev2_key(key):
    key = utf8(key)
    return (2 <= len(key) <= 255 and min(key) >= ord(' ')
            and max(key) <= ord('~')
            and key not in [b"OggS", b"TAG", b"ID3", b"MP+"])
예제 #8
0
파일: apev2.py 프로젝트: Sophist-UK/mutagen
def is_valid_apev2_key(key):
    key = utf8(key)
    return (2 <= len(key) <= 255 and min(key) >= ord(' ') and max(key) <= ord('~') and
            key not in [b"OggS", b"TAG", b"ID3", b"MP+"])
예제 #9
0
파일: test__util.py 프로젝트: stedy/mutagen
 def test_high_unicode(self):
     value = utf8(u"\u1234")
     self.failUnlessEqual(value, b'\xe1\x88\xb4')
     self.failUnless(isinstance(value, bytes))
예제 #10
0
파일: test__util.py 프로젝트: stedy/mutagen
 def test_low_unicode(self):
     value = utf8(u"1234")
     self.failUnlessEqual(value, b"1234")
     self.failUnless(isinstance(value, bytes))
예제 #11
0
파일: test__util.py 프로젝트: stedy/mutagen
 def test_bad_str(self):
     value = utf8(b"\xab\xde")
     # Two '?' symbols.
     self.failUnlessEqual(value, b"\xef\xbf\xbd\xef\xbf\xbd")
     self.failUnless(isinstance(value, bytes))
예제 #12
0
 def setter(tags, key, value):
     tags[atomid] = [utf8(v) for v in value]
예제 #13
0
 def setter(tags, key, value):
     tags[atomid] = [utf8(v) for v in value]
예제 #14
0
파일: mp4.py 프로젝트: stedy/mutagen
 def __render_text(self, key, value, flags=1):
     if isinstance(value, (text_type, bytes)):
         value = [value]
     return self.__render_data(key, flags, [utf8(v) for v in value])
예제 #15
0
파일: test__util.py 프로젝트: imoapps/pymp3
 def test_str(self):
     value = utf8("1234")
     self.failUnlessEqual(value, "1234")
     self.failUnless(isinstance(value, str))
예제 #16
0
 def test_high_unicode(self):
     value = utf8(u"\u1234")
     self.failUnlessEqual(value, b'\xe1\x88\xb4')
     self.failUnless(isinstance(value, bytes))
예제 #17
0
 def test_str(self):
     value = utf8(b"1234")
     self.failUnlessEqual(value, b"1234")
     self.failUnless(isinstance(value, bytes))
예제 #18
0
파일: test__util.py 프로젝트: stedy/mutagen
 def test_str(self):
     value = utf8(b"1234")
     self.failUnlessEqual(value, b"1234")
     self.failUnless(isinstance(value, bytes))