Ejemplo n.º 1
0
    def refresh(self, profile: Profile) -> Tuple[ActionResult, Profile]:
        """
        Refresh profile and all its script
        Script not exists will be removed from profile

        Args:
            profile (Profile): profile object

        Returns:
            Tuple[ActionResult, Profile]: [description]
        """

        result = ActionResult()

        for script_id in profile.script_id_list:
            script = library_service.find_script(script_id)

            if script:
                temp_result, script = self.script_manager.refresh(script)
                result.merge(temp_result)

                # remove from the script list script failed to refresh
                if not temp_result.success() or not script:
                    profile.remove(script)
            else:
                result.add_error(
                    ErrorMessages.could_not_find_script.format(script_id))

        result.ignore_error()
        return result, profile
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    def add_script(self, profile: Profile, script: Script) \
            -> Tuple[ActionResult, Profile]:
        """
        Add script to profile

        Args:
            profile (Profile): profile object
            script (Script): script object

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

        result = ActionResult()

        # this shouldn't happen
        if not profile or not script:
            return result, None

        profile.add(script.identifier())

        if profile.is_running():
            temp_result, script = library_service.start_script(
                script.identifier())
            result.merge(temp_result)

        return result, profile
Ejemplo 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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def _add(self):
        name, ok = QInputDialog.getText(self, 'Add Profile', 'Profile name:')

        result = ActionResult()
        if ok and name:
            temp_result, _ = profile_service.add(name)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 8
0
    def _open_in_explorer(self, items: List[ListWidgetItem]):
        if not items:
            return

        result = ActionResult()
        for item in items:
            temp_result = self.process_manager.open_explorer(item.identifier)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 9
0
    def _remove(self, items: List[ListWidgetItem]):
        if not items:
            return

        result = ActionResult()
        for item in items:
            temp_result = profile_service.remove(item.identifier)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 10
0
    def _stop(self, items: List[ListWidgetItem]):
        if not items:
            return

        result = ActionResult()
        for item in items:
            temp_result, _ = library_service.stop(item.identifier)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 11
0
    def _stop(self, items: List[TableWidgetItem]):
        if not items:
            return

        result = ActionResult()
        for i in range(0, len(items), len(self.columns)):
            script_id = items[i].script_id
            temp_result, _ = library_service.stop_script(script_id)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 12
0
    def _open_in_explorer(self, items: List[TableWidgetItem]):
        if not items:
            return

        result = ActionResult()
        for i in range(0, len(items), len(self.columns)):
            script_id = items[i].script_id
            temp_result = self.process_manager.open_explorer(script_id)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 13
0
    def _remove(self, items: List[TableWidgetItem]):
        profile_id = self.app_service.app_model.selected_profile_id

        if not profile_id or not items:
            return

        result = ActionResult()
        for i in range(0, len(items), len(self.columns)):
            script_id = items[i].script_id
            temp_result = profile_service.remove_script_from_profile(
                profile_id, script_id)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 14
0
    def _remove(self, items: List[ListWidgetItem]):
        if not items:
            return

        result = ActionResult()

        for item in items:
            script_id = item.identifier
            temp_result = profile_service.remove_script(script_id)
            result.merge(temp_result)
            temp_result = library_service.remove(script_id)
            result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 15
0
    def _add(self):
        dialog = QFileDialog(self, 'Library Folders')
        dialog.setFileMode(QFileDialog.Directory)

        result = ActionResult()
        if dialog.exec_():
            filenames = dialog.selectedFiles()
            if not filenames:
                return

            for filename in filenames:
                temp_result = library_service.add(filename)
                result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 16
0
    def refresh(self) -> Tuple[ActionResult, ProfileRepository]:
        """
        Refresh profile repository, remove script which not found

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

        result = ActionResult()

        for profile in self.repository.profile_list:
            temp_result, profile = self.profile_manager.refresh(profile)
            result.merge(temp_result)

        result.ignore_error()
        return result, self.repository
Ejemplo n.º 17
0
    def stop_all(self) -> ActionResult:
        """
        Stop all library in the repository

        Returns:
            ActionResult: return only warning messages even when failed
        """

        result = ActionResult()

        for library in self.repository.library_list:
            temp_result, library = self.library_manager.stop(library)
            result.merge(temp_result)

        result.ignore_error()
        return result
Ejemplo n.º 18
0
    def resume_all(self) -> ActionResult:
        """
        Resume all libraries and scripts in the repository

        Returns:
            ActionResult: return warning message when script cannot start
        """

        result = ActionResult()

        for library in self.repository.library_list:
            temp_result, library = self.library_manager.resume(library)
            result.merge(temp_result)

        result.ignore_error()
        return result
Ejemplo n.º 19
0
    def stop_all(self) -> ActionResult:
        """
        Stop all script in repository

        Returns:
            ActionResult: only return warning
        """

        result = ActionResult()

        for profile in self.repository.profile_list:
            temp_result, profile = self.profile_manager.stop(profile)
            result.merge(temp_result)

        result.ignore_error()
        return result
