-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxe.py
More file actions
executable file
·156 lines (133 loc) · 3.34 KB
/
Copy pathxe.py
File metadata and controls
executable file
·156 lines (133 loc) · 3.34 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/home/mgix/venv/bin/python3
# add user-defined app launcher PNG-icon buttons to the systray
# things we need
# --------------
import os
import sys
own_dir = os.path.dirname(os.path.realpath(__file__))
own_path = sys.executable + ' ' + os.path.realpath(__file__)
# fork a subprocess
# -----------------
def spawn(
cmd,
daemon
):
# do the daemon mode dance if requested
# -------------------------------------
if daemon:
# fork
# ----
if os.fork():
return
# batten down the hatches
# -----------------------
for i in range(0, 3):
try:
os.close(i)
except:
pass
# become process group leader
# ---------------------------
try:
os.setsid()
except:
pass
# chdir to something generic
# --------------------------
try:
os.chdir(own_dir)
except:
pass
# spawn child process
# -------------------
import shlex
import subprocess
try:
args = shlex.split(cmd)
child = subprocess.Popen(
args,
shell = False,
close_fds = True,
start_new_session=True,
stdin = subprocess.DEVNULL,
stdout = subprocess.DEVNULL,
stderr = subprocess.DEVNULL,
#creationflags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
)
# lest you be plagued by zombies
# ------------------------------
if not daemon:
child.wait()
except:
pass
# single-process tray icon manager
# --------------------------------
def launch():
print(f'in launch')
# hide these imports inside func, they don't play nice with fork
# ---------------------------------------------------------------
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GdkPixbuf
# straight from the GTK example code
# ----------------------------------
class Tray_Button:
def __init__(
self,
cmd_path,
icon_path
):
self.tray = Gtk.StatusIcon()
self.tray.set_tooltip_text(cmd_path)
self.tray.set_from_pixbuf(self.load_icon(icon_path))
self.tray.connect('activate', self.on_left_click)
self.cmd_path = cmd_path
def load_icon(
self,
icon_path
):
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(icon_path)
return pixbuf
except Exception as e:
print(f'error loading icon: {icon_path} - {e}')
return None
def on_left_click(
self,
icon
):
spawn(
f'{own_path} spawn {self.cmd_path}',
daemon = False
)
# load config
# -----------
config = None
try:
with open('config.json', 'r') as f:
import json
config = json.load(f)
except Exception as e:
print(f'could not read file config.json: {e}')
sys.exit(1)
# for all buttons in config
# -------------------------
tray_buttons = []
for app in config:
cmd = app['path']
icon = app['icon']
tray = Tray_Button(
cmd_path = cmd,
icon_path = icon
)
tray_buttons.append(tray)
# run the GTK main loop
# ---------------------
Gtk.main()
if __name__=='__main__':
if 1==len(sys.argv): spawn(f'{own_path} self', daemon = True)
elif 'spawn'==sys.argv[1]: spawn(sys.argv[2], daemon = True)
elif 'self'==sys.argv[1]: launch()
else: raise('no comprendo')
sys.exit(0)