fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
This commit is contained in:
AutoFix Bot
2026-03-03 06:03:38 +08:00
parent 2a0ed6af4d
commit 9fd1da8fb7
41 changed files with 901 additions and 768 deletions

View File

@@ -300,22 +300,22 @@ class SecurityManager:
cursor.execute("CREATE INDEX IF NOT EXISTS idx_audit_logs_user ON audit_logs(user_id)")
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_resource "
"ON audit_logs(resource_type, resource_id)"
"ON audit_logs(resource_type, resource_id)",
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs(action_type)"
"CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs(action_type)",
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_created ON audit_logs(created_at)"
"CREATE INDEX IF NOT EXISTS idx_audit_logs_created ON audit_logs(created_at)",
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_encryption_project ON encryption_configs(project_id)"
"CREATE INDEX IF NOT EXISTS idx_encryption_project ON encryption_configs(project_id)",
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_masking_project ON masking_rules(project_id)"
"CREATE INDEX IF NOT EXISTS idx_masking_project ON masking_rules(project_id)",
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_access_policy_project ON data_access_policies(project_id)"
"CREATE INDEX IF NOT EXISTS idx_access_policy_project ON data_access_policies(project_id)",
)
conn.commit()
@@ -324,7 +324,7 @@ class SecurityManager:
def _generate_id(self) -> str:
"""生成唯一ID"""
return hashlib.sha256(
f"{datetime.now().isoformat()}{secrets.token_hex(16)}".encode()
f"{datetime.now().isoformat()}{secrets.token_hex(16)}".encode(),
).hexdigest()[:32]
# ==================== 审计日志 ====================
@@ -464,7 +464,7 @@ class SecurityManager:
return logs
def get_audit_stats(
self, start_time: str | None = None, end_time: str | None = None
self, start_time: str | None = None, end_time: str | None = None,
) -> dict[str, Any]:
"""获取审计统计"""
conn = sqlite3.connect(self.db_path)
@@ -804,7 +804,7 @@ class SecurityManager:
description=row[8],
created_at=row[9],
updated_at=row[10],
)
),
)
return rules
@@ -882,7 +882,7 @@ class SecurityManager:
return success
def apply_masking(
self, text: str, project_id: str, rule_types: list[MaskingRuleType] | None = None
self, text: str, project_id: str, rule_types: list[MaskingRuleType] | None = None,
) -> str:
"""应用脱敏规则到文本"""
rules = self.get_masking_rules(project_id)
@@ -906,7 +906,7 @@ class SecurityManager:
return masked_text
def apply_masking_to_entity(
self, entity_data: dict[str, Any], project_id: str
self, entity_data: dict[str, Any], project_id: str,
) -> dict[str, Any]:
"""对实体数据应用脱敏"""
masked_data = entity_data.copy()
@@ -982,7 +982,7 @@ class SecurityManager:
return policy
def get_access_policies(
self, project_id: str, active_only: bool = True
self, project_id: str, active_only: bool = True,
) -> list[DataAccessPolicy]:
"""获取数据访问策略"""
conn = sqlite3.connect(self.db_path)
@@ -1015,20 +1015,20 @@ class SecurityManager:
is_active=bool(row[10]),
created_at=row[11],
updated_at=row[12],
)
),
)
return policies
def check_access_permission(
self, policy_id: str, user_id: str, user_ip: str | None = None
self, policy_id: str, user_id: str, user_ip: str | None = None,
) -> tuple[bool, str | None]:
"""检查访问权限"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM data_access_policies WHERE id = ? AND is_active = 1", (policy_id,)
"SELECT * FROM data_access_policies WHERE id = ? AND is_active = 1", (policy_id,),
)
row = cursor.fetchone()
conn.close()
@@ -1163,7 +1163,7 @@ class SecurityManager:
return request
def approve_access_request(
self, request_id: str, approved_by: str, expires_hours: int = 24
self, request_id: str, approved_by: str, expires_hours: int = 24,
) -> AccessRequest | None:
"""批准访问请求"""
conn = sqlite3.connect(self.db_path)