style: 统一 Ruff 代码格式
This commit is contained in:
+26
-20
@@ -9,6 +9,7 @@ Wup 包结构:
|
||||
tag7:sBuffer(bytes), tag8:iTimeout, tag9:context(map), tag10:status(map)
|
||||
sBuffer = Map<"tReq", 编码后的请求结构体>
|
||||
"""
|
||||
|
||||
import struct
|
||||
from typing import Any, Dict, Optional
|
||||
from .taf_protocol import TafOutputStream, TafInputStream, TafType, TafStruct
|
||||
@@ -18,16 +19,16 @@ class WupRequest:
|
||||
"""Wup 请求对象"""
|
||||
|
||||
def __init__(self):
|
||||
self.iVersion: int = 3 # tag 1
|
||||
self.cPacketType: int = 0 # tag 2
|
||||
self.iMessageType: int = 0 # tag 3
|
||||
self.iRequestId: int = 0 # tag 4
|
||||
self.sServantName: str = "" # tag 5
|
||||
self.sFuncName: str = "" # tag 6
|
||||
self.sBuffer: bytes = b'' # tag 7
|
||||
self.iTimeout: int = 3000 # tag 8
|
||||
self.context: Dict[str, str] = {} # tag 9
|
||||
self.status: Dict[str, str] = {} # tag 10
|
||||
self.iVersion: int = 3 # tag 1
|
||||
self.cPacketType: int = 0 # tag 2
|
||||
self.iMessageType: int = 0 # tag 3
|
||||
self.iRequestId: int = 0 # tag 4
|
||||
self.sServantName: str = "" # tag 5
|
||||
self.sFuncName: str = "" # tag 6
|
||||
self.sBuffer: bytes = b"" # tag 7
|
||||
self.iTimeout: int = 3000 # tag 8
|
||||
self.context: Dict[str, str] = {} # tag 9
|
||||
self.status: Dict[str, str] = {} # tag 10
|
||||
self.newdata: Dict[str, bytes] = {}
|
||||
|
||||
def setServant(self, name: str):
|
||||
@@ -49,7 +50,7 @@ class WupRequest:
|
||||
"""
|
||||
os = TafOutputStream()
|
||||
|
||||
if isinstance(struct_data, TafStruct) or hasattr(struct_data, 'write_to'):
|
||||
if isinstance(struct_data, TafStruct) or hasattr(struct_data, "write_to"):
|
||||
# 结构体对象:STRUCT_BEGIN + 内容 + STRUCT_END
|
||||
os.write_struct(0, struct_data)
|
||||
elif isinstance(struct_data, dict):
|
||||
@@ -83,7 +84,7 @@ class WupRequest:
|
||||
os.write_map(tag, value)
|
||||
elif isinstance(value, (list, tuple)):
|
||||
os.write_list(tag, list(value))
|
||||
elif hasattr(value, 'write_to'):
|
||||
elif hasattr(value, "write_to"):
|
||||
os.write_struct(tag, value)
|
||||
else:
|
||||
raise TypeError(f"不支持的字段类型: {type(value)}")
|
||||
@@ -115,14 +116,14 @@ class WupRequest:
|
||||
|
||||
# 3. 长度前缀
|
||||
length = 4 + len(wup_body)
|
||||
return struct.pack('>I', length) + wup_body
|
||||
return struct.pack(">I", length) + wup_body
|
||||
|
||||
|
||||
def normalize_wup_payload(data: bytes) -> bytes:
|
||||
"""去掉可选的 4 字节 WUP 长度前缀,返回裸 WUP body"""
|
||||
if len(data) < 4:
|
||||
return data
|
||||
declared_len = struct.unpack('>I', data[0:4])[0]
|
||||
declared_len = struct.unpack(">I", data[0:4])[0]
|
||||
if declared_len == len(data) or declared_len + 4 == len(data):
|
||||
return data[4:]
|
||||
return data
|
||||
@@ -138,7 +139,7 @@ class WupResponse:
|
||||
self.iRequestId: int = 0
|
||||
self.sServantName: str = ""
|
||||
self.sFuncName: str = ""
|
||||
self.sBuffer: bytes = b''
|
||||
self.sBuffer: bytes = b""
|
||||
self.iTimeout: int = 0
|
||||
self.context: Dict[str, str] = {}
|
||||
self.status: Dict[str, str] = {}
|
||||
@@ -249,19 +250,20 @@ class WupResponse:
|
||||
# 辅助:按已知 dtype 读取值
|
||||
# ============================================================
|
||||
|
||||
|
||||
def _read_string_value(ins: TafInputStream, dtype: int) -> str:
|
||||
if dtype == TafType.STRING1:
|
||||
length = struct.unpack('B', ins.buf.read(1))[0]
|
||||
length = struct.unpack("B", ins.buf.read(1))[0]
|
||||
elif dtype == TafType.STRING4:
|
||||
length = struct.unpack('>I', ins.buf.read(4))[0]
|
||||
length = struct.unpack(">I", ins.buf.read(4))[0]
|
||||
else:
|
||||
return ""
|
||||
return ins.buf.read(length).decode('utf-8', errors='replace')
|
||||
return ins.buf.read(length).decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _read_bytes_value(ins: TafInputStream, dtype: int) -> bytes:
|
||||
if dtype != TafType.SIMPLE_LIST:
|
||||
return b''
|
||||
return b""
|
||||
ins.read_head() # 元素类型 INT8
|
||||
length = ins._read_int_len()
|
||||
return ins.buf.read(length)
|
||||
@@ -276,6 +278,10 @@ def _read_map_value(ins: TafInputStream, dtype: int) -> Dict:
|
||||
_, kt = ins.read_head()
|
||||
k = _read_string_value(ins, kt)
|
||||
_, vt = ins.read_head()
|
||||
v = _read_string_value(ins, vt) if vt in (TafType.STRING1, TafType.STRING4) else ""
|
||||
v = (
|
||||
_read_string_value(ins, vt)
|
||||
if vt in (TafType.STRING1, TafType.STRING4)
|
||||
else ""
|
||||
)
|
||||
result[k] = v
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user