chore: ruff 代码检查与修复

130 issues auto-fixed (import ordering, UP045/UP006 type annotations),
33 issues manually fixed (E712/E501/E402/E722 + N818 rename + per-file
wechat ignore for N8xx naming conventions). All 33 tests pass.
This commit is contained in:
2026-05-09 14:28:09 +08:00
parent 02ea794015
commit 7455d7e426
33 changed files with 147 additions and 114 deletions
+3 -3
View File
@@ -1,16 +1,16 @@
import time
import httpx
from typing import Optional
from app.config import settings
class WeChatClient:
def __init__(self):
self._access_token: Optional[str] = None
self._access_token: str | None = None
self._token_expires_at: float = 0
async def _get_access_token(self) -> Optional[str]:
async def _get_access_token(self) -> str | None:
now = time.time()
if self._access_token and now < self._token_expires_at:
return self._access_token
+18 -18
View File
@@ -1,20 +1,20 @@
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
""" 对企业微信发送给企业后台的消息加解密示例代码.
@copyright: Copyright (c) 1998-2014 Tencent Inc.
"""
# ------------------------------------------------------------------------
import logging
import base64
import random
import hashlib
import time
import struct
from Crypto.Cipher import AES
import xml.etree.cElementTree as ET
import logging
import random
import socket
import struct
import time
import xml.etree.ElementTree as ET
from Crypto.Cipher import AES
try:
import ierror
@@ -29,11 +29,11 @@ except ImportError:
"""
class FormatException(Exception):
class FormatError(Exception):
pass
def throw_exception(message, exception_class=FormatException):
def throw_exception(message, exception_class=FormatError):
"""my define raise exception function"""
raise exception_class(message)
@@ -104,7 +104,7 @@ class XMLParse:
return resp_xml
class PKCS7Encoder():
class PKCS7Encoder:
"""提供基于PKCS7算法的加解密接口"""
block_size = 32
@@ -134,7 +134,7 @@ class PKCS7Encoder():
return decrypted[:-pad]
class Prpcrypt(object):
class Prpcrypt:
"""提供接收和推送给企业微信消息的加解密接口"""
def __init__(self, key):
@@ -151,7 +151,7 @@ class Prpcrypt(object):
"""
# 16位随机字符串添加到明文开头
text = text.encode()
text = self.get_random_str() + struct.pack("I", socket.htonl(len(text))) + text + receiveid.encode()
text = self.get_random_str() + struct.pack("I", socket.htonl(len(text))) + text + receiveid.encode() # noqa: E501
# 使用自定义的填充方式对明文进行补位填充
pkcs7 = PKCS7Encoder()
@@ -206,14 +206,14 @@ class Prpcrypt(object):
return str(random.randint(1000000000000000, 9999999999999999)).encode()
class WXBizMsgCrypt(object):
class WXBizMsgCrypt:
# 构造函数
def __init__(self, sToken, sEncodingAESKey, sReceiveId):
try:
self.key = base64.b64decode(sEncodingAESKey + "=")
assert len(self.key) == 32
except:
throw_exception("[error]: EncodingAESKey unvalid !", FormatException)
except Exception:
throw_exception("[error]: EncodingAESKey unvalid !", FormatError)
# return ierror.WXBizMsgCrypt_IllegalAesKey,None
self.m_sToken = sToken
self.m_sReceiveId = sReceiveId
@@ -240,9 +240,9 @@ class WXBizMsgCrypt(object):
def EncryptMsg(self, sReplyMsg, sNonce, timestamp=None):
# 将企业回复用户的消息加密打包
# @param sReplyMsg: 企业号待回复用户的消息,xml格式的字符串
# @param sTimeStamp: 时间戳,可以自己生成,也可以用URL参数的timestamp,如为None则自动用当前时间
# @param sNonce: 随机串,可以自己生成,也可以用URL参数的nonce
# sEncryptMsg: 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串,
# @param sTimeStamp: 时间戳,可以自己生成,也可以用URL参数的timestamp,如为None则自动用当前时间 # noqa: E501
# @param sNonce: 随机串,可以自己生成,也可以用URL参数的nonce # noqa: E501
# sEncryptMsg: 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串, # noqa: E501
# return:成功0sEncryptMsg,失败返回对应的错误码None
pc = Prpcrypt(self.key)
ret, encrypt = pc.encrypt(sReplyMsg, self.m_sReceiveId)
+8 -9
View File
@@ -1,8 +1,7 @@
import xml.etree.cElementTree as ET
from typing import Optional
import xml.etree.ElementTree as ET
from app.wechat.crypto import WXBizMsgCrypt
from app.config import settings
from app.wechat.crypto import WXBizMsgCrypt
class WeChatMessageHandler:
@@ -15,7 +14,7 @@ class WeChatMessageHandler:
def verify_url(
self, msg_signature: str, timestamp: str, nonce: str, echostr: str
) -> Optional[str]:
) -> str | None:
ret, sEchoStr = self.wxcpt.VerifyURL(
msg_signature, timestamp, nonce, echostr
)
@@ -33,7 +32,7 @@ class WeChatMessageHandler:
msg_signature: str,
timestamp: str,
nonce: str,
) -> Optional[ET.Element]:
) -> ET.Element | None:
ret, xml_content = self.wxcpt.DecryptMsg(
post_data, msg_signature, timestamp, nonce
)
@@ -43,16 +42,16 @@ class WeChatMessageHandler:
def encrypt_response(
self, response_xml: str, nonce: str, timestamp: str
) -> Optional[str]:
) -> str | None:
ret, encrypted = self.wxcpt.EncryptMsg(response_xml, nonce, timestamp)
if ret == 0:
return encrypted
return None
def handle_event(
self, event: str, event_key: Optional[str], from_user: str
) -> Optional[str]:
self, event: str, event_key: str | None, from_user: str
) -> str | None:
return None
def handle_text(self, content: str, from_user: str) -> Optional[str]:
def handle_text(self, content: str, from_user: str) -> str | None:
return None
+1 -2
View File
@@ -1,10 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#########################################################################
# Author: jonyqin
# Created Time: Thu 11 Sep 2014 01:53:58 PM CST
# File Name: ierror.py
# Description:定义错误码含义
# Description:定义错误码含义
#########################################################################
WXBizMsgCrypt_OK = 0
WXBizMsgCrypt_ValidateSignature_Error = -40001