Skip to content

Commit

Permalink
Unify imports in logitech package
Browse files Browse the repository at this point in the history
Related #2273
  • Loading branch information
MattHag committed May 16, 2024
1 parent a9ce033 commit 716b288
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 463 deletions.
134 changes: 59 additions & 75 deletions lib/logitech_receiver/base.py

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions lib/logitech_receiver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import annotations

# Some common functions and types.
import binascii
import dataclasses

from binascii import hexlify as _hexlify
from collections import namedtuple
from dataclasses import dataclass
from typing import Optional
from typing import Union

import yaml as _yaml
import yaml

from solaar.i18n import _

Expand Down Expand Up @@ -357,8 +357,8 @@ def to_yaml(cls, dumper, data):
return dumper.represent_mapping("!NamedInt", {"value": int(data), "name": data.name}, flow_style=True)


_yaml.SafeLoader.add_constructor("!NamedInt", NamedInt.from_yaml)
_yaml.add_representer(NamedInt, NamedInt.to_yaml)
yaml.SafeLoader.add_constructor("!NamedInt", NamedInt.from_yaml)
yaml.add_representer(NamedInt, NamedInt.to_yaml)


class NamedInts:
Expand Down Expand Up @@ -521,7 +521,7 @@ def __or__(self, other):
def strhex(x):
assert x is not None
"""Produce a hex-string representation of a sequence of bytes."""
return _hexlify(x).decode("ascii").upper()
return binascii.hexlify(x).decode("ascii").upper()


def bytes2int(x, signed=False):
Expand Down Expand Up @@ -554,7 +554,7 @@ def __getattr__(self, k):
FirmwareInfo = namedtuple("FirmwareInfo", ["kind", "name", "version", "extras"])


@dataclass
@dataclasses.dataclass
class Battery:
"""Information about the current state of a battery"""

Expand Down
41 changes: 25 additions & 16 deletions lib/logitech_receiver/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import errno as _errno
import errno
import logging
import threading as _threading
import threading
import time

from typing import Optional

import hidapi as _hid
import solaar.configuration as _configuration
import hidapi

from solaar import configuration

from . import base
from . import descriptors
Expand All @@ -33,9 +33,9 @@
from . import hidpp20
from . import hidpp20_constants
from . import settings
from . import settings_templates
from .common import ALERT
from .common import Battery
from .settings_templates import check_feature_settings as _check_feature_settings

logger = logging.getLogger(__name__)

Expand All @@ -58,7 +58,7 @@ def create_device(device_info, setting_callback=None):
return Device(None, None, None, handle=handle, device_info=device_info, setting_callback=setting_callback)
except OSError as e:
logger.exception("open %s", device_info)
if e.errno == _errno.EACCES:
if e.errno == errno.EACCES:
raise
except Exception:
logger.exception("open %s", device_info)
Expand All @@ -69,7 +69,16 @@ class Device:
read_register = hidpp10.read_register
write_register = hidpp10.write_register

def __init__(self, receiver, number, online, pairing_info=None, handle=None, device_info=None, setting_callback=None):
def __init__(
self,
receiver,
number,
online,
pairing_info=None,
handle=None,
device_info=None,
setting_callback=None,
):
assert receiver or device_info
if receiver:
assert 0 < number <= 15 # some receivers have devices past their max # of devices
Expand Down Expand Up @@ -109,14 +118,14 @@ def __init__(self, receiver, number, online, pairing_info=None, handle=None, dev
self._active = None # lags self.online - is used to help determine when to setup devices

self._feature_settings_checked = False
self._gestures_lock = _threading.Lock()
self._settings_lock = _threading.Lock()
self._persister_lock = _threading.Lock()
self._gestures_lock = threading.Lock()
self._settings_lock = threading.Lock()
self._persister_lock = threading.Lock()
self._notification_handlers = {} # See `add_notification_handler`
self.cleanups = [] # functions to run on the device when it is closed

if not self.path:
self.path = _hid.find_paired_node(receiver.path, number, 1) if receiver else None
self.path = hidapi.find_paired_node(receiver.path, number, 1) if receiver else None
if not self.handle:
try:
self.handle = base.open_path(self.path) if self.path else None
Expand Down Expand Up @@ -308,9 +317,9 @@ def profiles(self):
self._profiles = _hidpp20.get_profiles(self)
return self._profiles

def set_configuration(self, configuration, no_reply=False):
def set_configuration(self, configuration_, no_reply=False):
if self.online and self.protocol >= 2.0:
_hidpp20.config_change(self, configuration, no_reply=no_reply)
_hidpp20.config_change(self, configuration_, no_reply=no_reply)

def reset(self, no_reply=False):
self.set_configuration(0, no_reply)
Expand All @@ -320,7 +329,7 @@ def persister(self):
if not self._persister:
with self._persister_lock:
if not self._persister:
self._persister = _configuration.persister(self)
self._persister = configuration.persister(self)
return self._persister

@property
Expand All @@ -343,7 +352,7 @@ def settings(self):
if not self._feature_settings_checked:
with self._settings_lock:
if not self._feature_settings_checked:
self._feature_settings_checked = _check_feature_settings(self, self._settings)
self._feature_settings_checked = settings_templates.check_feature_settings(self, self._settings)
return self._settings

def battery(self): # None or level, next, status, voltage
Expand Down
Loading

0 comments on commit 716b288

Please sign in to comment.