def handleQuery(query): results = [] if query.isTriggered: # avoid rate limiting sleep(0.2) if not query.isValid: return item = Item(id=__prettyname__, icon=iconPath, completion=query.rawString, text=__prettyname__, actions=[ ProcAction("Open the language configuration file.", commandline=[ "xdg-open", language_configuration_file ]) ]) if len(query.string) >= 2: for lang in languages: try: url = urltmpl % (lang, urllib.parse.quote_plus( query.string)) req = urllib.request.Request(url, headers={'User-Agent': ua}) with urllib.request.urlopen(req) as response: #print(type()) #try: data = json.loads(response.read().decode()) #except TypeError as typerr: # print("Urgh this type.error. %s" % typerr) translText = data[0][0][0] sourceText = data[2] if sourceText == lang: continue else: results.append( Item(id=__prettyname__, icon=iconPath, text="%s" % (translText), subtext="%s" % lang.upper(), actions=[ ClipAction( "Copy translation to clipboard", translText), UrlAction( "Open in your Browser", urlbrowser % (lang, query.string)) ])) except urllib.error.URLError as urlerr: print("Check your internet connection: %s" % urlerr) item.subtext = "Check your internet connection." return item else: item.subtext = "Enter a query: 'mtr <text>'. Languages {%s}" % ", ".join( languages) return item return results
def prepareErrorMessage(message): """ Prepare error messae :param str message: Message :return str """ item = Item(id=__prettyname__, icon=icon_path) item.text = __prettyname__ item.subtext = message return item
def handleQuery(query): if not query.isTriggered: return item = Item(completion=query.rawString, 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
def prepareResultsItem(query, result): """ Prepare resuls item :param str query : Query :param str result: Result :return Item """ value = result.decode('utf-8').split(' ')[0] item = Item(id=__prettyname__, icon=icon_path, completion=query.rawString) item.text = value item.subtext = result item.addAction(ClipAction("Copy result to clipboard", value)) return item