This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
198 lines (166 loc) · 7.59 KB
/
__init__.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
# Copyright (C) 2016 - 2017 Sylvia van Os <sylvia@hackerchick.me>
# Copyright (C) 2019 Attila Szollosi
#
# Pext launcher module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import configparser
import html
import platform
import shlex
from distutils.util import strtobool
from os import access, environ, listdir, pathsep, X_OK
from os.path import expanduser, isfile, join
from re import sub, subn
from subprocess import Popen
from pext_base import ModuleBase
from pext_helpers import Action
class Module(ModuleBase):
def init(self, settings, q):
print(settings['use_path'])
self.use_path = False if 'use_path' in settings and settings['use_path'] == 'No' else True
self.executables = []
self.info_panels = {}
self.context_menus = {}
self.settings = settings
self.q = q
if self.settings['_api_version'] < [0, 8, 0]:
self.q.put([Action.critical_error, "API version 0.8.0 or higher is required."])
self._get_entries()
@staticmethod
def parse_desktop_entry(path):
parser = configparser.RawConfigParser(strict=False)
parser.read(path)
app = parser['Desktop Entry']['Name']
command = parser['Desktop Entry']['Exec']
# Remove deprecated and unrelevant field codes
command = sub(r'(?<!%)%[dDnNickvm]', '', command)
return app, command
def get_executables(self, paths=None):
"""Discover executables"""
if paths is None:
paths = environ['PATH'].split(pathsep)
for path in paths:
path = expanduser(path)
try:
for executable in listdir(path):
fullname = join(path, executable)
if isfile(fullname):
if platform.system() == 'Windows':
if not executable.endswith('.exe'):
continue
else:
if not access(fullname, X_OK):
continue
if executable not in self.executables:
self.executables.append(executable)
self.info_panels[executable] = "<b>{}</b>".format(html.escape(fullname))
self.context_menus[executable] = [fullname]
else:
self.info_panels[executable] += "<br/>{}".format(html.escape(fullname))
self.context_menus[executable].append(fullname)
except OSError:
pass
def _get_entries(self):
if platform.system() == 'Darwin':
if self.use_path:
self.get_executables()
else:
for executable in listdir('/Applications'):
if not executable.endswith('.app'):
continue
app_name = executable.rstrip('.app')
self.executables.append(app_name)
self.context_menus[app_name] = [executable]
elif platform.system() == 'Linux':
if self.use_path:
self.get_executables()
else:
try:
xdg_data_dirs = environ['XDG_DATA_DIRS'].split(pathsep)
except KeyError:
xdg_data_dirs = ['/usr/share', '/usr/local/share']
for directory in xdg_data_dirs:
directory = join(expanduser(directory), 'applications')
try:
for desktop_entry in listdir(directory):
if not desktop_entry.endswith('.desktop'):
continue
desktop_entry = join(directory, desktop_entry)
try:
app, command = self.parse_desktop_entry(desktop_entry)
except KeyError:
continue
# Add information about accepted arguments
for pattern, info in [(r'(?<!%)%[fF]', '<file name>'),
(r'(?<!%)%[uU]', '<URL>')]:
command_info, n = subn(pattern, info, command, count=1)
if n:
break
if app not in self.executables:
self.executables.append(app)
self.info_panels[app] = "<b>{}</b>".format(html.escape(command_info))
self.context_menus[app] = [command]
elif command not in self.context_menus[app]:
self.info_panels[app] += "<br/>{}".format(html.escape(command_info))
self.context_menus[app].append(command)
except OSError:
pass
elif platform.system() == 'Windows':
if self.use_path:
self.get_executables()
else:
import wmi
w = wmi.WMI()
paths = [p.InstallLocation for p in w.Win32_Product()]
self.get_executables(paths)
self.executables.sort()
self._set_entries()
def _set_entries(self):
if not self.use_path and platform.system() == 'Darwin':
self.q.put([Action.replace_command_list, []])
self.q.put([Action.replace_entry_list, self.executables])
else:
self.q.put([Action.replace_command_list, self.executables])
self.q.put([Action.replace_entry_list, []])
self.q.put([Action.replace_command_info_dict, self.info_panels])
self.q.put([Action.replace_command_context_dict, self.context_menus])
def stop(self):
pass
def selection_made(self, selection):
if len(selection) == 0:
self._set_entries()
elif len(selection) == 1:
command = (selection[0]["context_option"] if selection[0]["context_option"]
else self.context_menus[selection[0]["value"]][0])
args = selection[0]["args"]
if not self.use_path and platform.system() == 'Darwin':
if args:
command = ["open", "-a", shlex.quote(command), "--args"] + shlex.split(args)
else:
command = ["open", "-a", shlex.quote(command)]
Popen(command)
elif not self.use_path and platform.system() == 'Linux':
# TODO: Accept list of arguments
if args:
args = shlex.quote(args)
command = sub(r'(?<!%)%[fFuU]', args, command)
command = shlex.split(command)
Popen(command)
else:
if args:
command = [command] + shlex.split(args)
Popen(command)
self.q.put([Action.close])
def process_response(self, response):
pass