Exemplo n.º 1
0
def buildItem(completion, dst, number, padding=0):
    item = Item(id=__title__, completion=completion)
    try:
        src = base_prefixes[number[:2]]
        dst = int(dst)
        padding = int(padding)
        integer = int(number, src)
        item.text = np.base_repr(integer, dst)
        if integer >= 0 and len(item.text) < padding:
            item.text = '0' * (padding - len(item.text)) + item.text
        item.subtext = "Base %s representation of %s (base %s)" % (dst, number,
                                                                   src)
        item.addAction(ClipAction("Copy to clipboard", item.text))
    except Exception as e:
        item.text = e.__class__.__name__
        item.subtext = str(e)
    return item
Exemplo n.º 2
0
def handleQuery(query):
    if not query.isTriggered:
        return

    item = Item(icon=ICON_PATH)
    stripped = query.string.strip()

    if stripped:
        with NamedTemporaryFile() as f:
            f.write(bytes(stripped, 'utf-8'))
            f.flush()
            output = subprocess.check_output(['wolframscript', '-print', '-f', f.name])
        result = str(output.strip(), 'utf-8')
        item.text = result
        item.subtext = 'Result'
        item.addAction(ClipAction('Copy result to clipboard', result))
    else:
        item.text = ''
        item.subtext = 'Type a Mathematica expression'

    return item
Exemplo n.º 3
0
def handleQuery(query):
    if query.isTriggered:
        fields = query.string.split()
        if len(fields) == 1:
            src = "de"
            dst = "en"
            txt = " ".join(fields)
        elif len(fields) >= 2:
            if fields[0] == ">":
                src = "de"
                dst = "en"
                txt = " ".join(fields[1:])
            elif fields[0] == "<":
                src = "en"
                dst = "de"
                txt = " ".join(fields[1:])
            else:
                src = fields[0]
                dst = fields[1]
                txt = " ".join(fields[2:])

                # If neither source nor destination are valid languages, assume that it is a standard case with
                # multiple words (cc kinder erziehen)
                if src not in AVAILABLE_LANGUAGES and dst not in AVAILABLE_LANGUAGES:
                    src = "de"
                    dst = "en"
                    txt = " ".join(fields)
                elif src not in ["de", "en"] and dst not in ["de", "en"]:
                    item = Item(id=__title__,
                                icon=iconPath,
                                completion=query.rawString)
                    item.text = "Unsupported language combination!"
                    item.subtext = "One language must be one of ['en', 'de']."
                    return item
                elif src not in AVAILABLE_LANGUAGES or dst not in AVAILABLE_LANGUAGES:
                    item = Item(id=__title__,
                                icon=iconPath,
                                completion=query.rawString)
                    item.text = "Unsupported language!"
                    item.subtext = "Source and destination language must be one of %s." % [
                        x for x in AVAILABLE_LANGUAGES.keys()
                    ]
                    return item
        else:
            item = Item(id=__title__,
                        icon=iconPath,
                        completion=query.rawString)
            item.text = __title__
            item.subtext = "Enter a query in the form of \"&lt;srclang&gt; &lt;dstlang&gt; &lt;text&gt;\""
            return item

        result = Dict.translate(txt, src, dst)
        items = []
        for input_word, output_word in result.translation_tuples:
            # critical(input_word + " | " + output_word)
            # Select correct value as translation
            # Dict.cc can only do <any language> <-> German or English
            # If src->dst are de->en or en->de, de is always the output
            # If src or dst is something else, it can vary, but we can detect that:
            #   * If src or dst is german, from or to is "Deutsch", the other one is the other language
            #   * If src or dst is english, form or to is "English", the other one is the other language
            if src == "de" and dst == "en":
                inp = output_word
                output = input_word
            elif src == "en" and dst == "de":
                inp = input_word
                output = output_word
            elif src == "de":
                inp, output = resolve(result.from_lang, result.to_lang,
                                      input_word, output_word, "Deutsch", True)
            elif dst == "de":
                inp, output = resolve(result.from_lang, result.to_lang,
                                      input_word, output_word, "Deutsch",
                                      False)
            elif src == "en":
                inp, output = resolve(result.from_lang, result.to_lang,
                                      input_word, output_word, "English", True)
            elif dst == "en":
                inp, output = resolve(result.from_lang, result.to_lang,
                                      input_word, output_word, "English",
                                      False)
            else:
                inp, output = error_text

            item = Item(id=__title__,
                        icon=iconPath,
                        completion=query.rawString)
            item.text = output
            item.subtext = "%s->%s translation of '%s'" % (src, dst, inp)
            item.addAction(ClipAction("Copy translation to clipboard", output))
            items.append(item)

        # If there where no results
        if len(items) == 0:
            item = Item(id=__title__,
                        icon=iconPath,
                        completion=query.rawString)
            item.text = "No results found!"
            items.append(item)
        else:
            # Add URL entry
            item = Item(id=__title__,
                        icon=iconPath,
                        completion=query.rawString)
            item.addAction(UrlAction("Open dict.cc", result.request_url))
            item.text = "Show all results (opens browser)"
            item.subtext = "Tip: You can scroll Alberts result list with your arrow keys to show more results."
            items.insert(0, item)

        return items