Ejemplo n.º 20
0
    def remove(self, identifier: str) -> ActionResult:
        """
        Remove profile
        Profile will be removed from the repository even when script cannot
        be stopped

        Args:
            identifier (str): profile name

        Returns:
            ActionResult: return error if profile not
                found
                return warning if script cannot be
                stopped
        """

        result = ActionResult()

        temp_result, profile = self._check_profile_exists(identifier)
        if not temp_result.success() or not profile:
            temp_result.ignore_error()
            return temp_result, None

        # can be removed if not running
        if not profile.is_running():
            self.repository.remove(profile)
            return result

        # trying to stop script before remove
        for profile in self.repository.profile_list:
            i = 0
            while i < len(profile.script_id_list):
                script_id = profile.script_id_list[i]

                temp_result = self.remove_script_from_profile(
                    profile.identifier(), script_id)
                result.merge(temp_result)

                if temp_result.success():
                    i -= 1

                i += 1

        self.repository.remove(profile)
        result.ignore_error()
        return result
Ejemplo n.º 21
0
    def _add(self):
        profile_id = self.app_service.app_model.selected_profile_id
        if not profile_id:
            return

        dialog = AddScriptToProfileDialog(self)

        result = ActionResult()
        if dialog.exec_():
            scripts = dialog.get_selected_scripts()
            if not scripts:
                return

            for script in scripts:
                temp_result, _ = profile_service.add_script(
                    profile_id, script.identifier())
                result.merge(temp_result)

        self._post_process(result)
Ejemplo n.º 22
0
    def _init_library(self, path: str) -> ActionResult:
        result = ActionResult()

        library = self.find(path)
        if library:
            # ? Library already in the repository, reload library
            result.add_info(
                ErrorMessages.library_already_exists.format(library.path))
            temp_result, library = self.library_manager.reload(library)
            result.merge(temp_result)
        else:
            # ? Initialize library when not exists
            temp_result, library = self.library_manager.init_library(path)
            result.merge(temp_result)

            if temp_result.success() and library:
                self.repository.add(library)

        return result
Ejemplo n.º 23
0
    def force_start(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Force start script, script will be locked after this completed

        Args:
            script (Script):

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

        result = ActionResult()

        temp_result, script = self._start_script(script)
        result.merge(temp_result)

        if temp_result.success():
            script.lock()

        return result, script
Ejemplo n.º 24
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
Ejemplo n.º 25
0
    def stop(self, profile: Profile) -> Tuple[ActionResult, Profile]:
        """
        Stop profile and all its scripts

        Args:
            profile (Profile):  profile object

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

        result = ActionResult()

        for script_id in profile.script_id_list:
            temp_result, _ = library_service.stop_script(script_id)
            result.merge(temp_result)

        profile.stop()
        result.ignore_error()
        return result, profile
Ejemplo n.º 26
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
Ejemplo n.º 27
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
Ejemplo n.º 28
0
    def remove_script_from_profile(self, profile_id: str, script_id: str) \
            -> ActionResult:
        """
        Remove script from profile

        Args:
            profile_id (str): profile name
            script_id (str): script path

        Returns:
            ActionResult: return error when profile
                or script not found
        """

        result = ActionResult()

        temp_result, profile = self._check_profile_exists(profile_id)
        if not temp_result.success() or not profile:
            return temp_result

        if script_id not in profile.script_id_list:
            result.add_warning(
                ErrorMessages.script_not_in_profile.format(
                    profile=profile.identifier(), script=script_id))
            return result

        # try to stop script if no one else if running this script
        # get all profiles and libraries currently running contains script
        profiles = self.find_running_profiles_contains_script(script_id)
        library = library_service.find_library_contains_script(script_id)

        # check to see anything is running other than this script
        if len(profiles) <= 1 and not (library and library.is_running()):
            temp_result, _ = library_service.stop_script(script_id)
            result.merge(temp_result)

        # script will be removed even has error
        profile.remove(script_id)
        result.ignore_error()
        return result
Ejemplo n.º 29
0
    def add(self, path: str) -> ActionResult:
        """
        Initialize path into libraries, load all sub-directory and
        script files

        ? If library already exists, reload directory, add extra script into
        ? library. Otherwise, add libraries into repository.

        ! This method do not remove non exists script, call refresh() instead

        Args:
            path (str): directory path

        Returns:
            ActionResult: return error only when the given directory path is
                invalid
        """

        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
        # * Add path itself to repository
        temp_result = self._init_library(path)
        result.merge(temp_result)

        # * Initialize all sub directories
        directories = self.utility.get_directories(path)
        for directory in directories:
            temp_result = self._init_library(directory)
            result.merge(temp_result)

        # ignore errors, initialize library only return warning,
        result.ignore_error()
        return result
Ejemplo n.º 30
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