コード例 #1
0
    def _read(self,
              stream:  Union[TextIO, str],
              codec:   Codec,
              context: 'VerediContext') -> _DeserializeMidTypes:
        '''
        Read data from a single data stream.

        Returns:
          Output of yaml.safe_load().
          Mix of:
            - yaml objects
            - our subclasses of yaml objects
            - and python objects

        Raises:
          - exceptions.ReadError
          Maybes:
            - Other yaml/stream errors?
        '''
        self._log_data_processing(self.dotted,
                                  "Reading from '{}'...",
                                  type(stream),
                                  context=context)
        if isinstance(stream, TextIOBase):
            self._log_data_processing(self.dotted,
                                      "Seek to beginning first.",
                                      context=context)
            # Assume we are supposed to read the entire stream.
            stream.seek(0)

        data = None
        try:
            data = yaml.safe_load(stream)
            # TODO [2020-07-04]: may need to evaluate this in some way to get
            # it past its lazy loading... I want to catch any yaml exceptions
            # here and not let them infect unrelated code.
        except yaml.YAMLError as yaml_error:
            data = None
            error_info = {
                'data': stream,
            }
            error_info = self._stream_data(stream, error_info)
            msg = 'YAML failed while reading the data.'
            self._log_data_processing(self.dotted,
                                      msg,
                                      context=context,
                                      success=False)
            error = exceptions.ReadError(
                msg,
                context=context,
                data=error_info)
            raise log.exception(error, msg, context=context) from yaml_error

        self._log_data_processing(self.dotted,
                                  "Read YAML from '{}'!",
                                  type(stream),
                                  context=context,
                                  success=True)
        return data
コード例 #2
0
    def _read_all(self,
                  stream:  Union[TextIO, str],
                  codec:   Codec,
                  context: 'VerediContext') -> _DeserializeAllMidTypes:
        '''
        Read data from a single data stream.

        Returns:
          Output of yaml.safe_load_all().
          Mix of:
            - yaml objects
            - our subclasses of yaml objects
            - and python objects

        Raises:
          - exceptions.ReadError
          Maybes:
            - Other yaml/stream errors?
        '''
        self._log_data_processing(self.dotted,
                                  "Reading all from '{}'...",
                                  type(stream),
                                  context=context)
        if isinstance(stream, TextIOBase):
            self._log_data_processing(self.dotted,
                                      "Seek to beginning first.",
                                      context=context)
            # Assume we are supposed to read the entire stream.
            stream.seek(0)

        data = None
        try:
            data = yaml.safe_load_all(stream)
            data = self._finish_read(data)
        except yaml.YAMLError as yaml_error:
            data = None
            msg = 'YAML failed while reading all the data.'
            error_info = {
                'data': stream,
            }
            error_info = self._stream_data(stream, error_info)
            self._log_data_processing(self.dotted,
                                      msg,
                                      context=context,
                                      success=False)
            error = exceptions.ReadError(
                msg,
                context=context,
                data=error_info)
            raise log.exception(error, msg, context=context) from yaml_error

        self._log_data_processing(self.dotted,
                                  "Read all YAML from '{}'!",
                                  type(stream),
                                  context=context,
                                  success=True)
        return data
コード例 #3
0
    def deserialize_all(self,
                        stream:  Union[TextIO, str],
                        codec:   Codec,
                        context: 'VerediContext') -> DeserializeAllTypes:
        '''
        Read and deserializes data from a single data stream.

        Raises:
          - exceptions.ReadError
          Maybes:
            - Other yaml/stream errors?
        '''
        self._log_data_processing(self.dotted,
                                  "Deserializing all from '{}'...",
                                  type(stream),
                                  context=context)
        self._context_data(context, DataAction.LOAD, codec)
        data = self._read_all(stream, codec, context)
        if not data:
            msg = "Reading all yaml from stream resulted in no data."
            self._log_data_processing(self.dotted,
                                      msg,
                                      type(stream),
                                      context=context,
                                      success=False)
            error = exceptions.ReadError(
                msg,
                context=context,
                data={
                    'data': data,
                })
            raise log.exception(error, msg, context=context)

        # TODO: Here is where we'd check metadata for versions and stuff?

        # TODO: Here is where we'd verify data against templates
        # and requirements.

        # Convert YAML output to game data. YAML output is a mix of:
        #   - yaml objects
        #   - our subclasses of yaml objects
        #   - and python objects
        #
        # Game data should just be python: dicts, lists, str, int, etc.
        self._log_data_processing(self.dotted,
                                  "Deserialized all from '{}'!",
                                  type(stream),
                                  context=context,
                                  success=True)
        return self._decode_all(data, codec)
