Exemple #1
0
    def open_file(self, path: str) -> Application:
        """Open a file with the default application.

        :param path: Path to file

        Example:

        .. code-block:: robotframework

            Open file    orders.xlsx
        """
        path: Path = Path(path)

        if not path.exists():
            raise FileNotFoundError(f"File does not exist: {path}")

        # TODO: Move implementations to platform-specific adapters
        if utils.is_windows():
            return self._open_default_windows(path)
        elif utils.is_linux():
            return self._open_default_linux(path)
        elif utils.is_macos():
            return self._open_default_macos(path)
        else:
            raise NotImplementedError("Not supported for current system")
Exemple #2
0
    def highlight_elements(self, locator: str):
        """Draw an outline around all matching elements."""
        if not utils.is_windows():
            raise NotImplementedError("Not supported on non-Windows platforms")

        matches = self.ctx.find(locator)

        for match in matches:
            if isinstance(match, Region):
                _draw_outline(match)
            elif isinstance(match, Point):
                region = Region(match.x - 5, match.y - 5, match.x + 5,
                                match.y + 5)
                _draw_outline(region)
            else:
                raise TypeError(f"Unknown location type: {match}")
Exemple #3
0
    def open_file(self, path: str) -> Application:
        """Open a file with the default application.

        :param path: Path to file

        Example:

        .. code-block:: robotframework

            Open file    orders.xlsx
        """
        name = Path(path).name

        if not Path(path).exists():
            raise FileNotFoundError(f"File does not exist: {path}")

        if utils.is_windows():
            # TODO: Find out way to get actual default application
            return self._create_app(name, ["start", "/WAIT"], shell=True)
        elif utils.is_macos():
            # TODO: Find out way to get actual default application
            return self._create_app(name, ["open", "-W", path])
        else:
            mimetype = output("xdg-mime", "query", "filetype", path)
            applications = output("xdg-mime", "query", "default", mimetype)
            default = output("xdg-mime", "query", "default", "text/plain")

            def executable(app):
                with open(Path("/usr/share/applications/") / app) as fd:
                    matches = re.search(r"\bExec=(\S+)", fd.read(),
                                        re.MULTILINE)
                    return matches.group(1) if matches else None

            for app in applications.split(";") + default.split(";"):
                exe = executable(app)
                if not exe:
                    continue
                try:
                    return self._create_app(name, [exe, path])
                except FileNotFoundError:
                    pass

            raise RuntimeError(f"No default application found for {path}")
Exemple #4
0
import os
import re
import subprocess
from pathlib import Path
from typing import List
from RPA.Desktop import utils
from RPA.Desktop.keywords import LibraryContext, keyword

if utils.is_windows():
    import winreg


def output(*args):
    """Run command and return output."""
    return subprocess.check_output(args).decode().strip()


class Application:
    """Container for launched application."""
    def __init__(self, name: str, args: List[str], shell: bool = False):
        self._name = name
        self._args = args
        self._shell = shell
        self._proc = None

    def __str__(self):
        return 'Application("{name}", pid:{pid})'.format(name=self._name,
                                                         pid=self.pid)

    @property
    def is_running(self):