Exemplo n.º 1
0
    def __init__(self, schema: Optional[Mapping[str, Any]]) -> None:
        self._schema = schema

        if schema is None:
            self._string = ""
            self._validate_row = validate_bytes
            self.encode_row = NOOPCodec({}).encode
            self.decode_row = NOOPCodec({}).decode
            self.empty_value = b""
        else:
            try:
                TSKITMetadataSchemaValidator.check_schema(schema)
            except jsonschema.exceptions.SchemaError as ve:
                raise exceptions.MetadataSchemaValidationError(str(ve)) from ve
            try:
                codec_cls = codec_registry[schema["codec"]]
            except KeyError:
                raise exceptions.MetadataSchemaValidationError(
                    f"Unrecognised metadata codec '{schema['codec']}'. "
                    f"Valid options are {str(list(codec_registry.keys()))}.")
            # Codecs can modify the schema, for example to set defaults as the validator
            # does not.
            schema = codec_cls.modify_schema(schema)
            codec_instance = codec_cls(schema)
            self._string = tskit.canonical_json(schema)
            self._validate_row = TSKITMetadataSchemaValidator(schema).validate
            self.encode_row = codec_instance.encode
            self.decode_row = codec_instance.decode
            # If None is allowed by the schema it gets used even in the presence of
            # default and required values.
            if "type" in schema and "null" in schema["type"]:
                self.empty_value = None
            else:
                self.empty_value = {}
Exemplo n.º 2
0
 def encode(self, obj: Any) -> bytes:
     return tskit.canonical_json(obj).encode()
Exemplo n.º 3
0
 def encode(self, obj: Any) -> bytes:
     try:
         return tskit.canonical_json(obj).encode()
     except TypeError as e:
         raise exceptions.MetadataEncodingError(
             f"Could not encode metadata of type {str(e).split()[3]}")