Skip to content

Commit

Permalink
solaar: improve imports and guard Gtk, etc imports with correct version
Browse files Browse the repository at this point in the history
solaar: move imports to top of files

solaar: move more imports to top of files

solaar: guard Gtk, etc imports with correct version
  • Loading branch information
pfps committed Feb 18, 2024
1 parent 17e6463 commit d1c899d
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 56 deletions.
16 changes: 10 additions & 6 deletions lib/hidapi/hidapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
from threading import Thread
from time import sleep

import gi

gi.require_version('Gdk', '3.0')
from gi.repository import GLib # NOQA: E402

logger = logging.getLogger(__name__)

native_implementation = 'hidapi'
Expand Down Expand Up @@ -257,7 +262,7 @@ def _match(action, device, filterfn):
device['hidpp_long'] = True
except HIDError as e: # noqa: F841
if logger.isEnabledFor(logging.INFO):
logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}")
logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}") # noqa
finally:
if device_handle:
close(device_handle)
Expand All @@ -279,8 +284,8 @@ def _match(action, device, filterfn):
d_info = DeviceInfo(
path=device['path'].decode(),
bus_id=bus_id,
vendor_id=f'{vid:04X}',
product_id=f'{pid:04X}',
vendor_id=f'{vid:04X}', # noqa
product_id=f'{pid:04X}', # noqa
interface=None,
driver=None,
manufacturer=device['manufacturer_string'],
Expand All @@ -297,8 +302,8 @@ def _match(action, device, filterfn):
d_info = DeviceInfo(
path=device['path'].decode(),
bus_id=None,
vendor_id=f'{vid:04X}',
product_id=f'{pid:04X}',
vendor_id=f'{vid:04X}', # noqa
product_id=f'{pid:04X}', # noqa
interface=None,
driver=None,
manufacturer=None,
Expand All @@ -323,7 +328,6 @@ def find_paired_node_wpid(receiver_path, index):


def monitor_glib(callback, filterfn):
from gi.repository import GLib

def device_callback(action, device):
# print(f"device_callback({action}): {device}")
Expand Down
9 changes: 4 additions & 5 deletions lib/hidapi/hidconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import argparse
import os
import os.path
import readline
import sys
import time

from binascii import hexlify, unhexlify
from select import select as _select
from threading import Lock
from threading import Lock, Thread

import hidapi as _hid

Expand Down Expand Up @@ -176,7 +179,6 @@ def matchfn(bid, vid, pid, _a, _b):


def _parse_arguments():
import argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--history', help='history file (default ~/.hidconsole-history)')
arg_parser.add_argument('--hidpp', action='store_true', help='ensure input data is a valid HID++ request')
Expand All @@ -196,9 +198,7 @@ def main():
if interactive:
print('.. Press ^C/^D to exit, or type hex bytes to write to the device.')

import readline
if args.history is None:
import os.path
args.history = os.path.join(os.path.expanduser('~'), '.hidconsole-history')
try:
readline.read_history_file(args.history)
Expand All @@ -207,7 +207,6 @@ def main():
pass

try:
from threading import Thread
t = Thread(target=_continuous_read, args=(handle, ))
t.daemon = True
t.start()
Expand Down
11 changes: 6 additions & 5 deletions lib/hidapi/udev.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@
from time import sleep
from time import time as _timestamp

import gi

from hid_parser import ReportDescriptor as _ReportDescriptor
from pyudev import Context as _Context
from pyudev import Device as _Device
from pyudev import DeviceNotFoundError
from pyudev import Devices as _Devices
from pyudev import Monitor as _Monitor

gi.require_version('Gdk', '3.0')
from gi.repository import GLib # NOQA: E402

logger = logging.getLogger(__name__)

native_implementation = 'udev'
Expand Down Expand Up @@ -105,9 +111,6 @@ def _match(action, device, filterfn):
return # these are devices connected through a receiver so don't pick them up here

try: # if report descriptor does not indicate HID++ capabilities then this device is not of interest to Solaar
from hid_parser import ReportDescriptor as _ReportDescriptor

# from hid_parser import Usage as _Usage
hidpp_short = hidpp_long = False
devfile = '/sys' + hid_device.get('DEVPATH') + '/report_descriptor'
with fileopen(devfile, 'rb') as fd:
Expand Down Expand Up @@ -236,8 +239,6 @@ def find_paired_node_wpid(receiver_path, index):


def monitor_glib(callback, filterfn):
from gi.repository import GLib

c = _Context()

# already existing devices
Expand Down
22 changes: 10 additions & 12 deletions lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@

import ctypes as _ctypes
import logging
import math
import numbers
import os as _os
import os.path as _path
import platform as _platform
import socket
import subprocess
import sys as _sys
import time as _time

from math import sqrt as _sqrt
from struct import unpack as _unpack
import dbus
import gi
import keysyms.keysymdef as _keysymdef
import psutil

# There is no evdev on macOS or Windows. Diversion will not work without
# it but other Solaar functionality is available.
Expand All @@ -34,9 +40,8 @@
else:
import evdev

import dbus
import keysyms.keysymdef as _keysymdef
import psutil
from math import sqrt as _sqrt
from struct import unpack as _unpack

from yaml import add_representer as _yaml_add_representer
from yaml import dump_all as _yaml_dump_all
Expand All @@ -46,8 +51,6 @@
from .hidpp20 import FEATURE as _F
from .special_keys import CONTROL as _CONTROL

import gi # isort:skip

gi.require_version('Gdk', '3.0') # isort:skip
from gi.repository import Gdk, GLib # NOQA: E402 # isort:skip

Expand Down Expand Up @@ -1082,7 +1085,6 @@ def __str__(self):
def evaluate(self, feature, notification, device, status, last_result):
if logger.isEnabledFor(logging.DEBUG):
logger.debug('evaluate condition: %s', self)
import socket
hostname = socket.getfqdn()
return hostname.startswith(self.host)

Expand Down Expand Up @@ -1206,7 +1208,6 @@ def data(self):
class MouseScroll(Action):

def __init__(self, amounts, warn=True):
import numbers
if len(amounts) == 1 and isinstance(amounts[0], list):
amounts = amounts[0]
if not (len(amounts) == 2 and all([isinstance(a, numbers.Number) for a in amounts])):
Expand All @@ -1219,8 +1220,6 @@ def __str__(self):
return 'MouseScroll: ' + ' '.join([str(a) for a in self.amounts])

def evaluate(self, feature, notification, device, status, last_result):
import math
import numbers
amounts = self.amounts
if isinstance(last_result, numbers.Number):
amounts = [math.floor(last_result * a) for a in self.amounts]
Expand Down Expand Up @@ -1328,7 +1327,6 @@ def __str__(self):
return 'Execute: ' + ' '.join([a for a in self.args])

def evaluate(self, feature, notification, device, status, last_result):
import subprocess
if logger.isEnabledFor(logging.INFO):
logger.info('Execute action: %s', self.args)
subprocess.Popen(self.args)
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Logitech Unifying Receiver API.

import logging
import socket
import threading as _threading

from struct import pack as _pack
Expand Down Expand Up @@ -2003,7 +2004,6 @@ def get_host_names(device):
remaining = 0
host_names[host] = (bool(status), name)
# update the current host's name if it doesn't match the system name
import socket
hostname = socket.gethostname().partition('.')[0]
if host_names[currentHost][1] != hostname:
set_host_name(device, hostname, host_names[currentHost][1])
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import yaml as _yaml

from gi.repository import GLib
from logitech_receiver.common import NamedInt as _NamedInt
from solaar import __version__

Expand Down Expand Up @@ -142,7 +143,6 @@ def save(defer=False):
else:
with save_lock:
if not save_timer:
from gi.repository import GLib
save_timer = _Timer(5.0, lambda: GLib.idle_add(do_save))
save_timer.start()

Expand Down
26 changes: 13 additions & 13 deletions lib/solaar/gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import argparse
import faulthandler
import importlib
import logging
import os.path
Expand All @@ -26,8 +28,15 @@
import sys
import tempfile

from traceback import format_exc

import solaar.cli as _cli
import solaar.configuration as _configuration
import solaar.i18n as _i18n
import solaar.listener as _listener
import solaar.ui as _ui
import solaar.ui.common as _common
import solaar.upower as _upower

from solaar import NAME, __version__

Expand All @@ -52,7 +61,6 @@ def _require(module, os_package, gi=None, gi_package=None, gi_version=None):


def _parse_arguments():
import argparse
arg_parser = argparse.ArgumentParser(
prog=NAME.lower(), epilog='For more information see https://pwr-solaar.github.io/Solaar'
)
Expand Down Expand Up @@ -122,7 +130,6 @@ def _parse_arguments():

# On first SIGINT, dump threads to stderr; on second, exit
def _handlesig(signl, stack):
import faulthandler
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL)

Expand Down Expand Up @@ -161,25 +168,18 @@ def main():
logger.warning('Solaar udev file not found in expected location')
logger.warning('See https://pwr-solaar.github.io/Solaar/installation for more information')
try:
import solaar.listener as listener
import solaar.ui as ui
import solaar.ui.common as common

listener.setup_scanner(ui.status_changed, common.error_dialog)
_listener.setup_scanner(_ui.status_changed, _common.error_dialog)

import solaar.upower as _upower
if args.restart_on_wake_up:
_upower.watch(listener.start_all, listener.stop_all)
_upower.watch(_listener.start_all, _listener.stop_all)
else:
_upower.watch(lambda: listener.ping_all(True))
_upower.watch(lambda: _listener.ping_all(True))

import solaar.configuration as _configuration
_configuration.defer_saves = True # allow configuration saves to be deferred

# main UI event loop
ui.run_loop(listener.start_all, listener.stop_all, args.window != 'only', args.window != 'hide')
_ui.run_loop(_listener.start_all, _listener.stop_all, args.window != 'only', args.window != 'hide')
except Exception:
from traceback import format_exc
sys.exit('%s: error: %s' % (NAME.lower(), format_exc()))

temp.close()
Expand Down
9 changes: 4 additions & 5 deletions lib/solaar/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import gettext as _gettext
import locale
import os.path as _path
import sys as _sys

from glob import glob as _glob

from solaar import NAME as _NAME

Expand All @@ -27,13 +31,8 @@


def _find_locale_path(lc_domain):
import os.path as _path
import sys as _sys
prefix_share = _path.normpath(_path.join(_path.realpath(_sys.path[0]), '..'))
src_share = _path.normpath(_path.join(_path.realpath(_sys.path[0]), '..', 'share'))
del _sys

from glob import glob as _glob

for location in prefix_share, src_share:
mo_files = _glob(_path.join(location, 'locale', '*', 'LC_MESSAGES', lc_domain + '.mo'))
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import errno as _errno
import logging
import subprocess
import time

from collections import namedtuple
Expand Down Expand Up @@ -390,7 +391,6 @@ def _process_add(device_info, retry):
except OSError as e:
if e.errno == _errno.EACCES:
try:
import subprocess
output = subprocess.check_output(['/usr/bin/getfacl', '-p', device_info.path], text=True)
if logger.isEnabledFor(logging.WARNING):
logger.warning('Missing permissions on %s\n%s.', device_info.path, output)
Expand Down
3 changes: 2 additions & 1 deletion lib/solaar/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import gi
import yaml as _yaml

from gi.repository import Gio, GLib, Gtk
from logitech_receiver.status import ALERT
from solaar.i18n import _
from solaar.tasks import TaskRunner as _TaskRunner
Expand All @@ -31,6 +30,8 @@
from . import diversion_rules, notify, tray, window

gi.require_version('Gtk', '3.0')
from gi.repository import Gio, GLib, Gtk # NOQA: E402

logger = logging.getLogger(__name__)

#
Expand Down
6 changes: 5 additions & 1 deletion lib/solaar/ui/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import logging

from gi.repository import GLib, Gtk
import gi

from solaar.i18n import _

gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk # NOQA: E402

logger = logging.getLogger(__name__)


Expand Down
Loading

0 comments on commit d1c899d

Please sign in to comment.