mirror of
https://github.com/google/bumble.git
synced 2026-04-18 00:45:32 +00:00
Ruff: Add and fix UP rules
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
from bumble import utils
|
||||
from bumble.snoop import create_snooper
|
||||
@@ -111,7 +110,7 @@ async def open_transport(name: str) -> Transport:
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def _open_transport(scheme: str, spec: Optional[str]) -> Transport:
|
||||
async def _open_transport(scheme: str, spec: str | None) -> Transport:
|
||||
# pylint: disable=import-outside-toplevel
|
||||
# pylint: disable=too-many-return-statements
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
# Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
import logging
|
||||
from typing import Optional, Union
|
||||
|
||||
import grpc.aio
|
||||
|
||||
@@ -44,7 +43,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_android_emulator_transport(spec: Optional[str]) -> Transport:
|
||||
async def open_android_emulator_transport(spec: str | None) -> Transport:
|
||||
'''
|
||||
Open a transport connection to an Android emulator via its gRPC interface.
|
||||
The parameter string has this syntax:
|
||||
@@ -89,7 +88,7 @@ async def open_android_emulator_transport(spec: Optional[str]) -> Transport:
|
||||
logger.debug('connecting to gRPC server at %s', server_address)
|
||||
channel = grpc.aio.insecure_channel(server_address)
|
||||
|
||||
service: Union[EmulatedBluetoothServiceStub, VhciForwardingServiceStub]
|
||||
service: EmulatedBluetoothServiceStub | VhciForwardingServiceStub
|
||||
if mode == 'host':
|
||||
# Connect as a host
|
||||
service = EmulatedBluetoothServiceStub(channel)
|
||||
|
||||
@@ -22,7 +22,6 @@ import os
|
||||
import pathlib
|
||||
import platform
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
import grpc.aio
|
||||
|
||||
@@ -66,7 +65,7 @@ DEFAULT_VARIANT = ''
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def get_ini_dir() -> Optional[pathlib.Path]:
|
||||
def get_ini_dir() -> pathlib.Path | None:
|
||||
if sys.platform == 'darwin':
|
||||
if tmpdir := os.getenv('TMPDIR', None):
|
||||
return pathlib.Path(tmpdir)
|
||||
@@ -100,7 +99,7 @@ def find_grpc_port(instance_number: int) -> int:
|
||||
ini_file = ini_dir / ini_file_name(instance_number)
|
||||
logger.debug(f'Looking for .ini file at {ini_file}')
|
||||
if ini_file.is_file():
|
||||
with open(ini_file, 'r') as ini_file_data:
|
||||
with open(ini_file) as ini_file_data:
|
||||
for line in ini_file_data.readlines():
|
||||
if '=' in line:
|
||||
key, value = line.split('=')
|
||||
@@ -146,7 +145,7 @@ def publish_grpc_port(grpc_port: int, instance_number: int) -> bool:
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_android_netsim_controller_transport(
|
||||
server_host: Optional[str], server_port: int, options: dict[str, str]
|
||||
server_host: str | None, server_port: int, options: dict[str, str]
|
||||
) -> Transport:
|
||||
if server_host == '_' or not server_host:
|
||||
server_host = 'localhost'
|
||||
@@ -301,9 +300,9 @@ async def open_android_netsim_controller_transport(
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_android_netsim_host_transport_with_address(
|
||||
server_host: Optional[str],
|
||||
server_host: str | None,
|
||||
server_port: int,
|
||||
options: Optional[dict[str, str]] = None,
|
||||
options: dict[str, str] | None = None,
|
||||
):
|
||||
if server_host == '_' or not server_host:
|
||||
server_host = 'localhost'
|
||||
@@ -328,7 +327,7 @@ async def open_android_netsim_host_transport_with_address(
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_android_netsim_host_transport_with_channel(
|
||||
channel, options: Optional[dict[str, str]] = None
|
||||
channel, options: dict[str, str] | None = None
|
||||
):
|
||||
# Wrapper for I/O operations
|
||||
class HciDevice:
|
||||
@@ -408,7 +407,7 @@ async def open_android_netsim_host_transport_with_channel(
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_android_netsim_transport(spec: Optional[str]) -> Transport:
|
||||
async def open_android_netsim_transport(spec: str | None) -> Transport:
|
||||
'''
|
||||
Open a transport connection as a client or server, implementing Android's `netsim`
|
||||
simulator protocol over gRPC.
|
||||
|
||||
@@ -23,7 +23,7 @@ import io
|
||||
import logging
|
||||
import struct
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any, ContextManager, Optional, Protocol
|
||||
from typing import Any, Protocol
|
||||
|
||||
from bumble import core, hci
|
||||
from bumble.colors import color
|
||||
@@ -107,11 +107,11 @@ class PacketParser:
|
||||
NEED_LENGTH = 1
|
||||
NEED_BODY = 2
|
||||
|
||||
sink: Optional[TransportSink]
|
||||
sink: TransportSink | None
|
||||
extended_packet_info: dict[int, tuple[int, int, str]]
|
||||
packet_info: Optional[tuple[int, int, str]] = None
|
||||
packet_info: tuple[int, int, str] | None = None
|
||||
|
||||
def __init__(self, sink: Optional[TransportSink] = None) -> None:
|
||||
def __init__(self, sink: TransportSink | None = None) -> None:
|
||||
self.sink = sink
|
||||
self.extended_packet_info = {}
|
||||
self.reset()
|
||||
@@ -176,7 +176,7 @@ class PacketReader:
|
||||
self.source = source
|
||||
self.at_end = False
|
||||
|
||||
def next_packet(self) -> Optional[bytes]:
|
||||
def next_packet(self) -> bytes | None:
|
||||
# Get the packet type
|
||||
packet_type = self.source.read(1)
|
||||
if len(packet_type) != 1:
|
||||
@@ -253,7 +253,7 @@ class BaseSource:
|
||||
"""
|
||||
|
||||
terminated: asyncio.Future[None]
|
||||
sink: Optional[TransportSink]
|
||||
sink: TransportSink | None
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.terminated = asyncio.get_running_loop().create_future()
|
||||
@@ -357,7 +357,7 @@ class Transport:
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
class PumpedPacketSource(ParserSource):
|
||||
pump_task: Optional[asyncio.Task[None]]
|
||||
pump_task: asyncio.Task[None] | None
|
||||
|
||||
def __init__(self, receive) -> None:
|
||||
super().__init__()
|
||||
@@ -390,7 +390,7 @@ class PumpedPacketSource(ParserSource):
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
class PumpedPacketSink:
|
||||
pump_task: Optional[asyncio.Task[None]]
|
||||
pump_task: asyncio.Task[None] | None
|
||||
|
||||
def __init__(self, send: Callable[[bytes], Awaitable[Any]]):
|
||||
self.send_function = send
|
||||
@@ -443,7 +443,7 @@ class SnoopingTransport(Transport):
|
||||
|
||||
@staticmethod
|
||||
def create_with(
|
||||
transport: Transport, snooper: ContextManager[Snooper]
|
||||
transport: Transport, snooper: contextlib.AbstractContextManager[Snooper]
|
||||
) -> SnoopingTransport:
|
||||
"""
|
||||
Create an instance given a snooper that works as as context manager.
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
# Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import io
|
||||
import logging
|
||||
|
||||
from bumble.transport.common import StreamPacketSink, StreamPacketSource, Transport
|
||||
@@ -36,7 +35,7 @@ async def open_file_transport(spec: str) -> Transport:
|
||||
'''
|
||||
|
||||
# Open the file
|
||||
file = io.open(spec, 'r+b', buffering=0)
|
||||
file = open(spec, 'r+b', buffering=0)
|
||||
|
||||
# Setup reading
|
||||
read_transport, packet_source = await asyncio.get_running_loop().connect_read_pipe(
|
||||
|
||||
@@ -22,7 +22,6 @@ import logging
|
||||
import os
|
||||
import socket
|
||||
import struct
|
||||
from typing import Optional
|
||||
|
||||
from bumble.transport.common import ParserSource, Transport
|
||||
|
||||
@@ -33,7 +32,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_hci_socket_transport(spec: Optional[str]) -> Transport:
|
||||
async def open_hci_socket_transport(spec: str | None) -> Transport:
|
||||
'''
|
||||
Open an HCI Socket (only available on some platforms).
|
||||
The parameter string is either empty (to use the first/default Bluetooth adapter)
|
||||
@@ -87,7 +86,7 @@ async def open_hci_socket_transport(spec: Optional[str]) -> Transport:
|
||||
)
|
||||
!= 0
|
||||
):
|
||||
raise IOError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
|
||||
raise OSError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
|
||||
|
||||
class HciSocketSource(ParserSource):
|
||||
def __init__(self, hci_socket):
|
||||
|
||||
@@ -17,12 +17,10 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import atexit
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import pty
|
||||
import tty
|
||||
from typing import Optional
|
||||
|
||||
from bumble.transport.common import StreamPacketSink, StreamPacketSource, Transport
|
||||
|
||||
@@ -33,7 +31,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_pty_transport(spec: Optional[str]) -> Transport:
|
||||
async def open_pty_transport(spec: str | None) -> Transport:
|
||||
'''
|
||||
Open a PTY transport.
|
||||
The parameter string may be empty, or a path name where a symbolic link
|
||||
@@ -48,11 +46,11 @@ async def open_pty_transport(spec: Optional[str]) -> Transport:
|
||||
tty.setraw(replica)
|
||||
|
||||
read_transport, packet_source = await asyncio.get_running_loop().connect_read_pipe(
|
||||
StreamPacketSource, io.open(primary, 'rb', closefd=False)
|
||||
StreamPacketSource, open(primary, 'rb', closefd=False)
|
||||
)
|
||||
|
||||
write_transport, _ = await asyncio.get_running_loop().connect_write_pipe(
|
||||
asyncio.BaseProtocol, io.open(primary, 'wb', closefd=False)
|
||||
asyncio.BaseProtocol, open(primary, 'wb', closefd=False)
|
||||
)
|
||||
packet_sink = StreamPacketSink(write_transport)
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import asyncio
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import usb.core
|
||||
import usb.util
|
||||
@@ -389,7 +388,7 @@ def _set_port_status(device: UsbDevice, port: int, on: bool):
|
||||
)
|
||||
|
||||
|
||||
def _find_device_by_path(sys_path: str) -> Optional[UsbDevice]:
|
||||
def _find_device_by_path(sys_path: str) -> UsbDevice | None:
|
||||
"""Finds a USB device based on its system path."""
|
||||
bus_num, *port_parts = sys_path.split('-')
|
||||
ports = [int(port) for port in port_parts[0].split('.')]
|
||||
@@ -402,7 +401,7 @@ def _find_device_by_path(sys_path: str) -> Optional[UsbDevice]:
|
||||
return None
|
||||
|
||||
|
||||
def _find_hub_by_device_path(sys_path: str) -> Optional[UsbDevice]:
|
||||
def _find_hub_by_device_path(sys_path: str) -> UsbDevice | None:
|
||||
"""Finds the USB hub associated with a specific device path."""
|
||||
hub_sys_path = sys_path.rsplit('.', 1)[0]
|
||||
hub_device = _find_device_by_path(hub_sys_path)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import serial_asyncio
|
||||
|
||||
@@ -52,7 +51,7 @@ class SerialPacketSource(StreamPacketSource):
|
||||
logger.debug('connection made')
|
||||
self._ready.set()
|
||||
|
||||
def connection_lost(self, exc: Optional[Exception]) -> None:
|
||||
def connection_lost(self, exc: Exception | None) -> None:
|
||||
logger.debug('connection lost')
|
||||
self.on_transport_lost()
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
# Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from bumble.transport.common import Transport
|
||||
from bumble.transport.file import open_file_transport
|
||||
@@ -28,7 +27,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
async def open_vhci_transport(spec: Optional[str]) -> Transport:
|
||||
async def open_vhci_transport(spec: str | None) -> Transport:
|
||||
'''
|
||||
Open a VHCI transport (only available on some platforms).
|
||||
The parameter string is either empty (to use the default VHCI device
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
# Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import websockets.asyncio.server
|
||||
|
||||
@@ -43,8 +42,8 @@ async def open_ws_server_transport(spec: str) -> Transport:
|
||||
class WsServerTransport(Transport):
|
||||
sink: PumpedPacketSink
|
||||
source: ParserSource
|
||||
connection: Optional[websockets.asyncio.server.ServerConnection]
|
||||
server: Optional[websockets.asyncio.server.Server]
|
||||
connection: websockets.asyncio.server.ServerConnection | None
|
||||
server: websockets.asyncio.server.Server | None
|
||||
|
||||
def __init__(self) -> None:
|
||||
source = ParserSource()
|
||||
|
||||
Reference in New Issue
Block a user