11import json
22import os
3+ import re
34from typing import List
45
56from conf_manager import confManager
@@ -26,6 +27,7 @@ def __init__(self):
2627 self .hwmon_curve_paths = [] # 风扇曲线写入路径
2728
2829 self .hwmon_black_list = [] # 风扇hwmon黑名单
30+ self .hwmon_black_list_rules = [] # 风扇hwmon黑名单规则
2931
3032 # EC配置变量
3133 self .is_ec_configured = False # 是否配置好风扇ec
@@ -122,6 +124,14 @@ def __parse_fan_configuration_HWMON(self, name_path_map):
122124 )
123125 else []
124126 )
127+ fc .hwmon_black_list_rules = (
128+ hwmon_config ["black_list_rules" ]
129+ if (
130+ "black_list_rules" in hwmon_config
131+ and hwmon_config ["black_list_rules" ] is not None
132+ )
133+ else []
134+ )
125135
126136 fan_pwm_write = hwmon_config ["pwm_write" ]
127137 pwm_write_max = fan_pwm_write ["pwm_write_max" ]
@@ -229,6 +239,75 @@ def __parse_fan_configuration_HWMON(self, name_path_map):
229239 except Exception :
230240 logger .error (f"获取风扇({ hwmon_name } )hwmon信息失败:" , exc_info = True )
231241
242+ def __match_hwmon_black_list_value (self , target_value , rule_value , match_mode ):
243+ def _match_string (single_rule_value : str ):
244+ if match_mode == "contains" :
245+ return single_rule_value in target_value
246+ if match_mode == "regex" :
247+ try :
248+ return re .search (single_rule_value , target_value ) is not None
249+ except re .error :
250+ logger .error (
251+ f"Invalid hwmon black_list_rules regex: { single_rule_value } " ,
252+ exc_info = True ,
253+ )
254+ return False
255+ return target_value == single_rule_value
256+
257+ if isinstance (rule_value , str ):
258+ return _match_string (rule_value )
259+ if isinstance (rule_value , list ):
260+ return any (
261+ isinstance (single_rule_value , str )
262+ and _match_string (single_rule_value )
263+ for single_rule_value in rule_value
264+ )
265+ return False
266+
267+ def __is_hwmon_blacklisted (self , fan_config : FanConfig ):
268+ if PRODUCT_NAME in fan_config .hwmon_black_list :
269+ logger .info (
270+ f"hwmon black_list matched: product_name={ PRODUCT_NAME } , hwmon={ fan_config .hwmon_name } "
271+ )
272+ return True
273+
274+ for rule in fan_config .hwmon_black_list_rules :
275+ if not isinstance (rule , dict ):
276+ continue
277+
278+ match_mode = rule .get ("match" , "exact" )
279+ if not isinstance (match_mode , str ):
280+ match_mode = "exact"
281+ match_mode = match_mode .lower ()
282+ if match_mode not in ("exact" , "contains" , "regex" ):
283+ logger .error (
284+ f"Invalid hwmon black_list_rules match mode: { match_mode } , fallback to exact"
285+ )
286+ match_mode = "exact"
287+
288+ has_match_item = False
289+ is_matched = True
290+
291+ if "product_name" in rule :
292+ has_match_item = True
293+ is_matched = is_matched and self .__match_hwmon_black_list_value (
294+ PRODUCT_NAME , rule ["product_name" ], match_mode
295+ )
296+
297+ if "product_version" in rule :
298+ has_match_item = True
299+ is_matched = is_matched and self .__match_hwmon_black_list_value (
300+ PRODUCT_VERSION , rule ["product_version" ], match_mode
301+ )
302+
303+ if has_match_item and is_matched :
304+ logger .info (
305+ f"hwmon black_list_rules matched: product_name={ PRODUCT_NAME } , product_version={ PRODUCT_VERSION } , hwmon={ fan_config .hwmon_name } , rule={ rule } "
306+ )
307+ return True
308+
309+ return False
310+
232311 # 获取配置值,支持直接数值或按机型字典格式
233312 def __get_value_for_product (self , config_value , default = 0 ):
234313 """获取配置值,支持直接数值或按机型字典格式"""
@@ -361,9 +440,7 @@ def parse_fan_configuration(self):
361440
362441 # self.fan_config_list 筛选, 去除黑名单
363442 self .fan_config_list = list (
364- filter (
365- lambda x : PRODUCT_NAME not in x .hwmon_black_list , self .fan_config_list
366- )
443+ filter (lambda x : not self .__is_hwmon_blacklisted (x ), self .fan_config_list )
367444 )
368445 # self.fan_config_list = [
369446 # config
0 commit comments