Skip to content

Commit 84fac3c

Browse files
authored
Merge pull request #2 from joystein/master
Support python versions < 3.7
2 parents 1a50f0f + 80cb6f6 commit 84fac3c

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pytest_cache
2+
.tox
3+
libinput_gestures_qt.egg-info
4+
*/__pycache__
5+

libinput_gestures_qt/main.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,21 @@
5555
--------------
5656
"""
5757

58+
import os
59+
import re
5860
import sys
61+
import subprocess
62+
import functools
63+
from pathlib import Path
5964
from PyQt5 import QtWidgets, QtCore, QtGui
6065
from libinput_gestures_qt import main_window
6166
from libinput_gestures_qt import edit_window
6267

63-
import subprocess
64-
from pathlib import Path
65-
import os
66-
67-
import re
68+
# NOTE `capture_output` was introduced in 3.7
69+
if sys.version_info < (3, 7):
70+
run = functools.partial(subprocess.run, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
71+
else:
72+
run = functools.partial(subprocess.run, capture_output=False)
6873

6974
HOME = str(Path.home())
7075
CONFIG_LOCATION = HOME + '/.config/libinput-gestures.conf'
@@ -188,6 +193,7 @@ def write_config(new_conf):
188193
with open(CONFIG_LOCATION, 'w') as config:
189194
config.write(''.join(new_conf))
190195

196+
191197
def write_defaults(defaults):
192198
"""Write default settings and backs old config up
193199
@@ -200,6 +206,7 @@ def write_defaults(defaults):
200206
config.write(defaults)
201207
return defaults
202208

209+
203210
def fix_config():
204211
"""Fixes config
205212
@@ -231,6 +238,7 @@ def fix_config():
231238
fixed_conf.append(line)
232239
write_config(''.join(fixed_conf))
233240

241+
234242
def resub_config():
235243
"""Delete multiple tabs and spaces"""
236244
conf = ''.join(read_config())
@@ -255,13 +263,14 @@ def find_key_combo(qt_key_combo):
255263
xdotool_key_combo.append(lowered_key)
256264
return '+'.join(xdotool_key_combo)
257265

266+
258267
def get_qdbus_name():
259268
"""It's either 'qdbus' or 'qdbus-qt5'"""
260269
try:
261-
subprocess.run(['qdbus'], capture_output=True)
270+
run(['qdbus'])
262271
return 'qdbus'
263272
except FileNotFoundError:
264-
subprocess.run(['qdbus-qt5'], capture_output=True)
273+
run(['qdbus-qt5'])
265274
return 'qdbus-qt5'
266275

267276

@@ -328,13 +337,12 @@ def __init__(self, parent=None):
328337
self.actionLicense.triggered.connect(self.show_copyleft)
329338
#-->
330339
try:
331-
subprocess.run(['libinput-gestures-setup', 'status'], capture_output=True)
340+
run(['libinput-gestures-setup', 'status'])
332341
self.installed = True
333342
except FileNotFoundError:
334343
QtWidgets.QMessageBox.about(self, "Problem", "Cannot find libinput-gestures. Are you sure it is installed correctly?")
335344
self.installed = False
336-
337-
345+
338346
def start_adding(self):
339347
"""Shows EditGestures window"""
340348
self.adding = EditGestures(self)
@@ -393,26 +401,26 @@ def run_libinput_gestures(self):
393401
def kill_libinput_gestures(self):
394402
"""Fing libinput-gestures and kill it"""
395403
if self.installed:
396-
p = subprocess.run(['ps', 'axu'], capture_output=True)
404+
p = run(['ps', 'axu'])
397405
s = p.stdout.decode('utf-8').split('\n')
398406
for proc in s:
399407
if 'python3' in proc and 'libinput-gestures' in proc and 'libinput-gestures-qt' not in proc:
400408
try:
401-
subprocess.run(['kill', proc.split()[1]], capture_output=True)
409+
run(['kill', proc.split()[1]])
402410
except Exception:
403411
pass
404412
self.display_status()
405413

