forked from friday/ulauncher-clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
74 lines (56 loc) · 2.25 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
import subprocess
import sys
import gi
import os
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
gi.require_version('Notify', '0.7')
from gi.repository import Gdk, Gtk, Notify, GObject
from time import sleep
from distutils.spawn import find_executable as findExec
logger = logging.getLogger('ulauncher-clipboard')
Notify.init('ulauncher-clipboard-extension')
def execGet(*args):
return subprocess.check_output(list(args)).rstrip().decode('utf-8')
def tryOr(function, args, fallback=None):
try:
return function(*args)
except Exception:
return fallback
def tryInt(string, fallback=0):
return tryOr(int, [string, 10], fallback)
def pidOf(name):
# Get the first pid (there may be many space-separated pids), and parse to int
pids = tryOr(execGet, ['pidof', '-x', name], '').split(' ')
return tryInt(pids[0], None)
# Run each call in a new throwaway thread to escape Gtk.IconTheme.get_default() stupid cache
def getThemeIcon(name, size):
getIconCode = "Gtk.IconTheme.get_default().lookup_icon('{}', {}, 0).get_filename()".format(name, size)
return execGet(sys.executable, '-c', "import gi; gi.require_version('Gtk', '3.0'); from gi.repository import Gtk; print({})".format(getIconCode))
def setClipboard(text):
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)
clipboard.store()
GObject.timeout_add(1, Gtk.main_quit)
Gtk.main()
def showMessage(title, message, icon, expires=Notify.EXPIRES_NEVER, urgency=2):
message = Notify.Notification.new(title, message, icon)
message.set_timeout(expires)
message.set_urgency(urgency)
message.show()
return message
def ensureStatus(manager, attempts=0):
running = manager.isRunning()
if not running:
logger.info('Attempting to start manager %s', manager.name)
if not manager.canStart() or attempts > 30:
logger.warn('Could not start manager %s (%i attempts)', manager.name, 0)
return False
manager.start()
sleep(0.05 * attempts)
return ensureStatus(manager, attempts + 1)
isEnabled = manager.isEnabled()
if not isEnabled:
logger.warn('Clipboard manager %s is disabled', manager.name)
return isEnabled