Skip to content

Commit

Permalink
oslayer: reorg
Browse files Browse the repository at this point in the history
Add platform specific sub-packages.
  • Loading branch information
benoit-pierre committed May 14, 2022
1 parent d1aeb1c commit 9182783
Show file tree
Hide file tree
Showing 30 changed files with 109 additions and 121 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: 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()
32 changes: 0 additions & 32 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
File renamed without changes.
1 change: 1 addition & 0 deletions plover/oslayer/linux/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .log_dbus import DbusNotificationHandler as NotificationHandler # pylint: disable=unused-import
File renamed without changes.
7 changes: 7 additions & 0 deletions plover/oslayer/linux/wmctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .wmctrl_x11 import WmCtrl


_wmctrl = WmCtrl()

GetForegroundWindow = _wmctrl.get_foreground_window
SetForegroundWindow = _wmctrl.set_foreground_window
File renamed without changes.
Empty file added plover/oslayer/osx/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions plover/oslayer/osx/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from AppKit import NSLocale


def get_system_language():
lang_list = NSLocale.preferredLanguages()
return lang_list[0] if lang_list else None
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion plover/oslayer/log_osx.py → plover/oslayer/osx/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging


class OSXNotificationHandler(logging.Handler):
class NotificationHandler(logging.Handler):
""" Handler using OS X Notification Center to show messages. """

def __init__(self):
Expand Down
9 changes: 9 additions & 0 deletions plover/oslayer/osx/wmctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from Cocoa import NSWorkspace, NSRunningApplication, NSApplicationActivateIgnoringOtherApps


def GetForegroundWindow():
return NSWorkspace.sharedWorkspace().frontmostApplication().processIdentifier()

def SetForegroundWindow(pid):
target_window = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
target_window.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
Empty file.
7 changes: 7 additions & 0 deletions plover/oslayer/windows/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import locale

from ctypes import windll


def get_system_language():
return locale.windows_locale[windll.kernel32.GetUserDefaultUILanguage()]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from plover.misc import to_surrogate_pair
from plover.output import Output

from .winkeyboardlayout import KeyboardLayout
from .keyboardlayout import KeyboardLayout

# For the purposes of this class, we'll only report key presses that
# result in these outputs in order to exclude special key combos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import sys

from plover.key_combo import CHAR_TO_KEYNAME, add_modifiers_aliases
from plover.oslayer.wmctrl import GetForegroundWindow
from plover.misc import popcount_8

from .wmctrl import GetForegroundWindow


GetKeyboardLayout = windll.user32.GetKeyboardLayout
GetKeyboardLayout.argtypes = [
Expand Down
1 change: 1 addition & 0 deletions plover/oslayer/windows/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ..log_plyer import PlyerNotificationHandler as NotificationHandler # pylint: disable=unused-import
12 changes: 12 additions & 0 deletions plover/oslayer/windows/wmctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ctypes import windll, wintypes


GetForegroundWindow = windll.user32.GetForegroundWindow
GetForegroundWindow.argtypes = []
GetForegroundWindow.restype = wintypes.HWND

SetForegroundWindow = windll.user32.SetForegroundWindow
SetForegroundWindow.argtypes = [
wintypes.HWND, # hWnd
]
SetForegroundWindow.restype = wintypes.BOOL
37 changes: 0 additions & 37 deletions plover/oslayer/wmctrl.py

This file was deleted.

3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ packages =
plover.macro
plover.meta
plover.oslayer
plover.oslayer.linux
plover.oslayer.osx
plover.oslayer.windows
plover.output
plover.scripts
plover.system
Expand Down

0 comments on commit 9182783

Please sign in to comment.