| 1 |
''' |
| 2 |
Linux file chooser |
| 3 |
------------------ |
| 4 |
''' |
| 5 |
|
| 6 |
from plyer.facades import FileChooser |
| 7 |
from distutils.spawn import find_executable as which |
| 8 |
import os |
| 9 |
import subprocess as sp |
| 10 |
import time |
| 11 |
|
| 12 |
|
| 13 |
class SubprocessFileChooser: |
| 14 |
'''A file chooser implementation that allows using |
| 15 |
subprocess back-ends. |
| 16 |
Normally you only need to override _gen_cmdline, executable, |
| 17 |
separator and successretcode. |
| 18 |
''' |
| 19 |
|
| 20 |
executable = "" |
| 21 |
'''The name of the executable of the back-end. |
| 22 |
''' |
| 23 |
|
| 24 |
separator = "|" |
| 25 |
'''The separator used by the back-end. Override this for automatic |
| 26 |
splitting, or override _split_output. |
| 27 |
''' |
| 28 |
|
| 29 |
successretcode = 0 |
| 30 |
'''The return code which is returned when the user doesn't close the |
| 31 |
dialog without choosing anything, or when the app doesn't crash. |
| 32 |
''' |
| 33 |
|
| 34 |
path = None |
| 35 |
multiple = False |
| 36 |
filters = [] |
| 37 |
preview = False |
| 38 |
title = None |
| 39 |
icon = None |
| 40 |
show_hidden = False |
| 41 |
|
| 42 |
def __init__(self, *args, **kwargs): |
| 43 |
self._handle_selection = kwargs.pop( |
| 44 |
'on_selection', self._handle_selection |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
for i in kwargs: |
| 49 |
setattr(self, i, kwargs[i]) |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def _handle_selection(selection): |
| 53 |
''' |
| 54 |
Dummy placeholder for returning selection from chooser. |
| 55 |
''' |
| 56 |
return selection |
| 57 |
|
| 58 |
_process = None |
| 59 |
|
| 60 |
def _run_command(self, cmd): |
| 61 |
self._process = sp.Popen(cmd, stdout=sp.PIPE) |
| 62 |
while True: |
| 63 |
ret = self._process.poll() |
| 64 |
if ret is not None: |
| 65 |
if ret == self.successretcode: |
| 66 |
out = self._process.communicate()[0].strip().decode('utf8') |
| 67 |
return self._set_and_return_selection( |
| 68 |
self._split_output(out)) |
| 69 |
else: |
| 70 |
return self._set_and_return_selection(None) |
| 71 |
time.sleep(0.1) |
| 72 |
|
| 73 |
def _set_and_return_selection(self, value): |
| 74 |
self.selection = value |
| 75 |
self._handle_selection(value) |
| 76 |
return value |
| 77 |
|
| 78 |
def _split_output(self, out): |
| 79 |
'''This methods receives the output of the back-end and turns |
| 80 |
it into a list of paths. |
| 81 |
''' |
| 82 |
return out.split(self.separator) |
| 83 |
|
| 84 |
def _gen_cmdline(self): |
| 85 |
'''Returns the command line of the back-end, based on the current |
| 86 |
properties. You need to override this. |
| 87 |
''' |
| 88 |
raise NotImplementedError() |
| 89 |
|
| 90 |
def run(self): |
| 91 |
return self._run_command(self._gen_cmdline()) |
| 92 |
|
| 93 |
|
| 94 |
class ZenityFileChooser(SubprocessFileChooser): |
| 95 |
'''A FileChooser implementation using Zenity (on GNU/Linux). |
| 96 |
|
| 97 |
Not implemented features: |
| 98 |
* show_hidden |
| 99 |
* preview |
| 100 |
''' |
| 101 |
|
| 102 |
executable = "zenity" |
| 103 |
separator = "|" |
| 104 |
successretcode = 0 |
| 105 |
|
| 106 |
def _gen_cmdline(self): |
| 107 |
cmdline = [ |
| 108 |
which(self.executable), |
| 109 |
"--file-selection", |
| 110 |
"--confirm-overwrite" |
| 111 |
] |
| 112 |
if self.multiple: |
| 113 |
cmdline += ["--multiple"] |
| 114 |
if self.mode == "save": |
| 115 |
cmdline += ["--save"] |
| 116 |
elif self.mode == "dir": |
| 117 |
cmdline += ["--directory"] |
| 118 |
if self.path: |
| 119 |
cmdline += ["--filename", self.path] |
| 120 |
if self.title: |
| 121 |
cmdline += ["--name", self.title] |
| 122 |
if self.icon: |
| 123 |
cmdline += ["--window-icon", self.icon] |
| 124 |
for f in self.filters: |
| 125 |
if type(f) == str: |
| 126 |
cmdline += ["--file-filter", f] |
| 127 |
else: |
| 128 |
cmdline += [ |
| 129 |
"--file-filter", |
| 130 |
"{name} | {flt}".format(name=f[0], flt=" ".join(f[1:])) |
| 131 |
] |
| 132 |
return cmdline |
| 133 |
|
| 134 |
|
| 135 |
class KDialogFileChooser(SubprocessFileChooser): |
| 136 |
'''A FileChooser implementation using KDialog (on GNU/Linux). |
| 137 |
|
| 138 |
Not implemented features: |
| 139 |
* show_hidden |
| 140 |
* preview |
| 141 |
''' |
| 142 |
|
| 143 |
executable = "kdialog" |
| 144 |
separator = "\n" |
| 145 |
successretcode = 0 |
| 146 |
|
| 147 |
def _gen_cmdline(self): |
| 148 |
cmdline = [which(self.executable)] |
| 149 |
|
| 150 |
filt = [] |
| 151 |
|
| 152 |
for f in self.filters: |
| 153 |
if type(f) == str: |
| 154 |
filt += [f] |
| 155 |
else: |
| 156 |
filt += list(f[1:]) |
| 157 |
|
| 158 |
if self.mode == "dir": |
| 159 |
cmdline += [ |
| 160 |
"--getexistingdirectory", |
| 161 |
(self.path if self.path else os.path.expanduser("~")) |
| 162 |
] |
| 163 |
elif self.mode == "save": |
| 164 |
cmdline += [ |
| 165 |
"--getsavefilename", |
| 166 |
(self.path if self.path else os.path.expanduser("~")), |
| 167 |
" ".join(filt) |
| 168 |
] |
| 169 |
else: |
| 170 |
cmdline += [ |
| 171 |
"--getopenfilename", |
| 172 |
(self.path if self.path else os.path.expanduser("~")), |
| 173 |
" ".join(filt) |
| 174 |
] |
| 175 |
if self.multiple: |
| 176 |
cmdline += ["--multiple", "--separate-output"] |
| 177 |
if self.title: |
| 178 |
cmdline += ["--title", self.title] |
| 179 |
if self.icon: |
| 180 |
cmdline += ["--icon", self.icon] |
| 181 |
return cmdline |
| 182 |
|
| 183 |
|
| 184 |
class YADFileChooser(SubprocessFileChooser): |
| 185 |
'''A NativeFileChooser implementation using YAD (on GNU/Linux). |
| 186 |
|
| 187 |
Not implemented features: |
| 188 |
* show_hidden |
| 189 |
''' |
| 190 |
|
| 191 |
executable = "yad" |
| 192 |
separator = "|?|" |
| 193 |
successretcode = 0 |
| 194 |
|
| 195 |
def _gen_cmdline(self): |
| 196 |
cmdline = [ |
| 197 |
which(self.executable), |
| 198 |
"--file-selection", |
| 199 |
"--confirm-overwrite", |
| 200 |
"--geometry", |
| 201 |
"800x600+150+150" |
| 202 |
] |
| 203 |
if self.multiple: |
| 204 |
cmdline += ["--multiple", "--separator", self.separator] |
| 205 |
if self.mode == "save": |
| 206 |
cmdline += ["--save"] |
| 207 |
elif self.mode == "dir": |
| 208 |
cmdline += ["--directory"] |
| 209 |
if self.preview: |
| 210 |
cmdline += ["--add-preview"] |
| 211 |
if self.path: |
| 212 |
cmdline += ["--filename", self.path] |
| 213 |
if self.title: |
| 214 |
cmdline += ["--name", self.title] |
| 215 |
if self.icon: |
| 216 |
cmdline += ["--window-icon", self.icon] |
| 217 |
for f in self.filters: |
| 218 |
if type(f) == str: |
| 219 |
cmdline += ["--file-filter", f] |
| 220 |
else: |
| 221 |
cmdline += [ |
| 222 |
"--file-filter", |
| 223 |
"{name} | {flt}".format(name=f[0], flt=" ".join(f[1:])) |
| 224 |
] |
| 225 |
return cmdline |
| 226 |
|
| 227 |
|
| 228 |
CHOOSERS = { |
| 229 |
"gnome": ZenityFileChooser, |
| 230 |
"kde": KDialogFileChooser, |
| 231 |
"yad": YADFileChooser |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
class LinuxFileChooser(FileChooser): |
| 236 |
'''FileChooser implementation for GNu/Linux. Accepts one additional |
| 237 |
keyword argument, *desktop_override*, which, if set, overrides the |
| 238 |
back-end that will be used. Set it to "gnome" for Zenity, to "kde" |
| 239 |
for KDialog and to "yad" for YAD (Yet Another Dialog). |
| 240 |
If set to None or not set, a default one will be picked based on |
| 241 |
the running desktop environment and installed back-ends. |
| 242 |
''' |
| 243 |
|
| 244 |
desktop = None |
| 245 |
if (str(os.environ.get("XDG_CURRENT_DESKTOP")).lower() == "kde" |
| 246 |
and which("kdialog")): |
| 247 |
desktop = "kde" |
| 248 |
elif (str(os.environ.get("DESKTOP_SESSION")).lower() == "trinity" |
| 249 |
and which('kdialog')): |
| 250 |
desktop = "kde" |
| 251 |
elif which("yad"): |
| 252 |
desktop = "yad" |
| 253 |
elif which("zenity"): |
| 254 |
desktop = "gnome" |
| 255 |
|
| 256 |
def _file_selection_dialog(self, desktop_override=desktop, **kwargs): |
| 257 |
if not desktop_override: |
| 258 |
desktop_override = self.desktop |
| 259 |
|
| 260 |
if not desktop_override: |
| 261 |
raise OSError("No back-end available. Please install one.") |
| 262 |
|
| 263 |
chooser = CHOOSERS[desktop_override] |
| 264 |
c = chooser(**kwargs) |
| 265 |
return c.run() |
| 266 |
|
| 267 |
|
| 268 |
def instance(): |
| 269 |
return LinuxFileChooser() |