Skip to content

Commit

Permalink
Merge pull request #1509 from benoit-pierre/oslayer_reorg
Browse files Browse the repository at this point in the history
`osslayer` reorg
  • Loading branch information
benoit-pierre authored May 15, 2022
2 parents 3b98727 + 9182783 commit 6e29929
Show file tree
Hide file tree
Showing 34 changed files with 396 additions and 492 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci/skiplist_os_linux.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
.github/workflows/ci/skiplist_os_Windows.txt
.github/workflows/ci/skiplist_os_macOS.txt
osx/*
plover/oslayer/log_osx.py
plover/oslayer/osxkeyboardcontrol.py
plover/oslayer/osxkeyboardlayout.py
plover/oslayer/winkeyboardcontrol.py
plover/oslayer/winkeyboardlayout.py
plover/oslayer/osx/*
plover/oslayer/windows/*
windows/*
{EOF}
7 changes: 2 additions & 5 deletions .github/workflows/ci/skiplist_os_macos.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
.github/workflows/ci/skiplist_os_Linux.txt
.github/workflows/ci/skiplist_os_Windows.txt
linux/*
plover/oslayer/log_dbus.py
plover/oslayer/winkeyboardcontrol.py
plover/oslayer/winkeyboardlayout.py
plover/oslayer/xkeyboardcontrol.py
plover/oslayer/xwmctrl.py
plover/oslayer/linux/*
plover/oslayer/windows/*
windows/*
{EOF}
8 changes: 2 additions & 6 deletions .github/workflows/ci/skiplist_os_windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
.github/workflows/ci/skiplist_os_macOS.txt
linux/*
osx/*
plover/oslayer/log_dbus.py
plover/oslayer/log_osx.py
plover/oslayer/osxkeyboardcontrol.py
plover/oslayer/osxkeyboardlayout.py
plover/oslayer/xkeyboardcontrol.py
plover/oslayer/xwmctrl.py
plover/oslayer/linux/*
plover/oslayer/osx/*
{EOF}
31 changes: 11 additions & 20 deletions plover/i18n.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import os
import locale
import gettext

from plover.oslayer.config import CONFIG_DIR, PLATFORM
from plover.oslayer.i18n import get_system_language
from plover.resource import ASSET_SCHEME, resource_filename


def get_language():
env_vars = ['LANGUAGE']
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 PLATFORM in {'linux', 'bsd'}:
lang, enc = locale.getdefaultlocale()
elif PLATFORM == 'mac':
from AppKit import NSLocale
lang_list = NSLocale.preferredLanguages()
lang = lang_list[0] if lang_list else None
elif PLATFORM == 'win':
from ctypes import windll
lang = locale.windows_locale[windll.kernel32.GetUserDefaultUILanguage()]
if lang is None:
lang = 'en'
return lang
# Give priority to LANGUAGE environment variable.
lang = os.environ.get('LANGUAGE')
if lang is not None:
return lang
# Try to get system language.
lang = get_system_language()
if lang is not None:
return lang
# Fallback to English.
return 'en'

def get_locale_dir(package, resource_dir):
locale_dir = os.path.join(CONFIG_DIR, 'messages')
Expand Down
2 changes: 1 addition & 1 deletion plover/key_combo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Generated using:
#
# from Xlib import XK
# from plover.oslayer.xkeyboardcontrol import keysym_to_string
# from plover.oslayer.linux.keyboardcontrol_x11 import keysym_to_string
# for kn, ks in sorted({
# name[3:].lower(): getattr(XK, name)
# for name in sorted(dir(XK))
Expand Down
18 changes: 6 additions & 12 deletions plover/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,19 @@ def has_platform_handler(self):
def setup_platform_handler(self):
if self.has_platform_handler():
return
handler_class = None
NotificationHandler = None
try:
if PLATFORM == 'linux':
from plover.oslayer.log_dbus import DbusNotificationHandler
handler_class = DbusNotificationHandler
elif PLATFORM == 'mac':
from plover.oslayer.log_osx import OSXNotificationHandler
handler_class = OSXNotificationHandler
from plover.oslayer.log import NotificationHandler
except Exception:
self.info('could not import platform gui log', exc_info=True)
if handler_class is None:
return
try:
handler = handler_class()
handler = NotificationHandler()
self.addHandler(handler)
except Exception:
self.info('could not initialize platform gui log', exc_info=True)
else:
self.addHandler(handler)
self._platform_handler = handler
return
self._platform_handler = handler

def set_level(self, level):
self._print_handler.setLevel(level)
Expand Down
22 changes: 15 additions & 7 deletions plover/machine/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ class Keyboard(StenotypeBase):
"""

KEYS_LAYOUT = KeyboardCapture.SUPPORTED_KEYS_LAYOUT
KEYS_LAYOUT = '''
Escape F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12
` 1 2 3 4 5 6 7 8 9 0 - = \\ BackSpace Insert Home Page_Up
Tab q w e r t y u i o p [ ] Delete End Page_Down
a s d f g h j k l ; ' Return
z x c v b n m , . / Up
space Left Down Right
'''
ACTIONS = ('arpeggiate',)

def __init__(self, params):
Expand All @@ -40,11 +48,11 @@ def __init__(self, params):
self._stroke_key_down_count = 0
self._update_bindings()

def _suppress(self):
def _update_suppression(self):
if self._keyboard_capture is None:
return
suppressed_keys = self._bindings.keys() if self._is_suppressed else ()
self._keyboard_capture.suppress_keyboard(suppressed_keys)
self._keyboard_capture.suppress(suppressed_keys)

def _update_bindings(self):
self._arpeggiate_key = None
Expand All @@ -59,7 +67,7 @@ def _update_bindings(self):
else:
# Don't suppress arpeggiate key if it's not used.
del self._bindings[key]
self._suppress()
self._update_suppression()

def set_keymap(self, keymap):
super().set_keymap(keymap)
Expand All @@ -72,8 +80,8 @@ def start_capture(self):
self._keyboard_capture = KeyboardCapture()
self._keyboard_capture.key_down = self._key_down
self._keyboard_capture.key_up = self._key_up
self._suppress()
self._keyboard_capture.start()
self._update_suppression()
except:
self._error()
raise
Expand All @@ -83,14 +91,14 @@ def stop_capture(self):
"""Stop listening for output from the stenotype machine."""
if self._keyboard_capture is not None:
self._is_suppressed = False
self._suppress()
self._update_suppression()
self._keyboard_capture.cancel()
self._keyboard_capture = None
self._stopped()

def set_suppression(self, enabled):
self._is_suppressed = enabled
self._suppress()
self._update_suppression()

def suppress_last_stroke(self, send_backspaces):
send_backspaces(self._last_stroke_key_down_count)
Expand Down
19 changes: 19 additions & 0 deletions plover/machine/keyboard_capture/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Capture:

"""Keyboard capture interface."""

# Callbacks for keyboard press/release events.
key_down = lambda key: None
key_up = lambda key: None

def start(self):
"""Start capturing key events."""
raise NotImplementedError()

def cancel(self):
"""Stop capturing key events."""
raise NotImplementedError()

def suppress(self, suppressed_keys=()):
"""Setup suppression."""
raise NotImplementedError()
22 changes: 22 additions & 0 deletions plover/oslayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@

"""This package abstracts os details for plover."""

import os

from plover import log

from .config import PLATFORM


PLATFORM_PACKAGE = {
'bsd' : 'linux',
'linux': 'linux',
'mac' : 'osx',
'win' : 'windows',
}

def _add_platform_package_to_path():
platform_package = PLATFORM_PACKAGE.get(PLATFORM)
if platform_package is None:
log.warning('No platform-specific oslayer package for: %s' % PLATFORM)
return
__path__.insert(0, os.path.join(__path__[0], platform_package))

_add_platform_package_to_path()
87 changes: 0 additions & 87 deletions plover/oslayer/keyboardcontrol.py

This file was deleted.

Empty file.
12 changes: 12 additions & 0 deletions plover/oslayer/linux/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import locale
import os


# Note: highest priority first.
LANG_ENV_VARS = ('LC_ALL', 'LC_MESSAGES', 'LANG')

def get_system_language():
try:
return next(filter(None, map(os.environ.get, LANG_ENV_VARS)))
except StopIteration:
return locale.getdefaultlocale()[0]
1 change: 1 addition & 0 deletions plover/oslayer/linux/keyboardcontrol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .keyboardcontrol_x11 import KeyboardCapture, KeyboardEmulation # pylint: disable=unused-import
Loading

0 comments on commit 6e29929

Please sign in to comment.