Skip to content

Commit 0ad468f

Browse files
authored
feat(utils): new ProtoStruct (#36)
* feat: new ProtoStruct * fix: evaluate before decode * fix: fields init * chore: improve evaluate * chore: evaluate_all
1 parent a7b541a commit 0ad468f

File tree

10 files changed

+240
-166
lines changed

10 files changed

+240
-166
lines changed

lagrange/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .utils.sign import sign_provider
1010
from .info import InfoManager
1111
from .info.app import app_list
12+
from .utils.binary.protobuf.models import evaluate_all
1213

1314

1415
class Lagrange:
@@ -66,3 +67,6 @@ def launch(self):
6667
log.root.info("Program exited by user")
6768
else:
6869
log.root.info("Program exited normally")
70+
71+
72+
evaluate_all()

lagrange/client/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def _task_exec(self, client: "Client", event: "BaseEvent", handler: EVENT_
3232
try:
3333
await handler(client, event)
3434
except Exception as e:
35-
log.root.error(
35+
log.root.exception(
3636
f"Unhandled exception on task {event}", exc_info=e
3737
)
3838

lagrange/pb/highway/comm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class AudioExtInfo(ProtoStruct):
3434

3535

3636
class ExtBizInfo(ProtoStruct):
37-
pic: PicExtInfo = proto_field(1, default=PicExtInfo())
38-
video: VideoExtInfo = proto_field(2, default=VideoExtInfo())
39-
audio: AudioExtInfo = proto_field(3, default=AudioExtInfo())
37+
pic: PicExtInfo = proto_field(1, default_factory=PicExtInfo)
38+
video: VideoExtInfo = proto_field(2, default_factory=VideoExtInfo)
39+
audio: AudioExtInfo = proto_field(3, default_factory=AudioExtInfo)
4040
bus_type: Optional[int] = proto_field(4, default=None)
4141

4242

lagrange/pb/highway/req.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class DownloadVideoExt(ProtoStruct):
6363

6464
class DownloadExt(ProtoStruct):
6565
pic_ext: Optional[bytes] = proto_field(1, default=None)
66-
video_ext: DownloadVideoExt = proto_field(2, default=DownloadVideoExt())
66+
video_ext: DownloadVideoExt = proto_field(2, default_factory=DownloadVideoExt)
6767
ptt_ext: Optional[bytes] = proto_field(3, default=None)
6868

6969

7070
class DownloadReq(ProtoStruct):
7171
node: IndexNode = proto_field(1)
72-
ext: DownloadExt = proto_field(2, default=DownloadExt())
72+
ext: DownloadExt = proto_field(2, default_factory=DownloadExt)
7373

7474

7575
class NTV2RichMediaReq(ProtoStruct):

lagrange/pb/highway/rsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class DownloadInfo(ProtoStruct):
4141
domain: str = proto_field(1)
4242
url_path: str = proto_field(2)
4343
https_port: Optional[int] = proto_field(3, default=None)
44-
v4_addrs: list[IPv4] = proto_field(4, default=[])
45-
v6_addrs: list[IPv6] = proto_field(5, default=[])
44+
v4_addrs: list[IPv4] = proto_field(4, default_factory=list)
45+
v6_addrs: list[IPv6] = proto_field(5, default_factory=list)
4646
pic_info: Optional[PicUrlExtInfo] = proto_field(6, default=None)
4747
video_info: Optional[VideoExtInfo] = proto_field(7, default=None)
4848

lagrange/pb/message/rich_text/elems.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class NotOnlineImage(ProtoStruct):
5353
width: int = proto_field(9)
5454
res_id: str = proto_field(10)
5555
origin_path: Optional[str] = proto_field(15, default=None)
56-
args: ImageReserveArgs = proto_field(34, default=ImageReserveArgs())
56+
args: ImageReserveArgs = proto_field(34, default_factory=ImageReserveArgs)
5757

5858

5959
class TransElem(ProtoStruct):
@@ -90,7 +90,7 @@ class CustomFace(ProtoStruct):
9090
width: int = proto_field(22)
9191
height: int = proto_field(23)
9292
size: int = proto_field(25)
93-
args: ImageReserveArgs = proto_field(34, default=ImageReserveArgs())
93+
args: ImageReserveArgs = proto_field(34, default_factory=ImageReserveArgs)
9494

9595

9696
class ExtraInfo(ProtoStruct):
@@ -110,7 +110,7 @@ class SrcMsg(ProtoStruct):
110110
seq: int = proto_field(1)
111111
uin: int = proto_field(2, default=0)
112112
timestamp: int = proto_field(3)
113-
elems: list[dict] = proto_field(5, default=[{}])
113+
elems: list[dict] = proto_field(5, default_factory=lambda: [{}])
114114
pb_reserved: Optional[SrcMsgArgs] = proto_field(8, default=None)
115115
to_uin: int = proto_field(10, default=0)
116116

lagrange/pb/service/friend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PBGetFriendListRequest(ProtoStruct):
4545
f7: int = proto_field(7, default=2147483647) # MaxValue
4646
body: list[GetFriendBody] = proto_field(
4747
10001,
48-
default=[
48+
default_factory=lambda: [
4949
GetFriendBody(type=1, f2=GetFriendNumbers(f1=[103, 102, 20002, 27394])),
5050
GetFriendBody(type=4, f2=GetFriendNumbers(f1=[100, 101, 102])),
5151
],

lagrange/pb/service/group.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ class MemberInfoLevel(ProtoStruct):
319319
num: int = proto_field(2)
320320

321321

322+
class GetGrpMemberInfoRsp(ProtoStruct):
323+
grp_id: int = proto_field(1)
324+
body: "list[GetGrpMemberInfoRspBody]" = proto_field(2)
325+
next_key: Optional[bytes] = proto_field(15, default=None) # base64(pb)
326+
327+
322328
class GetGrpMemberInfoRspBody(ProtoStruct):
323329
account: AccountInfo = proto_field(1)
324330
nickname: str = proto_field(10, default="")
@@ -342,12 +348,6 @@ def is_owner(self) -> bool:
342348
return not self.is_admin and self.permission == 2
343349

344350

345-
class GetGrpMemberInfoRsp(ProtoStruct):
346-
grp_id: int = proto_field(1)
347-
body: list[GetGrpMemberInfoRspBody] = proto_field(2)
348-
next_key: Optional[bytes] = proto_field(15, default=None) # base64(pb)
349-
350-
351351
class GetGrpListReqBody(ProtoStruct):
352352
cfg1: bytes = proto_field(1)
353353
cfg2: bytes = proto_field(2)
@@ -396,7 +396,7 @@ class GrpInfo(ProtoStruct):
396396

397397

398398
class GetGrpListResponse(ProtoStruct):
399-
grp_list: list[GrpInfo] = proto_field(2, default=[])
399+
grp_list: list[GrpInfo] = proto_field(2, default_factory=list)
400400

401401

402402
class PBGetInfoFromUidReq(ProtoStruct):
@@ -425,19 +425,19 @@ def to_str(self) -> str:
425425

426426

427427
class GetInfoRspField(ProtoStruct, debug=True):
428-
int_t: list[GetInfoRspF1] = proto_field(1, default=[])
429-
str_t: list[GetInfoRspF2] = proto_field(2, default=[])
428+
int_t: list[GetInfoRspF1] = proto_field(1, default_factory=list)
429+
str_t: list[GetInfoRspF2] = proto_field(2, default_factory=list)
430+
431+
432+
class GetInfoFromUidRsp(ProtoStruct):
433+
body: list["GetInfoRspBody"] = proto_field(1)
430434

431435

432436
class GetInfoRspBody(ProtoStruct):
433437
uid: str = proto_field(1)
434438
fields: GetInfoRspField = proto_field(2)
435439

436440

437-
class GetInfoFromUidRsp(ProtoStruct):
438-
body: list[GetInfoRspBody] = proto_field(1)
439-
440-
441441
class Oidb88D0Args(ProtoStruct):
442442
seq: Optional[int] = proto_field(22, default=None)
443443

lagrange/pb/status/group.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ class GroupSub16Head(ProtoStruct):
111111
f44: Optional[PBGroupReaction] = proto_field(44, default=None) # set reaction only
112112

113113

114+
class GroupSub20Head(ProtoStruct):
115+
f1: int = proto_field(1) # 20
116+
grp_id: int = proto_field(4)
117+
f13: int = proto_field(13) # 19
118+
body: "GroupSub20Body" = proto_field(26)
119+
120+
114121
class GroupSub20Body(ProtoStruct):
115122
type: int = proto_field(1) # 12: nudge, 14: group_sign
116123
# f2: int = proto_field(2) # 1061
@@ -121,13 +128,6 @@ class GroupSub20Body(ProtoStruct):
121128
f10: int = proto_field(10) # rand?
122129

123130

124-
class GroupSub20Head(ProtoStruct):
125-
f1: int = proto_field(1) # 20
126-
grp_id: int = proto_field(4)
127-
f13: int = proto_field(13) # 19
128-
body: GroupSub20Body = proto_field(26)
129-
130-
131131
class PBGroupAlbumUpdateBody(ProtoStruct):
132132
# f1: 6
133133
args: str = proto_field(2)

0 commit comments

Comments
 (0)