コード例 #4
0
    def deserialize_all(self,
                        stream:  Union[TextIO, str],
                        codec:   Codec,
                        context: 'VerediContext') -> DeserializeTypes:
        '''
        Read and deserializes all documents from the data stream. Expects a
        valid/normal json document in the stream, since there isn't a "document
        separator" in the json spec like in other serialized formats.

        Raises:
          - exceptions.ReadError
            - wrapping a library error?
        '''
        self._log_data_processing(self.dotted,
                                  "Deserializing all from '{}'...",
                                  type(stream),
                                  context=context)
        self._context_data(context, DataAction.LOAD, codec)
        error_info = {
            'data': stream,
        }
        data = self._read_all(stream, codec, context)
        if not data:
            msg = "Deserializing all JSON from {} resulted in no data."
            error_info = self._stream_data(stream, error_info)
            self._log_data_processing(self.dotted,
                                      msg,
                                      type(stream),
                                      context=context,
                                      success=False)
            # TODO v://future/2021-03-14T12:27:54 - log 'data' field for
            # error_info.
            error = exceptions.ReadError(
                msg,
                type(stream),
                context=context,
                data=error_info)
            raise log.exception(error, msg,
                                context=context)

        self._log_data_processing(self.dotted,
                                  "Deserialized all from '{}'!",
                                  type(stream),
                                  context=context,
                                  success=True)
        return self._decode_all(data, codec)
コード例 #5
0
    def deserialize(self,
                    stream:  Union[TextIO, str],
                    codec:   Codec,
                    context: 'VerediContext') -> DeserializeTypes:
        '''
        Read and deserializes data from a single data stream.

        Raises:
          - exceptions.ReadError
            - wrapped json.JSONDecodeError
          Maybes:
            - Other json/stream errors?
        '''
        self._log_data_processing(self.dotted,
                                  "Deserializing from '{}'...",
                                  type(stream),
                                  context=context)

        self._context_data(context, DataAction.LOAD, codec)
        data = self._read(stream, codec, context)
        if not data:
            msg = "Reading all json from stream resulted in no data."
            self._log_data_processing(self.dotted,
                                      msg,
                                      context=context,
                                      success=False)
            error = exceptions.ReadError(
                msg,
                context=context,
                data={
                    'data': data,
                })
            raise log.exception(error, msg,
                                context=context)

        self._log_data_processing(self.dotted,
                                  "Deserialized to '{}'!",
                                  type(data),
                                  context=context)
        return self._decode(data, codec)
コード例 #6
0
    def _read(self,
              stream:  Union[TextIO, str],
              codec:   Codec,
              context: 'VerediContext') -> _DeserializeMidTypes:
        '''
        Read data from a single data stream.

        Returns:
          Output of json.load().
          Mix of:
            - json objects
            - our subclasses of json objects
            - and python objects

        Raises:
          - exceptions.ReadError
            - wrapped json.JSONDecodeError
          Maybes:
            - Other json/file errors?
        '''
        self._log_data_processing(self.dotted,
                                  "Reading from '{}'...",
                                  type(stream),
                                  context=context)
        if isinstance(stream, TextIOBase):
            self._log_data_processing(self.dotted,
                                      "Seek to beginning first.",
                                      context=context)
            # Assume we are supposed to read the entire stream.
            stream.seek(0)

        data = None
        try:
            data = self._json_load(stream, context)

        except json.JSONDecodeError as json_error:
            data = None
            error_info = {
                'data': stream,
            }
            error_info = self._stream_data(stream, error_info)
            msg = f"Error reading json from stream: {stream}"
            self._log_data_processing(self.dotted,
                                      msg,
                                      context=context,
                                      success=False)
            # TODO v://future/2021-03-14T12:27:54 - log 'data' field for
            # error_info.
            error = exceptions.ReadError(
                msg,
                context=context,
                data=error_info)
            raise log.exception(error, msg,
                                context=context) from json_error

        self._log_data_processing(self.dotted,
                                  "Read JSON from '{}'!",
                                  type(stream),
                                  context=context,
                                  success=True)
        return data