FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RevitDevTool.PyTest/src/revitdevtool_pytest/bridge.py at main · trgiangv/RevitDevTool.PyTest · GitHub
trgiangv
/
RevitDevTool.PyTest
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
RevitDevTool.PyTest
/
src
/
revitdevtool_pytest
/
bridge.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
237 lines (200 loc) · 7.51 KB
Breadcrumbs
RevitDevTool.PyTest
/
src
/
revitdevtool_pytest
/
bridge.py
Copy path
File metadata and controls
237 lines (200 loc) · 7.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""Named Pipe client for the DevTool bridge.
Protocol: ``[4-byte LE body length][UTF-8 JSON body]``
Matches ``RevitDevTool.McpParser.Models.BridgePipeConnection``.
"""
from
__future__
import
annotations
import
json
import
logging
import
struct
import
time
from
collections
.
abc
import
Callable
from
typing
import
Any
from
.
constants
import
(
BRIDGE_METHOD_IPY_TESTS_RUN
,
BRIDGE_METHOD_TESTS_RUN
,
BRIDGE_MSG_TYPE_NOTIFICATION
,
DEFAULT_CONNECT_TIMEOUT_MS
,
DEFAULT_TEST_TIMEOUT_S
,
PLUGIN_NAME
,
)
from
.
models
import
(
BridgeRequest
,
BridgeResponse
,
CollectionError
,
RunRequest
,
RunResponse
,
)
log
=
logging
.
getLogger
(
PLUGIN_NAME
)
_MAX_FRAME_SIZE
=
16
*
1024
*
1024
_HEADER_FMT
=
"<I"
_HEADER_LEN
=
struct
.
calcsize
(
_HEADER_FMT
)
NotificationCallback
=
Callable
[[
str
,
Any
],
None
]
class
HostBridge
:
"""Synchronous Named Pipe client for the DevTool bridge."""
def
__init__
(
self
,
pipe_name
:
str
,
*
,
connect_timeout_ms
:
int
=
DEFAULT_CONNECT_TIMEOUT_MS
,
)
->
None
:
self
.
_pipe_name
=
pipe_name
self
.
_connect_timeout_ms
=
connect_timeout_ms
self
.
_handle
:
Any
=
None
# -- Connection lifecycle -----------------------------------------------
def
connect
(
self
)
->
None
:
import
win32file
# type: ignore[import-untyped]
import
win32pipe
# type: ignore[import-untyped]
pipe_path
=
rf"\\.\pipe\
{
self
.
_pipe_name
}
"
deadline
=
time
.
monotonic
()
+
self
.
_connect_timeout_ms
/
1000.0
while
True
:
try
:
self
.
_handle
=
win32file
.
CreateFile
(
pipe_path
,
win32file
.
GENERIC_READ
|
win32file
.
GENERIC_WRITE
,
0
,
None
,
win32file
.
OPEN_EXISTING
,
0
,
None
,
)
win32pipe
.
SetNamedPipeHandleState
(
self
.
_handle
,
win32pipe
.
PIPE_READMODE_BYTE
,
None
,
None
,
)
return
except
Exception
:
# noqa
if
time
.
monotonic
()
>=
deadline
:
raise
ConnectionError
(
f"Cannot connect to pipe '
{
self
.
_pipe_name
}
' "
f"within
{
self
.
_connect_timeout_ms
}
ms"
)
time
.
sleep
(
0.2
)
def
disconnect
(
self
)
->
None
:
handle
,
self
.
_handle
=
self
.
_handle
,
None
if
handle
is
None
:
return
import
win32file
# type: ignore[import-untyped]
try
:
win32file
.
FlushFileBuffers
(
handle
)
except
Exception
:
# noqa
pass
try
:
win32file
.
CloseHandle
(
handle
)
except
Exception
:
# noqa
pass
@
property
def
connected
(
self
)
->
bool
:
if
self
.
_handle
is
None
:
return
False
try
:
import
win32pipe
# type: ignore[import-untyped]
win32pipe
.
PeekNamedPipe
(
self
.
_handle
,
0
)
return
True
except
Exception
:
# noqa
log
.
debug
(
"Pipe health check failed, marking as disconnected"
)
return
False
# -- Public RPC methods -------------------------------------------------
def
run_tests
(
self
,
workspace_root
:
str
,
test_root
:
str
,
nodeids
:
list
[
str
],
*
,
pytest_args
:
list
[
str
]
|
None
=
None
,
timeout_s
:
float
=
DEFAULT_TEST_TIMEOUT_S
,
on_notification
:
NotificationCallback
|
None
=
None
,
)
->
RunResponse
:
request
=
RunRequest
(
workspace_root
=
workspace_root
,
test_root
=
test_root
,
nodeids
=
nodeids
,
pytest_args
=
pytest_args
or
[],
)
response
:
BridgeResponse
=
self
.
_request
(
BridgeRequest
(
method
=
BRIDGE_METHOD_TESTS_RUN
,
params
=
request
.
to_params
()),
timeout_s
,
on_notification
=
on_notification
,
)
return
_parse_run_response
(
response
)
def
run_ipy_tests
(
self
,
workspace_root
:
str
,
test_root
:
str
,
nodeids
:
list
[
str
],
*
,
pytest_args
:
list
[
str
]
|
None
=
None
,
timeout_s
:
float
=
DEFAULT_TEST_TIMEOUT_S
,
on_notification
:
NotificationCallback
|
None
=
None
,
)
->
RunResponse
:
request
=
RunRequest
(
workspace_root
=
workspace_root
,
test_root
=
test_root
,
nodeids
=
nodeids
,
pytest_args
=
pytest_args
or
[],
)
response
:
BridgeResponse
=
self
.
_request
(
BridgeRequest
(
method
=
BRIDGE_METHOD_IPY_TESTS_RUN
,
params
=
request
.
to_params
()),
timeout_s
,
on_notification
=
on_notification
,
)
return
_parse_run_response
(
response
)
# -- Wire protocol ------------------------------------------------------
def
_request
(
self
,
req
:
BridgeRequest
,
timeout_s
:
float
,
*
,
on_notification
:
NotificationCallback
|
None
=
None
,
)
->
BridgeResponse
:
self
.
_write_frame
(
req
.
to_json_bytes
())
deadline
=
time
.
monotonic
()
+
timeout_s
while
time
.
monotonic
()
<
deadline
:
data
=
self
.
_read_frame
(
deadline
-
time
.
monotonic
())
parsed
=
json
.
loads
(
data
)
if
parsed
.
get
(
"type"
)
==
BRIDGE_MSG_TYPE_NOTIFICATION
:
_dispatch_notification
(
parsed
,
on_notification
)
continue
return
BridgeResponse
.
from_json
(
parsed
)
raise
TimeoutError
(
f"Timed out waiting for response to
{
req
.
id
}
"
)
def
_write_frame
(
self
,
body
:
bytes
)
->
None
:
import
win32file
# type: ignore[import-untyped]
win32file
.
WriteFile
(
self
.
_handle
,
struct
.
pack
(
_HEADER_FMT
,
len
(
body
))
+
body
)
def
_read_frame
(
self
,
timeout_s
:
float
)
->
bytes
:
header
=
self
.
_read_exact
(
_HEADER_LEN
,
timeout_s
)
(
body_len
,)
=
struct
.
unpack
(
_HEADER_FMT
,
header
)
if
body_len
<=
0
or
body_len
>
_MAX_FRAME_SIZE
:
raise
RuntimeError
(
f"Invalid frame length:
{
body_len
}
"
)
return
self
.
_read_exact
(
body_len
,
timeout_s
)
def
_read_exact
(
self
,
count
:
int
,
timeout_s
:
float
)
->
bytes
:
import
win32file
# type: ignore[import-untyped]
buf
=
bytearray
()
deadline
=
time
.
monotonic
()
+
timeout_s
while
len
(
buf
)
<
count
:
if
time
.
monotonic
()
>=
deadline
:
raise
TimeoutError
(
f"Timed out reading
{
count
}
bytes (got
{
len
(
buf
)
}
)"
)
_
,
data
=
win32file
.
ReadFile
(
self
.
_handle
,
count
-
len
(
buf
))
# type: ignore[call-overload]
if
not
data
:
raise
ConnectionError
(
"Pipe closed while reading"
)
buf
.
extend
(
data
)
# type: ignore[arg-type]
return
bytes
(
buf
)
# ---------------------------------------------------------------------------
# Response parsing helpers
# ---------------------------------------------------------------------------
def
_parse_run_response
(
response
:
BridgeResponse
)
->
RunResponse
:
if
response
.
is_error
:
return
RunResponse
(
exit_code
=
1
,
collection_errors
=
(
CollectionError
(
message
=
response
.
error_message
),),
)
if
isinstance
(
response
.
result
,
dict
):
return
RunResponse
.
from_dict
(
response
.
result
)
return
RunResponse
(
exit_code
=
1
,
collection_errors
=
(
CollectionError
(
message
=
f"Unexpected response:
{
response
.
result
}
"
),
),
)
def
_dispatch_notification
(
parsed
:
dict
[
str
,
Any
],
callback
:
NotificationCallback
|
None
,
)
->
None
:
if
callback
is
None
:
return
method
=
parsed
.
get
(
"method"
,
""
)
params
=
parsed
.
get
(
"params"
)
try
:
callback
(
method
,
params
)
except
Exception
:
# noqa
log
.
debug
(
"Notification callback error for method=%s"
,
method
,
exc_info
=
True
)
Back
|
FazBrowse Home
|
New Git URL