Esempio n. 1
0
    def refresh(self, library: Library) \
            -> Tuple[ActionResult, Library]:
        """
        Refresh library, remove not found scripts

        Args:
            library (Library): library object

        Returns:
            Tuple[ActionResult, Library]: return error
                when library not found
        """

        result = ActionResult()

        # if library not exists, the scripts are not exists either
        if not library.exists():
            result.add_error(
                ErrorMessages.library_path_not_exists_library_removed.format(
                    library.path))
            return result

        # fresh all script
        # remove script that is not found
        for script in library.script_list:
            # remove from the script list script failed to refresh
            temp_result, script = self.script_manager.refresh(script)
            result.merge(temp_result)

            if not temp_result.success() or not script:
                library.remove(script)

        return result
Esempio n. 2
0
    def pause_test(self, target: LibraryManager, library: Library):
        # * Prepare
        library.start()

        target.script_manager.pause = MagicMock(
            side_effect=lambda script: (ActionResult(), script))

        # * Act
        result, library = target.pause(library)

        # * Assert
        assert result.success()
        assert not result.messages
        assert library.is_paused()
Esempio n. 3
0
    def from_json(json_str):
        repo = LibraryRepository()

        for library in json_str['library_list']:
            repo.library_list.append(Library.from_json(library))

        return repo
Esempio n. 4
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. 5
0
    def resume_test(self, target: LibraryManager, library: Library):
        # * Prepare
        target.script_manager.resume = MagicMock(
            side_effect=lambda script: (ActionResult(), script))

        # * Act
        result, library = target.resume(library)

        # * Assert
        assert result.success()
        assert library.is_running()
Esempio n. 6
0
    def resume(self, library: Library) -> Tuple[ActionResult, Library]:
        """
        Resume library and all scripts

        Args:
            library (Library):

        Returns:
            Tuple[ActionResult, Library]:
        """

        result = ActionResult()
        if library.is_paused():
            library.resume()

        for script in library.script_list:
            temp_result, script = self.script_manager.resume(script)
            result.merge(temp_result)

        return result, library
Esempio n. 7
0
    def pause(self, library: Library) -> Tuple[ActionResult, Library]:
        """
        Pause library and all scripts

        Args:
            library (Library):

        Returns:
            Tuple[ActionResult, Library]:
                return error when script cannot be stopped
        """

        result = ActionResult()
        if library.is_running():
            library.pause()

        for script in library.script_list:
            temp_result, script = self.script_manager.pause(script)
            result.merge(temp_result)

        return result, library
Esempio n. 8
0
    def stop(self, library: Library) -> Tuple[ActionResult, Library]:
        """
        Stop library and all its scripts

        Args:
            library (Library): libray object

        Returns:
            Tuple[ActionResult, Library]:
                ActionResult: return error
                    if failed to stop any script
        """

        result = ActionResult()

        for script in library.script_list:
            temp_result, script = self.script_manager.stop(script)
            result.merge(temp_result)

        library.stop()
        return result, library
Esempio n. 9
0
    def resume_test_has_error(self, target: LibraryManager, library: Library):
        # * Prepare
        error_result = ActionResult()

        target.script_manager.resume = MagicMock(
            side_effect=lambda x: (error_result, x))

        # * Act
        result, library = target.resume(library)

        # * Assert
        assert result.success()
        assert library.is_running()
Esempio n. 10
0
    def init_library(self, path: str) -> Tuple[ActionResult, Library]:
        """
        Initialize directory and all its sub-directories

        Args:
            path (str): directory path

        Returns:
            Tuple[ActionResult, Library]: return error
                when path is not directory
        """
        result = ActionResult()
        path = self.utility.format_path(path)

        # check whether path is a valid directory
        if not self.utility.is_dir(path):
            result.add_error(
                ErrorMessages.path_is_not_directory_path.format(path))
            return result, None

        files = self.utility.get_files_in_directory(path)
        if not files:
            # ignore the directory do not have any file
            result.add_warning(ErrorMessages.directory_is_empty.format(path))
            return result, None

        library = Library(path)
        for file in files:
            # initialize script file
            temp_result, script = self.script_manager.init_script(file)
            result.merge(temp_result)

            # only add script to the list when no error returned.
            if temp_result.success() and script:
                library.add(script)

        return result, library
Esempio n. 11
0
    def reload(self, library: Library) -> Tuple[ActionResult, Library]:
        """
        Reload library path, search for new script that not in the library

        Args:
            library (Library): library object

        Returns:
            Tuple[ActionResult, Library]:
                ActionResult: return error when library path not exists
        """
        result = ActionResult()
        # check whether path is a valid directory
        if not library.exists():
            result.add_error(
                ErrorMessages.library_path_not_exists.format(library.path))
            return result, None

        files = self.utility.get_files_in_directory(library.path)
        if not files:
            # ignore the directory do not have any file
            return result, None

        for file in files:
            script = library.find(file)

            if not script:
                # initialize script file
                temp_result, script = self.script_manager.init_script(file)
                result.merge(temp_result)

                # only add script to the list when no error returned.
                if temp_result.success() and script:
                    library.add(script)

        return result, library
Esempio n. 12
0
    def library(self) -> Library:
        lib = Library(self.example_paths['directory'])
        for file in self.example_paths['files']:
            lib.add(Script(file))

        return lib