From 7e5abc68742c1cedf0c20b8f568230dbe73d5454 Mon Sep 17 00:00:00 2001 From: xiazimo <64586065+xiazimo@users.noreply.github.com> Date: Sat, 10 May 2025 23:23:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=83=85=E5=86=B5=E4=B8=8B,=20`core=E4=BF=A1=E6=81=AF?= =?UTF-8?q?`=E6=97=A0=E6=B3=95=E5=93=8D=E5=BA=94=20(#119)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * get_hw.py * 🚨 `pre-commit-ci`修复格式错误 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- gsuid_core/status/get_hw.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/gsuid_core/status/get_hw.py b/gsuid_core/status/get_hw.py index f6c3e72..2b9d3e4 100644 --- a/gsuid_core/status/get_hw.py +++ b/gsuid_core/status/get_hw.py @@ -7,18 +7,41 @@ from gsuid_core.logger import logger def get_cpu_info(): + cpu_name = None # 初始化变量 + + # 尝试从 /proc/cpuinfo 获取型号 try: with open('/proc/cpuinfo', 'r') as f: for line in f: if line.startswith('model name'): cpu_name = line.split(': ')[1].strip() - break + break # 找到后立即退出循环 except FileNotFoundError: - cpu_name = platform.processor() or "Unknown CPU" + logger.warning("未找到 /proc/cpuinfo 文件,尝试其他方式获取CPU名称") + # 如果未从文件获取到名称,尝试其他方式 + if cpu_name is None: + logger.debug( + "未从 /proc/cpuinfo 获取到CPU名称,尝试 platform.processor()" + ) + try: + cpu_name = platform.processor() or "Unknown CPU" + except Exception as e: + logger.error(f"获取CPU名称失败: {e}") + cpu_name = "Unknown CPU" + + # 找不到,干脆别找了,叫Unknown CPU也挺好,至少比后台报错前台半天没响应的好QAQ + + # 处理CPU名称格式 + try: + cpu_name = ' '.join(cpu_name.split()[:2]) + except Exception: + cpu_name = "Unknown CPU" + + # 获取核心数和使用率 cores = psutil.cpu_count(logical=True) usage = psutil.cpu_percent(interval=1) - cpu_name = ' '.join(cpu_name.split()[:2]) + return {"name": f"{cpu_name} ({cores}核)", "value": usage}