From 67164e7b233bb1c0beeae66226903a1d246badc6 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 16 Apr 2023 18:50:40 -0700 Subject: [PATCH] fix mypy issues --- jc/parsers/bluetoothctl.py | 60 +++++++++++++++++++++++++++----------- tests/test_bluetoothctl.py | 4 +-- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/jc/parsers/bluetoothctl.py b/jc/parsers/bluetoothctl.py index 9c026e59..eabb2cbb 100644 --- a/jc/parsers/bluetoothctl.py +++ b/jc/parsers/bluetoothctl.py @@ -97,7 +97,7 @@ Examples: ] """ import re -from typing import List, Dict, Union +from typing import List, Dict, Optional, Any from jc.jc_types import JSONDictType import jc.utils @@ -157,8 +157,8 @@ try: }, ) except ImportError: - Controller = Dict[str, Union[str, bool, List[str]]] - Device = Dict[str, Union[str, bool, int, List[str]]] + Controller = Dict[str, Any] # type: ignore + Device = Dict[str, Any] # type: ignore _controller_head_pattern = r"Controller (?P
([0-9A-F]{2}:){5}[0-9A-F]{2}) (?P.+)" @@ -176,7 +176,7 @@ _controller_line_pattern = ( + r"|\s*UUID:\s*(?P.+))" ) -def _parse_controller(next_lines: List[str]) -> Controller: +def _parse_controller(next_lines: List[str]) -> Optional[Controller]: next_line = next_lines.pop() result = re.match(_controller_head_pattern, next_line) @@ -191,9 +191,21 @@ def _parse_controller(next_lines: List[str]) -> Controller: if name.endswith("not available"): return None - controller = Controller = { - "address": matches["address"], - } + controller: Controller = { + "name": '', + "is_default": False, + "is_public": False, + "address": matches["address"], + "alias": '', + "class": '', + "powered": '', + "discoverable": '', + "discoverable_timeout": '', + "pairable": '', + "modalias": '', + "discovering": '', + "uuids": [], + } if name.endswith("[default]"): controller["is_default"] = True @@ -260,7 +272,7 @@ _device_line_pattern = ( ) -def _parse_device(next_lines: List[str]) -> Device: +def _parse_device(next_lines: List[str], quiet: bool) -> Optional[Device]: next_line = next_lines.pop() result = re.match(_device_head_pattern, next_line) @@ -275,8 +287,22 @@ def _parse_device(next_lines: List[str]) -> Device: if name.endswith("not available"): return None - device = Device = { + device: Device = { + "name": '', + "is_public": False, "address": matches["address"], + "alias": '', + "class": '', + "icon": '', + "paired": '', + "bonded": '', + "trusted": '', + "blocked": '', + "connected": '', + "legacy_pairing": '', + "rssi": 0, + "txpower": 0, + "uuids": [], } if name.endswith("(public)"): @@ -315,20 +341,20 @@ def _parse_device(next_lines: List[str]) -> Device: device["connected"] = matches["connected"] elif matches["legacy_pairing"]: device["legacy_pairing"] = matches["legacy_pairing"] - elif matches["modalias"]: - device["modalias"] = matches["modalias"] elif matches["rssi"]: rssi = matches["rssi"] try: device["rssi"] = int(rssi) - except ValueError and not quiet: - jc.utils.warning_message([f"{next_line} : rssi - {rssi} is not int-able"]) + except ValueError: + if not quiet: + jc.utils.warning_message([f"{next_line} : rssi - {rssi} is not int-able"]) elif matches["txpower"]: txpower = matches["txpower"] try: device["txpower"] = int(txpower) - except ValueError and not quiet: - jc.utils.warning_message([f"{next_line} : txpower - {txpower} is not int-able"]) + except ValueError: + if not quiet: + jc.utils.warning_message([f"{next_line} : txpower - {txpower} is not int-able"]) elif matches["uuid"]: if not "uuids" in device: device["uuids"] = [] @@ -350,7 +376,7 @@ def parse(data: str, raw: bool = False, quiet: bool = False) -> List[JSONDictTyp List of Dictionaries. Raw or processed structured data. """ - result: List[TypeDict] = [] + result: List = [] if jc.utils.has_data(data): jc.utils.compatibility(__name__, info.compatible, quiet) @@ -364,7 +390,7 @@ def parse(data: str, raw: bool = False, quiet: bool = False) -> List[JSONDictTyp if data.startswith("Controller"): element = _parse_controller(linedata) elif data.startswith("Device"): - element = _parse_device(linedata) + element = _parse_device(linedata, quiet) # type: ignore if element: result.append(element) diff --git a/tests/test_bluetoothctl.py b/tests/test_bluetoothctl.py index 2cf1d09f..34d30c8f 100644 --- a/tests/test_bluetoothctl.py +++ b/tests/test_bluetoothctl.py @@ -134,7 +134,7 @@ class BluetoothctlTests(unittest.TestCase): if actual: for k, v in expected[0].items(): self.assertEqual(v, actual[0][k], f"Controller regex failed on {k}") - + for k, v in expected[1].items(): self.assertEqual(v, actual[1][k], f"Controller regex failed on {k}") @@ -209,7 +209,7 @@ class BluetoothctlTests(unittest.TestCase): if actual: for k, v in expected[0].items(): self.assertEqual(v, actual[0][k], f"Device regex failed on {k}") - + for k, v in expected[1].items(): self.assertEqual(v, actual[1][k], f"Device regex failed on {k}")