Skip to content

Instantly share code, notes, and snippets.

@benoit-pierre
Created May 1, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benoit-pierre/9894a11e829cd328ebd8f795a3b0cace to your computer and use it in GitHub Desktop.
Save benoit-pierre/9894a11e829cd328ebd8f795a3b0cace to your computer and use it in GitHub Desktop.
plover/dist_main.py | 5 ++---
plover/gui_qt/engine.py | 6 ++----
plover/gui_qt/main_window.py | 8 ++++----
plover/gui_qt/trayicon.py | 5 ++---
plover/i18n.py | 11 +++++------
plover/log.py | 8 ++++----
plover/macro/undo.py | 6 ++----
plover/main.py | 12 +++++-------
plover/oslayer/config.py | 22 +++++++++++++---------
plover/oslayer/keyboardcontrol.py | 10 +++++-----
plover/oslayer/log_plyer.py | 5 ++---
plover/oslayer/processlock.py | 4 +++-
plover/oslayer/wmctrl.py | 9 ++++-----
reqs/dist.txt | 4 ++--
test/test_config.py | 5 +++--
test/test_misc.py | 3 +--
test/test_translation.py | 6 +++---
17 files changed, 62 insertions(+), 67 deletions(-)
diff --git i/plover/dist_main.py w/plover/dist_main.py
index 92ebb140..29db404d 100644
--- i/plover/dist_main.py
+++ w/plover/dist_main.py
@@ -1,9 +1,8 @@
-
import os
import sys
import subprocess
-from plover.oslayer.config import CONFIG_DIR, PLUGINS_PLATFORM
+from plover.oslayer.config import CONFIG_DIR, PLATFORM, PLUGINS_PLATFORM
def main():
@@ -13,7 +12,7 @@ def main():
args.remove('--no-user-plugins')
args.insert(1, '-s')
os.environ['PYTHONUSERBASE'] = os.path.join(CONFIG_DIR, 'plugins', PLUGINS_PLATFORM)
- if sys.platform.startswith('win32'):
+ if PLATFORM == 'win':
# Workaround https://bugs.python.org/issue19066
subprocess.Popen(args, cwd=os.getcwd())
sys.exit(0)
diff --git i/plover/gui_qt/engine.py w/plover/gui_qt/engine.py
index be29b119..490e9bae 100644
--- i/plover/gui_qt/engine.py
+++ w/plover/gui_qt/engine.py
@@ -1,6 +1,3 @@
-
-import sys
-
from PyQt5.QtCore import (
QThread,
QVariant,
@@ -8,6 +5,7 @@
)
from plover.engine import StenoEngine
+from plover.oslayer.config import PLATFORM
class Engine(StenoEngine, QThread):
@@ -50,7 +48,7 @@ def join(self):
return self.code
def run(self):
- if sys.platform.startswith('darwin'):
+ if PLATFORM == 'mac':
import appnope
appnope.nope()
super().run()
diff --git i/plover/gui_qt/main_window.py w/plover/gui_qt/main_window.py
index 423ad00d..110f3085 100644
--- i/plover/gui_qt/main_window.py
+++ w/plover/gui_qt/main_window.py
@@ -14,7 +14,7 @@
from plover import _, log
from plover.oslayer import wmctrl
-from plover.oslayer.config import CONFIG_DIR
+from plover.oslayer.config import CONFIG_DIR, PLATFORM
from plover.registry import registry
from plover.resource import resource_filename
@@ -243,11 +243,11 @@ def on_configure(self):
self._configure()
def on_open_config_folder(self):
- if sys.platform.startswith('win'):
+ if PLATFORM == 'win':
os.startfile(CONFIG_DIR)
- elif sys.platform.startswith('linux'):
+ elif PLATFORM == 'linux':
subprocess.call(['xdg-open', CONFIG_DIR])
- elif sys.platform.startswith('darwin'):
+ elif PLATFORM == 'mac':
subprocess.call(['open', CONFIG_DIR])
def on_reconnect(self):
diff --git i/plover/gui_qt/trayicon.py w/plover/gui_qt/trayicon.py
index 41cc54cc..d00e9a59 100644
--- i/plover/gui_qt/trayicon.py
+++ w/plover/gui_qt/trayicon.py
@@ -1,11 +1,10 @@
-import sys
-
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMessageBox, QSystemTrayIcon
from plover import _, __name__ as __software_name__
from plover import log
+from plover.oslayer.config import PLATFORM
from plover.machine.base import (
STATE_STOPPED,
STATE_INITIALIZING,
@@ -76,7 +75,7 @@ def enable(self):
self._trayicon = QSystemTrayIcon()
# On OS X, the context menu is activated with either mouse buttons,
# and activation messages are still sent, so ignore those...
- if not sys.platform.startswith('darwin'):
+ if PLATFORM != 'mac':
self._trayicon.activated.connect(self._on_activated)
if self._context_menu is not None:
self._trayicon.setContextMenu(self._context_menu)
diff --git i/plover/i18n.py w/plover/i18n.py
index 4f6330d7..602f88c9 100644
--- i/plover/i18n.py
+++ w/plover/i18n.py
@@ -1,28 +1,27 @@
import os
-import sys
import locale
import gettext
import pkg_resources
-from plover.oslayer.config import CONFIG_DIR
+from plover.oslayer.config import CONFIG_DIR, PLATFORM
def get_language():
env_vars = ['LANGUAGE']
- if sys.platform.startswith('linux'):
+ if PLATFORM in {'linux', 'bsd'}:
env_vars.extend(('LC_ALL', 'LC_MESSAGES', 'LANG'))
for var in env_vars:
lang = os.environ.get(var)
if lang is not None:
return lang
- if sys.platform.startswith('linux'):
+ if PLATFORM in {'linux', 'bsd'}:
lang, enc = locale.getdefaultlocale()
- elif sys.platform.startswith('darwin'):
+ elif PLATFORM == 'mac':
from AppKit import NSLocale
lang_list = NSLocale.preferredLanguages()
lang = lang_list[0] if lang_list else None
- elif sys.platform.startswith('win'):
+ elif PLATFORM == 'win':
from ctypes import windll
lang = locale.windows_locale[windll.kernel32.GetUserDefaultUILanguage()]
if lang is None:
diff --git i/plover/log.py w/plover/log.py
index 1aecf6cf..6d32555e 100644
--- i/plover/log.py
+++ w/plover/log.py
@@ -11,7 +11,7 @@
from logging.handlers import RotatingFileHandler
from logging import DEBUG, INFO, WARNING, ERROR
-from plover.oslayer.config import CONFIG_DIR
+from plover.oslayer.config import CONFIG_DIR, PLATFORM
LOG_FORMAT = '%(asctime)s [%(threadName)s] %(levelname)s: %(message)s'
@@ -84,13 +84,13 @@ def setup_platform_handler(self):
return
handler_class = None
try:
- if sys.platform.startswith('linux'):
+ if PLATFORM == 'linux':
from plover.oslayer.log_dbus import DbusNotificationHandler
handler_class = DbusNotificationHandler
- elif sys.platform.startswith('darwin'):
+ elif PLATFORM == 'mac':
from plover.oslayer.log_osx import OSXNotificationHandler
handler_class = OSXNotificationHandler
- elif sys.platform.startswith('win32'):
+ elif PLATFORM == 'win':
from plover.oslayer.log_plyer import PlyerNotificationHandler
handler_class = PlyerNotificationHandler
except Exception:
diff --git i/plover/macro/undo.py w/plover/macro/undo.py
index 15585050..15a53502 100644
--- i/plover/macro/undo.py
+++ w/plover/macro/undo.py
@@ -1,10 +1,8 @@
-
-import sys
-
from plover.translation import Translation
+from plover.oslayer.config import PLATFORM
-if sys.platform.startswith('darwin'):
+if PLATFORM == 'mac':
BACK_STRING = '{#Alt_L(BackSpace)}{^}'
else:
BACK_STRING = '{#Control_L(BackSpace)}{^}'
diff --git i/plover/main.py w/plover/main.py
index 4a05878e..6010174c 100644
--- i/plover/main.py
+++ w/plover/main.py
@@ -14,12 +14,9 @@
import pkg_resources
-if sys.platform.startswith('darwin'):
- import appnope
-
from plover.config import Config
from plover.oslayer import processlock
-from plover.oslayer.config import CONFIG_DIR, CONFIG_FILE
+from plover.oslayer.config import CONFIG_DIR, CONFIG_FILE, PLATFORM
from plover.registry import registry
from plover import log
from plover import __name__ as __software_name__
@@ -61,7 +58,7 @@ def main():
log.info('Plover %s', __version__)
log.info('configuration directory: %s', CONFIG_DIR)
- if sys.platform == 'darwin':
+ if PLATFORM == 'mac':
# Fixes PyQt issue on macOS Big Sur.
os.environ['QT_MAC_WANTS_LAYER'] = '1'
@@ -121,7 +118,8 @@ def main():
# Ensure only one instance of Plover is running at a time.
with processlock.PloverLock():
- if sys.platform.startswith('darwin'):
+ if PLATFORM == 'mac':
+ import appnope
appnope.nope()
init_config_dir()
# This must be done after calling init_config_dir, so
@@ -144,7 +142,7 @@ def main():
args[0:1] = [sys.executable, '-m', __spec__.name]
# Execute atexit handlers.
atexit._run_exitfuncs()
- if sys.platform.startswith('win32'):
+ if PLATFORM == 'win':
# Workaround https://bugs.python.org/issue19066
subprocess.Popen(args, cwd=os.getcwd())
code = 0
diff --git i/plover/oslayer/config.py w/plover/oslayer/config.py
index e2594506..f4b9dee7 100644
--- i/plover/oslayer/config.py
+++ w/plover/oslayer/config.py
@@ -10,12 +10,23 @@
import pkg_resources
+if sys.platform.startswith('darwin'):
+ PLATFORM = 'mac'
+elif sys.platform.startswith('linux'):
+ PLATFORM = 'linux'
+elif sys.platform.startswith('win'):
+ PLATFORM = 'win'
+elif sys.platform.startswith(('freebsd', 'openbsd')):
+ PLATFORM = 'bsd'
+else:
+ PLATFORM = None
+
# If the program's working directory has a plover.cfg file then run in
# "portable mode", i.e. store all data in the same directory. This allows
# keeping all Plover files in a portable drive.
#
# Note: the special case when run from an app bundle on macOS.
-if sys.platform.startswith('darwin') and '.app/' in os.path.realpath(__file__):
+if PLATFORM == 'mac' and '.app/' in os.path.realpath(__file__):
PROGRAM_DIR = os.path.abspath(os.path.join(os.path.dirname(sys.executable),
*[os.path.pardir] * 3))
else:
@@ -38,14 +49,7 @@
CONFIG_FILE = os.path.join(CONFIG_DIR, CONFIG_BASENAME)
# Setup plugins directory.
-if sys.platform.startswith('darwin'):
- PLUGINS_PLATFORM = 'mac'
-elif sys.platform.startswith('linux'):
- PLUGINS_PLATFORM = 'linux'
-elif sys.platform.startswith('win'):
- PLUGINS_PLATFORM = 'win'
-else:
- PLUGINS_PLATFORM = None
+PLUGINS_PLATFORM = PLATFORM
plover_dist = pkg_resources.working_set.by_key['plover']
diff --git i/plover/oslayer/keyboardcontrol.py w/plover/oslayer/keyboardcontrol.py
index 7e8f8aff..1b892c4e 100644
--- i/plover/oslayer/keyboardcontrol.py
+++ w/plover/oslayer/keyboardcontrol.py
@@ -16,16 +16,16 @@
"""
-import sys
+from plover.oslayer.config import PLATFORM
KEYBOARDCONTROL_NOT_FOUND_FOR_OS = \
- "No keyboard control module was found for os %s" % sys.platform
+ "No keyboard control module was found for platform: %s" % PLATFORM
-if sys.platform.startswith('linux'):
+if PLATFORM in {'linux', 'bsd'}:
from plover.oslayer import xkeyboardcontrol as keyboardcontrol
-elif sys.platform.startswith('win32'):
+elif PLATFORM == 'win':
from plover.oslayer import winkeyboardcontrol as keyboardcontrol
-elif sys.platform.startswith('darwin'):
+elif PLATFORM == 'mac':
from plover.oslayer import osxkeyboardcontrol as keyboardcontrol
else:
raise Exception(KEYBOARDCONTROL_NOT_FOUND_FOR_OS)
diff --git i/plover/oslayer/log_plyer.py w/plover/oslayer/log_plyer.py
index 91979dac..c26aa874 100644
--- i/plover/oslayer/log_plyer.py
+++ w/plover/oslayer/log_plyer.py
@@ -1,16 +1,15 @@
import os
-import sys
import logging
from plyer import notification
from plover import log, __name__ as __software_name__
-from plover.oslayer.config import ASSETS_DIR
+from plover.oslayer.config import ASSETS_DIR, PLATFORM
APPNAME = __software_name__.capitalize()
-if sys.platform.startswith('win32'):
+if PLATFORM == 'win':
APPICON = os.path.join(ASSETS_DIR, 'plover.ico')
else:
APPICON = os.path.join(ASSETS_DIR, 'plover_32x32.png')
diff --git i/plover/oslayer/processlock.py w/plover/oslayer/processlock.py
index f5fa417c..5b983936 100644
--- i/plover/oslayer/processlock.py
+++ w/plover/oslayer/processlock.py
@@ -7,12 +7,14 @@
import sys
+from plover.oslayer.config import PLATFORM
+
class LockNotAcquiredException(Exception):
pass
-if sys.platform.startswith('win32'):
+if PLATFORM == 'win':
from ctypes import windll
diff --git i/plover/oslayer/wmctrl.py w/plover/oslayer/wmctrl.py
index c954f628..0aeb4304 100644
--- i/plover/oslayer/wmctrl.py
+++ w/plover/oslayer/wmctrl.py
@@ -1,8 +1,7 @@
+from plover.oslayer.config import PLATFORM
-import sys
-
-if sys.platform.startswith('win32'):
+if PLATFORM == 'win':
from ctypes import windll, wintypes
@@ -17,7 +16,7 @@
SetForegroundWindow.restype = wintypes.BOOL
-elif sys.platform.startswith('darwin'):
+elif PLATFORM == 'mac':
from Cocoa import NSWorkspace, NSRunningApplication, NSApplicationActivateIgnoringOtherApps
@@ -28,7 +27,7 @@ def SetForegroundWindow(pid):
target_window = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
target_window.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
-elif sys.platform.startswith('linux'):
+elif PLATFORM in {'linux', 'bsd'}:
from plover.oslayer.xwmctrl import WmCtrl
diff --git i/reqs/dist.txt w/reqs/dist.txt
index 98864c8d..b45b9e33 100644
--- i/reqs/dist.txt
+++ w/reqs/dist.txt
@@ -4,8 +4,8 @@ pyobjc-core>=4.0; "darwin" in sys_platform
pyobjc-framework-Cocoa>=4.0; "darwin" in sys_platform
pyobjc-framework-Quartz>=4.0; "darwin" in sys_platform
pyserial>=2.7
-python-xlib>=0.16; "linux" in sys_platform and python_version < "3.9"
-python-xlib>=0.29; "linux" in sys_platform and python_version >= "3.9"
+python-xlib>=0.16; ("linux" in sys_platform or "bsd" in sys_platform) and python_version < "3.9"
+python-xlib>=0.29; ("linux" in sys_platform or "bsd" in sys_platform) and python_version >= "3.9"
setuptools
wcwidth
diff --git i/test/test_config.py w/test/test_config.py
index 387945bc..8a06c4bb 100644
--- i/test/test_config.py
+++ w/test/test_config.py
@@ -20,6 +20,7 @@
from plover import config
from plover.config import DictionaryConfig
+from plover.oslayer.config import PLATFORM
from plover.machine.keyboard import Keyboard
from plover.machine.keymap import Keymap
from plover.misc import expand_path
@@ -102,7 +103,7 @@ def test_config_dict():
DictionaryConfig(short_path, False)
-if sys.platform.startswith('win32'):
+if PLATFORM == 'win':
ABS_PATH = os.path.normcase(r'c:/foo/bar')
else:
ABS_PATH = '/foo/bar'
@@ -554,7 +555,7 @@ def test_config_dir(tree, expected_config_dir, tmpdir):
}
# Setup environment.
env = dict(os.environ)
- if sys.platform.startswith('win32'):
+ if PLATFORM == 'win':
env['USERPROFILE'] = str(home)
else:
env['HOME'] = str(home)
diff --git i/test/test_misc.py w/test/test_misc.py
index da518896..83b84cad 100644
--- i/test/test_misc.py
+++ w/test/test_misc.py
@@ -6,7 +6,6 @@
import inspect
import os
-import sys
import pytest
@@ -26,7 +25,7 @@ def test_popcount_8():
misc.popcount_8(-1)
-if sys.platform.startswith('win32'):
+if conf.PLATFORM == 'win':
ABS_PATH = os.path.normcase(r'c:\foo\bar')
else:
ABS_PATH = '/foo/bar'
diff --git i/test/test_translation.py w/test/test_translation.py
index 98c46226..9f0a2fb7 100644
--- i/test/test_translation.py
+++ w/test/test_translation.py
@@ -6,19 +6,19 @@
from collections import namedtuple
import copy
import operator
-import sys
+from plover.oslayer.config import PLATFORM
+from plover.steno import Stroke, normalize_steno
from plover.steno_dictionary import StenoDictionary, StenoDictionaryCollection
from plover.translation import Translation, Translator, _State
from plover.translation import escape_translation, unescape_translation
-from plover.steno import Stroke, normalize_steno
from plover_build_utils.testing import steno_to_stroke as stroke
from . import parametrize
-if sys.platform.startswith('darwin'):
+if PLATFORM == 'mac':
BACK_STRING = '{#Alt_L(BackSpace)}{^}'
else:
BACK_STRING = '{#Control_L(BackSpace)}{^}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment