Ejemplo n.º 1
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.º 2
0
    def restart(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Restart script

        Args:
            script (Script):

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

        result = ActionResult()

        if not script.allow_state_change():
            result.add_error(
                ErrorMessages.could_not_restart_script.format(
                    script.identifier()))
            return result, script

        temp_result, script = self.stop(script)

        if not temp_result.success():
            return temp_result, script

        return self._start_script(script)
Ejemplo n.º 3
0
    def remove_script(self, identifier: str) -> ActionResult:
        """
        Remove ID from all profiles.
        If path ID is library, remove all script in that library

        Args:
            identifier (str): script path or library path

        Returns:
            ActionResult: return warning only
        """

        result = ActionResult()

        # if identifier is a library, delete all script belongs to that library
        library = library_service.find(identifier)
        if library:
            for script in library.script_list:
                profiles = self.find_profiles_contains_script(
                    script.identifier())

                if profiles:
                    for profile in profiles:
                        profile.remove(script.identifier())
        else:
            profiles = self.find_profiles_contains_script(identifier)
            if profiles:
                for profile in profiles:
                    profile.remove(identifier)

        result.ignore_error()
        return result
Ejemplo n.º 4
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.º 5
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.º 6
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.º 7
0
    def _check_library_exists(self, identifier: str) \
            -> Tuple[ActionResult, Library]:
        result = ActionResult()

        library = self.find(identifier)
        if not library:
            result.add_error(
                ErrorMessages.could_not_find_library.format(identifier))

        return result, library
Ejemplo n.º 8
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.º 9
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.º 10
0
    def _check_profile_exists(self, identifier: str) \
            -> Tuple[ActionResult, Profile]:
        result = ActionResult()

        profile = self.find(identifier)
        if not profile:
            result.add_error(
                ErrorMessages.could_not_find_profile.format(identifier))
            return result, None

        return result, profile
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 _check_script_exists(self, identifier: str) \
            -> Tuple[ActionResult, Script]:
        result = ActionResult()

        script = self.find_script(identifier)
        if not script:
            result.add_error(
                ErrorMessages.could_not_find_script.format(identifier))
            return result, None

        return result, script
Ejemplo n.º 13
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.º 14
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.º 15
0
    def start(self, path: str) -> Tuple[ActionResult, Optional[Popen]]:
        """
        Open file path using ahk executable path

        Args:
            path (str): file path

        Returns:
            Tuple[ActionResult,  Optional[Popen]]:
                ActionResult: return error
                    if failed to start script
                 Optional[Popen]: launched ahk process
        """

        result = ActionResult()

        if not self.utility.is_file(path):
            result.add_error(
                ErrorMessages.script_path_not_valid.format(path))
            return result, None

        ahk_path = ProcessManager.configuration.utility.ahk_executable
        if not ahk_path:
            result.add_error(
                ErrorMessages.autohotkey_path_is_not_valid.format(ahk_path))
            return result, None

        try:
            process = subprocess.Popen([ahk_path, path], shell=False)
            return result, process
        except Exception:
            result.add_error(
                ErrorMessages.could_not_start_script.format(path))
            return result, None
Ejemplo n.º 16
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.º 17
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.º 18
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.º 19
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.º 20
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.º 21
0
    def _start_script(self, script: Script) -> Tuple[ActionResult, Script]:
        result = ActionResult()

        temp_result, process = self.process_manager.start(script.path)

        if not temp_result.success() or not process:
            return temp_result, script

        script.start(process)
        return result, script
Ejemplo n.º 22
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()
Ejemplo n.º 23
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.º 24
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.º 25
0
    def refresh(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Refresh script, check whether it exists

        Args:
            script (Script):

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

        result = ActionResult()

        # check whether file exists
        if not script.exists():
            result.add_error(
                ErrorMessages.could_not_refresh_script_script_removed.format(
                    script.identifier()))

        return result, script
Ejemplo n.º 26
0
    def open_explorer(self, path: str) -> ActionResult:
        """
        Open path in file explorer for windows only

        Args:
            path (str): file path

        Returns:
            ActionResult: return error
                if for some reason failed to open path
        """

        result = ActionResult()

        try:
            subprocess.Popen('explorer /select,{}'.format(path))
        except Exception:
            result.add_error(ErrorMessages.could_not_open_path.format(path))

        return result
Ejemplo n.º 27
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.º 28
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.º 29
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.º 30
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