Exemplo n.º 4
0
def handleQuery(query):
    if query.isTriggered:
        if not query.string.strip():
            return Item(
                id="%s-update" % __name__,
                icon=iconPath,
                text="Pacman package manager",
                subtext="Enter the package you are looking for or hit enter to update.",
                completion=__triggers__,
                actions=[
                    TermAction("Update the system (no confirm)", "sudo pacman -Syu --noconfirm"),
                    TermAction("Update the system", "sudo pacman -Syu")
                ]
            )

        time.sleep(0.1)
        if not query.isValid:
            return

        # Get data. Results are sorted so we can merge in O(n)
        proc_s = subprocess.Popen(["expac", "-Ss", "%n\t%v\t%r\t%d\t%u\t%E", query.string],
                                  stdout=subprocess.PIPE, universal_newlines=True)
        proc_q = subprocess.Popen(["expac", "-Qs", "%n", query.string], stdout=subprocess.PIPE, universal_newlines=True)
        proc_q.wait()

        items = []
        pattern = re.compile(query.string, re.IGNORECASE)
        local_pkgs = set(proc_q.stdout.read().split('\n'))
        remote_pkgs = [tuple(line.split('\t')) for line in proc_s.stdout.read().split('\n')[:-1]]  # newline at end

        for pkg_name, pkg_vers, pkg_repo, pkg_desc, pkg_purl, pkg_deps in remote_pkgs:
            if not pattern.search(pkg_name):
                continue

            pkg_installed = True if pkg_name in local_pkgs else False

            item = Item(
                id="%s:%s:%s" % (__name__, pkg_repo, pkg_name),
                icon=iconPath,
                text="%s <i>%s</i> [%s]"
                     % (pattern.sub(lambda m: "<u>%s</u>" % m.group(0), pkg_name), pkg_vers, pkg_repo),
                subtext=("<b>[Installed]</b> %s%s" if pkg_installed else "%s%s")
                        % (pkg_desc, (" <i>(%s)</i>" % pkg_deps) if pkg_deps else ""),
                completion="%s%s" % (query.trigger, pkg_name)
            )
            items.append(item)

            if pkg_installed:
                item.addAction(TermAction("Remove", "sudo pacman -Rs %s" % pkg_name))
                item.addAction(TermAction("Reinstall", "sudo pacman -S %s" % pkg_name))
            else:
                item.addAction(TermAction("Install", "sudo pacman -S %s" % pkg_name))
            item.addAction(UrlAction("Show on packages.archlinux.org", "https://www.archlinux.org/packages/%s/x86_64/%s/" % (pkg_repo, pkg_name)))
            if pkg_purl:
                item.addAction(UrlAction("Show project website", pkg_purl))

        if items:
            return items
        else:
            return Item(
                id="%s-empty" % __name__,
                icon=iconPath,
                text="Search on archlinux.org",
                subtext="No results found in the local database",
                actions=[
                    UrlAction("Search on archlinux.org",
                              "https://www.archlinux.org/packages/?q=%s" % query.string.strip())
                ]
            )

    elif len(query.string.strip()) > 0 and ("pacman".startswith(query.string.lower()) or "update".startswith(query.string.lower())):
        return Item(
            id="%s-update" % __name__,
            icon=iconPath,
            text="Update all packages on the system",
            subtext="Synchronizes the repository databases and updates the system's packages",
            actions=[
                TermAction("Update the system (no confirm)", "sudo pacman -Syu --noconfirm"),
                TermAction("Update the system", "sudo pacman -Syu")
            ]
        )
Exemplo n.º 5
0
def buildVmItem(vm):
    item = Item(id=vm.__uuid__,
                icon=iconPath,
                text=vm.name,
                subtext="{vm.state}".format(vm=vm),
                completion=vm.name)

    if vm.state == MachineState.powered_off:  #1
        item.addAction(
            FuncAction(text="Start virtual machine",
                       callable=lambda: startVm(vm)))
    if vm.state == MachineState.saved:  #2
        item.addAction(
            FuncAction(text="Restore virtual machine",
                       callable=lambda: startVm(vm)))
        item.addAction(
            FuncAction(text="Discard saved state",
                       callable=lambda: discardSavedVm(vm)))
    if vm.state == MachineState.aborted:  #4
        item.addAction(
            FuncAction(text="Start virtual machine",
                       callable=lambda: startVm(vm)))
    if vm.state == MachineState.running:  #5
        item.addAction(
            FuncAction(text="Save virtual machine",
                       callable=lambda: saveVm(vm)))
        item.addAction(
            FuncAction(text="Power off via ACPI event (Power button)",
                       callable=lambda: acpiPowerVm(vm)))
        item.addAction(
            FuncAction(text="Turn off virtual machine",
                       callable=lambda: stopVm(vm)))
        item.addAction(
            FuncAction(text="Pause virtual machine",
                       callable=lambda: pauseVm(vm)))
    if vm.state == MachineState.paused:  #6
        item.addAction(
            FuncAction(text="Resume virtual machine",
                       callable=lambda: resumeVm(vm)))

    return item