From da0b76137487798316395e0b959622e4c50a89b5 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Wed, 15 May 2024 15:30:36 +0200 Subject: [PATCH] [goldendict:1.4] Support flatpaks and goldendict-ng --- goldendict/__init__.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/goldendict/__init__.py b/goldendict/__init__.py index aebbc44..9887434 100644 --- a/goldendict/__init__.py +++ b/goldendict/__init__.py @@ -1,16 +1,18 @@ # -*- coding: utf-8 -*- # Copyright (c) 2017-2024 Manuel Schneider +import os +import shutil + from albert import * md_iid = '2.0' -md_version = '1.3' +md_version = '1.4' md_name = 'GoldenDict' -md_description = 'Searches in GoldenDict' +md_description = 'Quick access to GoldenDict' md_license = 'MIT' -md_url = 'https://github.com/albertlauncher/python/' +md_url = 'https://github.com/albertlauncher/python/tree/main/goldendict' md_authors = '@manuelschneid3r' -md_bin_dependencies = ['goldendict'] class Plugin(PluginInstance, TriggerQueryHandler): @@ -20,22 +22,36 @@ def __init__(self): id=md_id, name=md_name, description=md_description, - synopsis='query', defaultTrigger='gd ') PluginInstance.__init__(self, extensions=[self]) - self.iconUrls = ["xdg:goldendict"] - def handleTriggerQuery(self, query: TriggerQuery) -> None: - query_str = query.string.strip() - if not query_str: - return + commands = [ + '/var/lib/flatpak/exports/bin/org.goldendict.GoldenDict', # flatpak + '/var/lib/flatpak/exports/bin/io.github.xiaoyifang.goldendict_ng', # flatpak ng + 'goldendict', # native + 'goldendict-ng', # native ng + ] + + executables = [e for e in [shutil.which(c) for c in commands] if e] + + if not executables: + raise RuntimeError(f'None of the GoldenDict distributions found.') + + self.executable = executables[0] + self.iconUrls = [f'xdg:{os.path.basename(self.executable)}'] + + if len(executables) > 1: + warning(f"Multiple GoldenDict commands found: {', '.join(executables)}") + warning(f"Using {self.executable}") + def handleTriggerQuery(self, query: TriggerQuery): + q = query.string.strip() query.add( StandardItem( id=md_name, text=md_name, - subtext=f'Look up {query_str} using GoldenDict', + subtext=f"Look up '{q}' in GoldenDict", iconUrls=self.iconUrls, - actions=[Action(md_name, md_name, lambda: runDetachedProcess(['goldendict', query_str]))], + actions=[Action(md_name, md_name, lambda e=self.executable: runDetachedProcess([e, q]))], ) )