Esempio n. 1
0
    def add(self, name: str = '') -> Tuple[ActionResult, Profile]:
        """
        Add profile into repository
            name (str, optional): Defaults to ''. Profile name can auto
                generate when not provided

        Returns:
            ActionResult: return error when profile name already exists
        """

        result = ActionResult()

        # * Check whether profile already exists
        profile = self.repository.find(name)
        if profile:
            result.add_error(
                ErrorMessages.profile_name_already_exists.format(name))
            return result, None

        # * Auto generate next available name
        if not name:
            name = self._get_next_profile_name()

        # * Initialize profile
        temp_result, profile = self.profile_manager.init_profile(name)
        result.merge(temp_result)
        if result.success() and profile:
            self.repository.add(profile)

        return result, profile
Esempio n. 2
0
    def remove(self, library: Library) -> ActionResult:
        """
        Remove library, trying to stop all scripts before removal

        Args:
            library (Library): library object

        Returns:
            ActionResult: return error if script cannot be stopped
        """

        result = ActionResult()

        # remove each script from the library
        i = 0
        while i < len(library.script_list):
            script = library.script_list[i]

            temp_result = self.script_manager.remove(script)
            result.merge(temp_result)

            if temp_result.success():
                del library.script_list[i]
                i -= 1

            i += 1

        if not result.success():
            result.add_error(
                ErrorMessages.
                could_not_remove_library_script_can_not_be_removed.format(
                    library.identifier()))

        return result
Esempio n. 3
0
    def remove_script(self, identifier: str) -> ActionResult:
        """
        Remove script using script ID

        Args:
            identifier (str): script path

        Returns:
            ActionResult: return error when failed to stop script,
                if script not exists return warning
        """

        result = ActionResult()

        # * Check if script exists in any of the library
        temp_result, script = self._check_script_exists(identifier)
        if not temp_result.success() or not script:
            temp_result.ignore_error()
            return temp_result

        # * Trying to stop script before remove
        temp_result = self.script_manager.remove(script)
        result.merge(temp_result)

        # * Find library containing script and remove it
        if result.success():
            library = self.find_library_contains_script(identifier)
            library.remove(script)

        return result
Esempio n. 4
0
    def resume(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Resume script if it is being paused

        Args:
            script (Script):

        Returns:
            Tuple[ActionResult, Script]:
                Return error when script cannot start
        """

        if not script.is_paused():
            return ActionResult(), script

        result = ActionResult()

        # check whether script is locked or not exists
        if not script.allow_state_change():
            result.add_error(
                ErrorMessages.could_not_start_locked_script.format(
                    script.identifier()))
            return result, script

        result, script = self._start_script(script)
        if result.success():
            script.resume()

        return result, script