Esempio n. 1
0
    def test_serialize_round_trip(self):
        """
        Test the serialization of a dictionary with Nodes in various data structure
        Also make sure that the serialized data is json-serializable
        """
        node_a = Node().store()
        node_b = Node().store()

        data = {
            'test': 1,
            'list': [1, 2, 3, node_a],
            'dict': {
                ('Si', ): node_b,
                'foo': 'bar'
            },
            'baz': 'aar'
        }

        serialized_data = serialize_data(data)
        json_dumped = json.dumps(serialized_data)
        deserialized_data = deserialize_data(serialized_data)

        # For now manual element-for-element comparison until we come up with general
        # purpose function that can equate two node instances properly
        self.assertEqual(data['test'], deserialized_data['test'])
        self.assertEqual(data['baz'], deserialized_data['baz'])
        self.assertEqual(data['list'][:3], deserialized_data['list'][:3])
        self.assertEqual(data['list'][3].uuid,
                         deserialized_data['list'][3].uuid)
        self.assertEqual(data['dict'][('Si', )].uuid,
                         deserialized_data['dict'][('Si', )].uuid)
Esempio n. 2
0
    def on_create(self, pid, inputs, saved_state):
        super(WorkChain, self).on_create(pid, inputs, saved_state)

        if saved_state is None:
            self._context = self.Context()
        else:
            # Recreate the context
            self._context = self.Context(
                deserialize_data(saved_state[self._CONTEXT]))

            # Recreate the stepper
            if self._STEPPER_STATE in saved_state:
                self._stepper = self.spec().get_outline().create_stepper(self)
                self._stepper.load_position(saved_state[self._STEPPER_STATE])

            try:
                self._intersteps = [
                    load_with_classloader(b)
                    for b in saved_state[self._INTERSTEPS]
                ]
            except KeyError:
                self._intersteps = []

            try:
                self._barriers = [
                    WaitOn.create_from(b) for b in saved_state[self._BARRIERS]
                ]
            except KeyError:
                pass

            self._aborted = saved_state[self._ABORTED]
Esempio n. 3
0
    def decode_input_args(self, encoded):
        """
        Decode saved input arguments as they came from the saved instance state Bundle

        :param encoded:
        :return: The decoded input args
        """
        return deserialize_data(encoded)
Esempio n. 4
0
def decode_response(response):
    """
    Used by kiwipy to decode a message that has been received.  We check for
    any node PKs and convert these back into the corresponding node instance
    using `load_node`.  Any other entries are left untouched.

    .. see: `encode_response`

    :param response: The response string to decode
    :return: A data structure containing deserialized node instances
    """
    response = yaml.load(response)
    return deserialize_data(response)
Esempio n. 5
0
    def test_serialize_group(self):
        """
        Test that serialization and deserialization of Groups works.
        Also make sure that the serialized data is json-serializable
        """
        group_name = 'groupie'
        group_a = Group(name=group_name).store()

        data = {'group': group_a}

        serialized_data = serialize_data(data)
        json_dumped = json.dumps(serialized_data)
        deserialized_data = deserialize_data(serialized_data)

        self.assertEqual(data['group'].uuid, deserialized_data['group'].uuid)
        self.assertEqual(data['group'].name, deserialized_data['group'].name)
Esempio n. 6
0
    def load_instance_state(self, saved_state, load_context):
        super(WorkChain, self).load_instance_state(saved_state, load_context)
        # Load the context
        self._context = AttributeDict(
            **deserialize_data(saved_state[self._CONTEXT]))

        # Recreate the stepper
        self._stepper = None
        stepper_state = saved_state.get(self._STEPPER_STATE, None)
        if stepper_state is not None:
            self._stepper = self.spec().get_outline().recreate_stepper(
                stepper_state, self)

        self.set_logger(self._calc.logger)

        if self._awaitables:
            self.action_awaitables()