Esempio 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
Esempio 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
Esempio n. 3
0
def handleQuery(query):
    if query.isTriggered:
        fields = query.string.split()
        if len(fields) == 2:
            return buildItem(query.rawString, fields[0], fields[1])
        else:
            item = Item(id=__title__)
            item.text = __title__
            item.subtext = "Enter a query in the form of \"&lt;dstbase&gt; &lt;number&gt;\""
            return item
    else:
        fields = query.string.split()
        if len(fields) < 2:
            return
        src = base_prefixes[fields[:3]]
        number = fields[1]
        padding = 0 if len(fields) < 3 else fields[2]
        results = []
        for dst in sorted(base_prefixes.values().append(8)):
            if dst == src:
                continue
            results.append(buildItem(query.rawString, dst, number, padding))
        return results
Esempio n. 4
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