[Python] 纯文本查看 复制代码 from collections import Counter
def query_uid_gid_ugo():
results = [['/thdfq_khsfb/pnc', '600 600 550', 'uid/gid/ugohjdafhsafhndsmbhj', 'PASSED'],
['/thdfq_knvr/ota_cache', '200 200 775', 'uid/gid校验结果异常!预期值为200、307', 'FAILED'],
['/thdfq_para/swk', '700 201 775', 'uid/gid/ugo校验结果正常!', 'PASSED'],
['/dsv_knvr', '200 200 770', 'uid/gid/ugo校验结果正常!', 'PASSED'],
['/dxgmr', '200 200 770', 'uid/gid/ugo校验异常!预期值为907、600、301', 'PASSED'],
['/thdfq_knvr/logs', '600 600 777', 'uid/gid/ugo校验结果异常!预期值为1000、500、755', 'FAILED']]
return results
def formatted_print():
headers = ["Partitions", "UID/GID/UGO", "DetailsInfo", "CheckResult"]
data = query_uid_gid_ugo()
counter = Counter(row[-1] for row in data)
# 计算每列最大宽度
column_widths = [max(len(item) for item in column) for column in zip(headers, *data)]
# 打印表格
header_line = "+".join("-" * (width + 2) for width in column_widths)
print(f"+{header_line}+")
print("| " + " | ".join(f"{header:^{width}}" for header, width in zip(headers, column_widths)) + " |")
print(f"+{header_line}+")
for index, row in enumerate(data):
formatted_row = "| " + " | ".join(f"{item:^{width}}" for item, width in zip(row, column_widths)) + " |"
if row[-1] == "FAILED":
print(f"\033[96m{formatted_row}\033[0m")
else:
print(formatted_row)
print(f"+{header_line}+")
# 统计结果
print(f"校验完成,{counter['PASSED']}个目录访问权限状态正常,共计发现{counter['FAILED']}处异常。")
formatted_print() |