Ejemplo n.º 1
0
  def from_json_str(cls, json_str):
    """Convert json string representation into class instance.

    Args:
      json_str: json representation as string.

    Returns:
      New instance of the class with data loaded from json string.
    """
    return cls.from_json(simplejson.loads(json_str, cls=JsonDecoder))
Ejemplo n.º 2
0
    def from_json_str(cls, json_str):
        """Convert json string representation into class instance.

    Args:
      json_str: json representation as string.

    Returns:
      New instance of the class with data loaded from json string.
    """
        return cls.from_json(simplejson.loads(json_str, cls=JsonDecoder))
Ejemplo n.º 3
0
  def value(self):
    """Returns the value of this Slot."""
    if hasattr(self, '_value_decoded'):
      return self._value_decoded

    if self.value_blob is not None:
      encoded_value = self.value_blob.open().read()
    else:
      encoded_value = self.value_text

    self._value_decoded = simplejson.loads(encoded_value, cls=util.JsonDecoder)
    return self._value_decoded
Ejemplo n.º 4
0
    def value(self):
        """Returns the value of this Slot."""
        if hasattr(self, '_value_decoded'):
            return self._value_decoded

        if self.value_blob is not None:
            encoded_value = self.value_blob.open().read()
        else:
            encoded_value = self.value_text

        self._value_decoded = simplejson.loads(encoded_value,
                                               cls=util.JsonDecoder)
        return self._value_decoded
Ejemplo n.º 5
0
    def from_request(cls, request):
        """Create new TransientShardState from webapp request."""
        mapreduce_spec = MapreduceSpec.from_json_str(
            request.get("mapreduce_spec"))
        mapper_spec = mapreduce_spec.mapper
        input_reader_spec_dict = simplejson.loads(
            request.get("input_reader_state"), cls=json_util.JsonDecoder)
        input_reader = mapper_spec.input_reader_class().from_json(
            input_reader_spec_dict)
        initial_input_reader_spec_dict = simplejson.loads(
            request.get("initial_input_reader_state"),
            cls=json_util.JsonDecoder)
        initial_input_reader = mapper_spec.input_reader_class().from_json(
            initial_input_reader_spec_dict)

        output_writer = None
        if mapper_spec.output_writer_class():
            output_writer = mapper_spec.output_writer_class().from_json(
                simplejson.loads(request.get("output_writer_state", "{}"),
                                 cls=json_util.JsonDecoder))
            assert isinstance(output_writer, mapper_spec.output_writer_class(
            )), ("%s.from_json returned an instance of wrong class: %s" %
                 (mapper_spec.output_writer_class(), output_writer.__class__))

        handler = util.try_deserialize_handler(
            request.get("serialized_handler"))
        if not handler:
            handler = mapreduce_spec.mapper.handler

        return cls(mapreduce_spec.params["base_path"],
                   mapreduce_spec,
                   str(request.get("shard_id")),
                   int(request.get("slice_id")),
                   input_reader,
                   initial_input_reader,
                   output_writer=output_writer,
                   retries=int(request.get("retries")),
                   handler=handler)
Ejemplo n.º 6
0
  def from_request(cls, request):
    """Create new TransientShardState from webapp request."""
    mapreduce_spec = MapreduceSpec.from_json_str(request.get("mapreduce_spec"))
    mapper_spec = mapreduce_spec.mapper
    input_reader_spec_dict = simplejson.loads(request.get("input_reader_state"),
                                              cls=json_util.JsonDecoder)
    input_reader = mapper_spec.input_reader_class().from_json(
        input_reader_spec_dict)
    initial_input_reader_spec_dict = simplejson.loads(
        request.get("initial_input_reader_state"), cls=json_util.JsonDecoder)
    initial_input_reader = mapper_spec.input_reader_class().from_json(
        initial_input_reader_spec_dict)

    output_writer = None
    if mapper_spec.output_writer_class():
      output_writer = mapper_spec.output_writer_class().from_json(
          simplejson.loads(request.get("output_writer_state", "{}"),
                           cls=json_util.JsonDecoder))
      assert isinstance(output_writer, mapper_spec.output_writer_class()), (
          "%s.from_json returned an instance of wrong class: %s" % (
              mapper_spec.output_writer_class(),
              output_writer.__class__))

    handler = util.try_deserialize_handler(request.get("serialized_handler"))
    if not handler:
      handler = mapreduce_spec.mapper.handler

    return cls(mapreduce_spec.params["base_path"],
               mapreduce_spec,
               str(request.get("shard_id")),
               int(request.get("slice_id")),
               input_reader,
               initial_input_reader,
               output_writer=output_writer,
               retries=int(request.get("retries")),
               handler=handler)
Ejemplo n.º 7
0
    def make_value_from_datastore(self, value):
        """Convert value from datastore representation.

    Args:
      value: datastore value.

    Returns:
      value to store in the model.
    """

        if value is None:
            return None
        json = simplejson.loads(value, cls=JsonDecoder)
        if self.data_type == dict:
            return json
        return self.data_type.from_json(json)
Ejemplo n.º 8
0
  def make_value_from_datastore(self, value):
    """Convert value from datastore representation.

    Args:
      value: datastore value.

    Returns:
      value to store in the model.
    """

    if value is None:
      return None
    json = simplejson.loads(value, cls=JsonDecoder)
    if self.data_type == dict:
      return json
    return self.data_type.from_json(json)
Ejemplo n.º 9
0
    def params(self):
        """Returns the dictionary of parameters for this Pipeline."""
        if hasattr(self, '_params_decoded'):
            return self._params_decoded

        if self.params_blob is not None:
            value_encoded = self.params_blob.open().read()
        else:
            value_encoded = self.params_text

        value = simplejson.loads(value_encoded, cls=util.JsonDecoder)
        if isinstance(value, dict):
            kwargs = value.get('kwargs')
            if kwargs:
                adjusted_kwargs = {}
                for arg_key, arg_value in kwargs.iteritems():
                    # Python only allows non-unicode strings as keyword arguments.
                    adjusted_kwargs[str(arg_key)] = arg_value
                value['kwargs'] = adjusted_kwargs

        self._params_decoded = value
        return self._params_decoded
Ejemplo n.º 10
0
  def params(self):
    """Returns the dictionary of parameters for this Pipeline."""
    if hasattr(self, '_params_decoded'):
      return self._params_decoded

    if self.params_blob is not None:
      value_encoded = self.params_blob.open().read()
    else:
      value_encoded = self.params_text

    value = simplejson.loads(value_encoded, cls=util.JsonDecoder)
    if isinstance(value, dict):
      kwargs = value.get('kwargs')
      if kwargs:
        adjusted_kwargs = {}
        for arg_key, arg_value in kwargs.iteritems():
          # Python only allows non-unicode strings as keyword arguments.
          adjusted_kwargs[str(arg_key)] = arg_value
        value['kwargs'] = adjusted_kwargs

    self._params_decoded = value
    return self._params_decoded