Skip to content

Commit

Permalink
[goldendict:1.4] Support flatpaks and goldendict-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelSchneid3r committed May 15, 2024
1 parent faeddaf commit da0b761
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions goldendict/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 <i>GoldenDict</i>',
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]))],
)
)

0 comments on commit da0b761

Please sign in to comment.