Example #1
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
Example #2
0
    def pause(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Pause script

        Args:
            script (Script):

        Returns:
            Tuple[ActionResult, Script]: return error when process
                cannot be stopped
        """

        result = ActionResult()

        if not script.is_running():
            return result, script

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

        # trying to kill the process
        try:
            script.process.kill()
            script.pause()
        except Exception:
            result.add_error(
                ErrorMessages.could_not_stop_script.format(
                    script.identifier()))

        return result, script
Example #3
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
Example #4
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
Example #5
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)
Example #6
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
Example #7
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
Example #8
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
Example #9
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
Example #10
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
Example #11
0
    def _is_script_file(self, path: str) -> ActionResult:
        result = ActionResult()

        if not self.utility.is_file(path):
            result.add_error(ErrorMessages.path_is_not_file.format(path))
            return result

        is_script_file = self.utility.get_file_extension(path) in \
            ScriptManager.configuration.utility.file_types
        if not is_script_file:
            result.add_error(
                ErrorMessages.path_is_not_script_file.format(path))

        return result
Example #12
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
Example #13
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
Example #14
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
Example #15
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
Example #16
0
    def add_script(self, profile_id: str, script_id: str) \
            -> Tuple[ActionResult, Profile]:
        """
        Add script into profile

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

        Returns:
            Tuple[ActionResult, Profile]:
                ActionResult: return error when profile
                    or script not found
        """

        result = ActionResult()

        # check whether profile exists
        temp_result, profile = self._check_profile_exists(profile_id)
        if not temp_result.success() or not profile:
            return result, None

        # find profile
        profile = self.find(profile_id)
        if profile.has_script(script_id):
            result.add_warning(
                ErrorMessages.profile_already_contains_script.format(
                    script_id))
            return result, profile

        # find script
        script = library_service.find_script(script_id)
        if not script:
            result.add_error(
                ErrorMessages.could_not_find_script.format(script_id))
            return result, None

        return self.profile_manager.add_script(profile, script)
Example #17
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
Example #18
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
Example #19
0
    def start(self, script: Script) -> Tuple[ActionResult, Script]:
        """
        Start script

        Args:
            script (Script):

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

        result = ActionResult()

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

        if script.is_running():
            return result, script

        return self._start_script(script)