winpnp 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,137 @@
1
+ import ctypes
2
+ from array import array
3
+ from ctypes import FormatError, Structure, byref, c_int32, c_uint16, sizeof, windll
4
+ from dataclasses import dataclass, field
5
+ from datetime import datetime, timezone, tzinfo
6
+ from itertools import takewhile
7
+ from typing import TYPE_CHECKING, Any, Literal, Optional
8
+ from uuid import UUID
9
+
10
+ from more_itertools import sliced
11
+
12
+ from winpnp import _setupapi
13
+
14
+ if TYPE_CHECKING:
15
+ # Import only if type checking to avoid circular import
16
+ from .pnp_property import PnpPropertyKey, PnpPropertyType
17
+
18
+ _GUID_BYTE_SIZE = 16
19
+ _FILETIME_BYTE_SIZE = 8
20
+
21
+ _ArrayIntTypeCode = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
22
+ _ArrayFloatTypeCode = Literal["f", "d"]
23
+
24
+
25
+ class _SYSTEMTIME(Structure):
26
+ _fields_ = (
27
+ ("wYear", c_uint16),
28
+ ("wMonth", c_uint16),
29
+ ("wDayOfWeek", c_uint16),
30
+ ("wDay", c_uint16),
31
+ ("wHour", c_uint16),
32
+ ("wMinute", c_uint16),
33
+ ("wSecond", c_uint16),
34
+ ("wMilliseconds", c_uint16),
35
+ )
36
+
37
+ def to_datetime(self, time_zone: Optional[tzinfo] = None) -> datetime:
38
+ return datetime(
39
+ self.wYear,
40
+ self.wMonth,
41
+ self.wDay,
42
+ self.wHour,
43
+ self.wMinute,
44
+ self.wSecond,
45
+ self.wMilliseconds * 1000,
46
+ time_zone,
47
+ )
48
+
49
+
50
+ @dataclass(frozen=True)
51
+ class Win32Error:
52
+ code: int
53
+ description: str = field(init=False, compare=False)
54
+
55
+ def __post_init__(self) -> None:
56
+ # using object.__setattr__ because self is frozen
57
+ object.__setattr__(self, "description", FormatError(c_int32(self.code).value))
58
+
59
+
60
+ @dataclass(frozen=True)
61
+ class NtStatus:
62
+ code: int
63
+ description: str = field(init=False, compare=False)
64
+
65
+ def __post_init__(self) -> None:
66
+ # using object.__setattr__ because self is frozen
67
+ object.__setattr__(
68
+ self,
69
+ "description",
70
+ FormatError(c_int32(windll.ntdll.RtlNtStatusToDosError(self.code)).value),
71
+ )
72
+
73
+
74
+ def decode_raw(data: bytes) -> bytes:
75
+ return data
76
+
77
+
78
+ def decode_integers(data: bytes, typecode: _ArrayIntTypeCode) -> list[int]:
79
+ return list(array(typecode, data))
80
+
81
+
82
+ def decode_floats(data: bytes, typecode: _ArrayFloatTypeCode) -> list[float]:
83
+ return list(array(typecode, data))
84
+
85
+
86
+ def decode_guids(data: bytes) -> list[UUID]:
87
+ return [UUID(bytes_le=x) for x in sliced(data, _GUID_BYTE_SIZE, strict=True)]
88
+
89
+
90
+ def decode_filetimes(data: bytes) -> list[datetime]:
91
+ output = []
92
+ systemtime = _SYSTEMTIME()
93
+
94
+ for filetime in sliced(data, _FILETIME_BYTE_SIZE, strict=True):
95
+ if not windll.kernel32.FileTimeToSystemTime(filetime, byref(systemtime)):
96
+ raise ctypes.WinError()
97
+ output.append(systemtime.to_datetime(timezone.utc))
98
+
99
+ return output
100
+
101
+
102
+ def decode_booleans(data: bytes) -> list[bool]:
103
+ return [x != 0 for x in data]
104
+
105
+
106
+ def decode_string(data: bytes) -> str:
107
+ string = data.decode("utf-16-le")
108
+ return "".join(takewhile(lambda char: char != "\0", string))
109
+
110
+
111
+ def decode_strings(data: bytes) -> list[str]:
112
+ strings = data.decode("utf-16-le").split("\0")
113
+ return list(takewhile(lambda x: len(x) > 0, strings))
114
+
115
+
116
+ def decode_property_keys(data: bytes) -> list["PnpPropertyKey[Any]"]:
117
+ # Import in function to avoid circular import
118
+ from .pnp_property import PnpPropertyKey
119
+
120
+ count = len(data) // sizeof(_setupapi.DEVPROPKEY)
121
+ keys = (_setupapi.DEVPROPKEY * count).from_buffer_copy(data)
122
+ return [PnpPropertyKey(key.fmtid.to_uuid(), key.pid) for key in keys]
123
+
124
+
125
+ def decode_property_types(data: bytes) -> list["PnpPropertyType[Any]"]:
126
+ # Import in function to avoid circular import
127
+ from .pnp_property import PnpPropertyType
128
+
129
+ return [PnpPropertyType(type_id) for type_id in decode_integers(data, "L")]
130
+
131
+
132
+ def decode_win32_errors(data: bytes) -> list[Win32Error]:
133
+ return [Win32Error(code) for code in array("L", data)]
134
+
135
+
136
+ def decode_nt_statuses(data: bytes) -> list[NtStatus]:
137
+ return [NtStatus(code) for code in array("L", data)]
@@ -0,0 +1,18 @@
1
+ from uuid import UUID
2
+
3
+ from winpnp.properties import kinds
4
+ from winpnp.properties.pnp_property import PnpPropertyKey
5
+
6
+ from . import (
7
+ dev_query,
8
+ device,
9
+ device_class,
10
+ device_container,
11
+ device_interface,
12
+ device_interface_class,
13
+ driver_package,
14
+ )
15
+
16
+ NAME = PnpPropertyKey.register_new(
17
+ UUID("{b725f130-47ef-101a-a5f102608c9eebac}"), 10, "NAME", (kinds.STRING,)
18
+ )
@@ -0,0 +1,88 @@
1
+ from uuid import UUID
2
+
3
+ from winpnp.properties import kinds
4
+ from winpnp.properties.pnp_property import PnpPropertyKey
5
+
6
+ DEVICE_ADDRESS = PnpPropertyKey.register_new(
7
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
8
+ 1,
9
+ "Bluetooth_DeviceAddress",
10
+ (kinds.STRING,),
11
+ )
12
+
13
+ SERVICE_GUID = PnpPropertyKey.register_new(
14
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
15
+ 2,
16
+ "Bluetooth_ServiceGUID",
17
+ (kinds.GUID,),
18
+ )
19
+
20
+ DEVICE_FLAGS = PnpPropertyKey.register_new(
21
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
22
+ 3,
23
+ "Bluetooth_DeviceFlags",
24
+ (kinds.UINT32,),
25
+ )
26
+
27
+ DEVICE_MANUFACTURER = PnpPropertyKey.register_new(
28
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
29
+ 4,
30
+ "Bluetooth_DeviceManufacturer",
31
+ (kinds.STRING,),
32
+ )
33
+
34
+ DEVICE_MODEL_NUMBER = PnpPropertyKey.register_new(
35
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
36
+ 5,
37
+ "Bluetooth_DeviceModelNumber",
38
+ (kinds.STRING,),
39
+ )
40
+
41
+ DEVICE_VID_SOURCE = PnpPropertyKey.register_new(
42
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
43
+ 6,
44
+ "Bluetooth_DeviceVIDSource",
45
+ (kinds.BYTE,),
46
+ )
47
+
48
+ DEVICE_VID = PnpPropertyKey.register_new(
49
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
50
+ 7,
51
+ "Bluetooth_DeviceVID",
52
+ (kinds.UINT16,),
53
+ )
54
+
55
+ DEVICE_PID = PnpPropertyKey.register_new(
56
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
57
+ 8,
58
+ "Bluetooth_DevicePID",
59
+ (kinds.UINT16,),
60
+ )
61
+
62
+ DEVICE_PRODUCT_VERSION = PnpPropertyKey.register_new(
63
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
64
+ 9,
65
+ "Bluetooth_DeviceProductVersion",
66
+ (kinds.UINT16,),
67
+ )
68
+
69
+ CLASS_OF_DEVICE = PnpPropertyKey.register_new(
70
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
71
+ 10,
72
+ "Bluetooth_ClassOfDevice",
73
+ (kinds.UINT32,),
74
+ )
75
+
76
+ LAST_CONNECTED_TIME = PnpPropertyKey.register_new(
77
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
78
+ 11,
79
+ "Bluetooth_LastConnectedTime",
80
+ (kinds.FILETIME,),
81
+ )
82
+
83
+ LAST_SEEN_TIME = PnpPropertyKey.register_new(
84
+ UUID("{2bd67d8b-8beb-48d5-87e06cda3428040a}"),
85
+ 12,
86
+ "Bluetooth_LastSeenTime",
87
+ (kinds.FILETIME,),
88
+ )
@@ -0,0 +1,11 @@
1
+ from uuid import UUID
2
+
3
+ from winpnp.properties import kinds
4
+ from winpnp.properties.pnp_property import PnpPropertyKey
5
+
6
+ OBJECT_TYPE = PnpPropertyKey.register_new(
7
+ UUID("{13673f42-a3d6-49f6-b4daae46e0c5237c}"),
8
+ 2,
9
+ "DevQuery_ObjectType",
10
+ (kinds.UINT32,),
11
+ )