def TransformEncode(r, encoding, undefined=''): """Returns the encoded value of the resource using encoding. Args: r: A JSON-serializable object. encoding: The encoding name. *base64* and *utf-8* are supported. undefined: Returns this value if the encoding fails. Returns: The encoded resource. """ if encoding == 'base64': try: b = base64.b64encode(console_attr.EncodeToBytes(r)) return console_attr.SafeText(b).rstrip('\n') except: # pylint: disable=bare-except, undefined for any exception return undefined try: return console_attr.SafeText(r, encoding) except: # pylint: disable=bare-except, undefined for any exception return undefined
def testEncodeToBytesIso8859_1(self): expected = b'\xdc\xf1\xee\xe7\xf2\xd0\xe9' actual = console_attr.EncodeToBytes(_ISO_8859_1) self.assertEqual(expected, actual)
def testEncodeToBytesUtf8(self): expected = (b'\xe1\xb9\xb2\xe1\xbe\x94\xe1\xb8\xaf\xc2\xa2' b'\xe2\x97\x8e\xe2\x85\xbe\xe2\x84\xaf') actual = console_attr.EncodeToBytes(_UTF8) self.assertEqual(expected, actual)
def testEncodeToBytesAscii(self): expected = b'Unicode' actual = console_attr.EncodeToBytes(_ASCII) self.assertEqual(expected, actual)