Example #1
0
def save_state_recursively(state, base_path, parent_path, as_copy=False):
    """Recursively saves a state to a json file

    It calls this method on all its substates.

    :param state: State to be stored
    :param base_path: Path to the state machine
    :param parent_path: Path to the parent state
    :param bool as_copy: Temporary storage flag to signal that the given path is not the new file_system_path
    :return:
    """
    from rafcon.core.states.execution_state import ExecutionState
    from rafcon.core.states.container_state import ContainerState

    state_path = os.path.join(parent_path, get_storage_id_for_state(state))
    state_path_full = os.path.join(base_path, state_path)
    if not os.path.exists(state_path_full):
        os.makedirs(state_path_full)

    storage_utils.write_dict_to_json(state, os.path.join(state_path_full, FILE_NAME_CORE_DATA))
    if not as_copy:
        state.file_system_path = state_path_full

    if isinstance(state, ExecutionState):
        save_script_file_for_state_and_source_path(state, state_path_full, as_copy)

    save_semantic_data_for_state(state, state_path_full)

    # create yaml files for all children
    if isinstance(state, ContainerState):
        remove_obsolete_folders(state.states.values(), os.path.join(base_path, state_path))
        for state in state.states.values():
            save_state_recursively(state, base_path, state_path, as_copy)
Example #2
0
    def store_meta_data(self, copy_path=None):
        """Save meta data of state model to the file system

        This method generates a dictionary of the meta data of the state together with the meta data of all state
        elements (data ports, outcomes, etc.) and stores it on the filesystem.
        Secure that the store meta data method is called after storing the core data otherwise the last_stored_path is
        maybe wrong or None.
        The copy path is considered to be a state machine file system path but not the current one but e.g.
        of a as copy saved state machine. The meta data will be stored in respective relative state folder in the state
        machine  hierarchy. This folder has to exist.
        Dues the core elements of the state machine has to be stored first.

        :param str copy_path: Optional copy path if meta data is not stored to the file system path of state machine
        """
        if copy_path:
            meta_file_path_json = os.path.join(copy_path, self.state.get_storage_path(), storage.FILE_NAME_META_DATA)
        else:
            if self.state.file_system_path is None:
                logger.error("Meta data of {0} can be stored temporary arbitrary but by default first after the "
                             "respective state was stored and a file system path is set.".format(self))
                return
            meta_file_path_json = os.path.join(self.state.file_system_path, storage.FILE_NAME_META_DATA)
        meta_data = deepcopy(self.meta)
        self._generate_element_meta_data(meta_data)
        storage_utils.write_dict_to_json(meta_data, meta_file_path_json)
Example #3
0
def save_state_machine_to_path(state_machine,
                               base_path,
                               delete_old_state_machine=False,
                               as_copy=False):
    """Saves a state machine recursively to the file system

    The `as_copy` flag determines whether the state machine is saved as copy. If so (`as_copy=True`), some state
    machine attributes will be left untouched, such as the `file_system_path` or the `dirty_flag`.

    :param rafcon.core.state_machine.StateMachine state_machine: the state_machine to be saved
    :param str base_path: base_path to which all further relative paths refers to
    :param bool delete_old_state_machine: Whether to delete any state machine existing at the given path
    :param bool as_copy: Whether to use a copy storage for the state machine
    """
    # warns the user in the logger when using deprecated names
    clean_path_from_deprecated_naming(base_path)

    state_machine.acquire_modification_lock()
    try:
        root_state = state_machine.root_state

        # clean old path first
        if delete_old_state_machine:
            if os.path.exists(base_path):
                shutil.rmtree(base_path)

        # Ensure that path is existing
        if not os.path.exists(base_path):
            os.makedirs(base_path)

        old_update_time = state_machine.last_update
        state_machine.last_update = storage_utils.get_current_time_string()
        state_machine_dict = state_machine.to_dict()
        storage_utils.write_dict_to_json(
            state_machine_dict, os.path.join(base_path, STATEMACHINE_FILE))

        # set the file_system_path of the state machine
        if not as_copy:
            state_machine.file_system_path = copy.copy(base_path)
        else:
            state_machine.last_update = old_update_time

        # add root state recursively
        remove_obsolete_folders([root_state], base_path)
        save_state_recursively(root_state, base_path, "", as_copy)

        if state_machine.marked_dirty and not as_copy:
            state_machine.marked_dirty = False
        logger.debug("State machine with id {0} was saved at {1}".format(
            state_machine.state_machine_id, base_path))
    except Exception:
        raise
    finally:
        state_machine.release_modification_lock()
 def save_file_data(self, path):
     """ Implements the abstract method of the ExternalEditor class.
     """
     try:
         # just create file with empty text first; this command also creates the whole path to the file
         filesystem.write_file(os.path.join(path, storage.SCRIPT_FILE), "", create_full_path=True)
         storage_utils.write_dict_to_json(self.model.state.semantic_data, os.path.join(path, storage.SEMANTIC_DATA_FILE))
     except IOError as e:
         # Only happens if the file doesnt exist yet and would be written to the temp folder.
         # The method write_file doesn't create the path
         logger.error('The operating system raised an error: {}'.format(e))
Example #5
0
def save_semantic_data_for_state(state, state_path_full):
    """Saves the semantic data in a separate json file.

    :param state: The state of which the script file should be saved
    :param str state_path_full: The path to the file system storage location of the state
    """

    destination_script_file = os.path.join(state_path_full, SEMANTIC_DATA_FILE)

    try:
        storage_utils.write_dict_to_json(state.semantic_data, destination_script_file)
    except IOError:
        logger.exception("Storing of semantic data for state {0} failed! Destination path: {1}".
                         format(state.get_path(), destination_script_file))
        raise
Example #6
0
    def store_meta_data(self, copy_path=None):
        """Save meta data of the state machine model to the file system

        This method generates a dictionary of the meta data of the state machine and stores it on the filesystem.

        :param str copy_path: Optional, if the path is specified, it will be used instead of the file system path
        """
        if copy_path:
            meta_file_json = os.path.join(copy_path, storage.FILE_NAME_META_DATA)
        else:
            meta_file_json = os.path.join(self.state_machine.file_system_path, storage.FILE_NAME_META_DATA)

        storage_utils.write_dict_to_json(self.meta, meta_file_json)

        self.root_state.store_meta_data(copy_path)
Example #7
0
def save_single_state(state, base_path, parent_path, as_copy=False):
    """Saves an ExecutionState directly in the provided path.

    :param state: ExecutionState to be stored
    :param base_path: Path to the state machine
    :param parent_path: Path to the parent state
    :param bool as_copy: Temporary storage flag to signal that the given path is not the new file_system_path
    """
    state_path_full = os.path.join(base_path, parent_path)
    if not os.path.exists(state_path_full):
        os.makedirs(state_path_full)

    storage_utils.write_dict_to_json(state, os.path.join(state_path_full, FILE_NAME_CORE_DATA))
    if not as_copy:
        state.file_system_path = state_path_full

    save_script_file_for_state_and_source_path(state, state_path_full, as_copy)