示例#1
0
 def test_alpha2(self):
     prefixes = [
         'en', 'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av',
         'ay', 'az', 'ba', 'be', 'bg', 'bh', 'bi', 'bm', 'bn', 'bo', 'br',
         'bs', 'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy', 'da',
         'de', 'dv', 'dz', 'ee', 'el', 'eo', 'es', 'et', 'eu', 'fa', 'ff',
         'fi', 'fj', 'fo', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gu', 'gv',
         'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz', 'ia', 'id',
         'ie', 'ig', 'ii', 'ik', 'io', 'is', 'it', 'iu', 'ja', 'jv', 'ka',
         'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku',
         'kv', 'kw', 'ky', 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu',
         'lv', 'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my',
         'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny',
         'oc', 'oj', 'om', 'or', 'os', 'pa', 'pi', 'pl', 'ps', 'pt', 'qu',
         'rm', 'rn', 'ro', 'ru', 'rw', 'sa', 'sc', 'sd', 'se', 'sg', 'si',
         'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv',
         'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr',
         'ts', 'tt', 'tw', 'ty', 'ug', 'uk', 'ur', 'uz', 've', 'vi', 'vo',
         'wa', 'wo', 'xh', 'yi', 'yo', 'za', 'zh', 'zu'
     ]
     for prefix in prefixes:
         metadata = deepcopy(example_010)
         metadata['stream']['metadata']['language'] = prefix
         claim = ClaimDict.load_dict(metadata)
         serialized = claim.serialized
         self.assertDictEqual(
             metadata, dict(ClaimDict.deserialize(serialized).claim_dict))
示例#2
0
def smart_decode(claim_value):
    """
    Decode a claim value

    Try decoding claim protobuf, if this fails try decoding json and migrating it.
    If unable to decode or migrate, raise DecodeError
    """

    # if already decoded, return
    if isinstance(claim_value, ClaimDict):
        return claim_value
    elif isinstance(claim_value, dict):
        return ClaimDict.load_dict(claim_value)

    # see if we were given a hex string, try decoding it
    skip_hex = sum(1 if char not in hex_chars else 0 for char in claim_value)
    if not skip_hex:
        try:
            decoded = claim_value.decode('hex')
            claim_value = decoded
        except (TypeError, ValueError):
            pass

    if claim_value.startswith("{"):
        # try deserializing protobuf, if that fails try parsing from json
        try:
            decoded_json = json.loads(claim_value)
        except (ValueError, TypeError):
            try:
                decoded_claim = ClaimDict.deserialize(claim_value)
                return decoded_claim
            except (DecodeError, InvalidAddress, KeyError):
                raise DecodeError()
        migrated_claim = migrate_json_claim_value(decoded_json)
        return migrated_claim
    else:
        try:
            decoded_claim = ClaimDict.deserialize(claim_value)
            return decoded_claim
        except (DecodeError, InvalidAddress, KeyError):
            try:
                decoded_json = json.loads(claim_value)
            except (ValueError, TypeError):
                raise DecodeError()
            migrated_claim = migrate_json_claim_value(decoded_json)
            return migrated_claim
示例#3
0
 def test_stream_is_not_certificate(self):
     deserialized_claim = ClaimDict.deserialize(
         example_010_serialized.decode('hex'))
     self.assertEquals(deserialized_claim.is_certificate, False)
示例#4
0
 def test_deserialize(self):
     deserialized_claim = ClaimDict.deserialize(
         example_010_serialized.decode('hex'))
     self.assertDictEqual(
         ClaimDict.load_dict(example_010).claim_dict,
         deserialized_claim.claim_dict)