406414
def set_to_autostart(self):
407415
"""Sets libinput-gestures to autostart"""
408416
if self.installed:
409-
subprocess.run(['libinput-gestures-setup', 'autostart'], capture_output=True)
417+
run(['libinput-gestures-setup', 'autostart'])
410418
self.display_status()
411419

412420
def disable_autostart(self):
413421
"""Set libinput-gestures to autostop"""
414422
if self.installed:
415-
subprocess.run(['libinput-gestures-setup', 'autostop'], capture_output=True)
423+
run(['libinput-gestures-setup', 'autostop'])
416424
self.display_status()
417425

418426
'''
@@ -422,7 +430,7 @@ def disable_autostart(self):
422430
def display_status(self):
423431
"""Check status of libinput-gestures and shows it in message box"""
424432
if self.installed:
425-
status = subprocess.run(['libinput-gestures-setup', 'status'], capture_output=True)
433+
status = run(['libinput-gestures-setup', 'status'])
426434
status = status.stdout.decode('utf-8')
427435
installed = 'no'
428436
if 'is installed' in status:
@@ -439,21 +447,21 @@ def display_status(self):
439447
def restart_utility(self):
440448
"""Runs 'libinput-gestures-setup restart', displays output in message box"""
441449
if self.installed:
442-
status = subprocess.run(['libinput-gestures-setup', 'restart'], capture_output=True)
450+
status = run(['libinput-gestures-setup', 'restart'])
443451
status = status.stdout.decode('utf-8')
444452
QtWidgets.QMessageBox.about(self, "Status", status)
445453

446454
def stop_utility(self):
447455
"""Runs 'libinput-gestures-setup stop', displays output in message box"""
448456
if self.installed:
449-
status = subprocess.run(['libinput-gestures-setup', 'stop'], capture_output=True)
457+
status = run(['libinput-gestures-setup', 'stop'])
450458
status = status.stdout.decode('utf-8')
451459
QtWidgets.QMessageBox.about(self, "Status", status)
452460

453461
def start_utility(self):
454462
"""Runs 'libinput-gestures-setup start', displays output in message box"""
455463
if self.installed:
456-
status = subprocess.run(['libinput-gestures-setup', 'start'], capture_output=True)
464+
status = run(['libinput-gestures-setup', 'start'])
457465
status = status.stdout.decode('utf-8')
458466
QtWidgets.QMessageBox.about(self, "Status", status)
459467

@@ -709,12 +717,11 @@ def draw_plasma_actions(self):
709717

710718
self.actionType.setText('Plasma action')
711719
self.plasmaActions = QtWidgets.QComboBox()
712-
kwin_shortcuts = subprocess.run(
720+
kwin_shortcuts = run(
713721
[
714722
self.QDBUS_NAME, 'org.kde.kglobalaccel', '/component/kwin',
715723
'org.kde.kglobalaccel.Component.shortcutNames'
716-
],
717-
capture_output=True,
724+
]
718725
)
719726
kwin_shortcuts = kwin_shortcuts.stdout.decode('utf-8').split('\n')[:-2]
720727
kwin_shortcuts.sort()
@@ -786,5 +793,6 @@ def main():
786793
window.show()
787794
app.exec_()
788795

796+
789797
if __name__ == '__main__':
790798
main()

tests/test_main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from libinput_gestures_qt.main import run
2+
3+
4+
def test_run():
5+
run('ls')

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# tox (https://tox.readthedocs.io/) is a tool for running tests
2+
# in multiple virtualenvs. This configuration file will run the
3+
# test suite on all supported python versions. To use it, "pip install tox"
4+
# and then run "tox" from this directory.
5+
6+
[tox]
7+
envlist = py35, py36, py37
8+
9+
[testenv]
10+
commands = pytest
11+
deps =
12+
pytest

0 commit comments

Comments
 (0)