201 lines
7.7 KiB
Python
201 lines
7.7 KiB
Python
"""
|
||
诊断样式保存失败的具体问题
|
||
"""
|
||
import os
|
||
import traceback
|
||
from style_manager import style_manager, DocumentStyle, FontStyle, ParagraphStyle, HeadingStyle
|
||
|
||
def diagnose_save_failure():
|
||
"""诊断样式保存失败的具体问题"""
|
||
print("=== 诊断样式保存失败问题 ===")
|
||
|
||
# 1. 检查文件权限
|
||
print("\n1. 检查文件系统权限")
|
||
styles_dir = style_manager.styles_dir
|
||
print(f"样式目录: {styles_dir}")
|
||
|
||
if not os.path.exists(styles_dir):
|
||
try:
|
||
os.makedirs(styles_dir, exist_ok=True)
|
||
print("✅ 样式目录创建成功")
|
||
except Exception as e:
|
||
print(f"❌ 无法创建样式目录: {e}")
|
||
return
|
||
else:
|
||
print("✅ 样式目录已存在")
|
||
|
||
# 检查权限
|
||
readable = os.access(styles_dir, os.R_OK)
|
||
writable = os.access(styles_dir, os.W_OK)
|
||
executable = os.access(styles_dir, os.X_OK)
|
||
|
||
print(f"目录可读: {'✅' if readable else '❌'}")
|
||
print(f"目录可写: {'✅' if writable else '❌'}")
|
||
print(f"目录可执行: {'✅' if executable else '❌'}")
|
||
|
||
if not writable:
|
||
print("❌ 目录没有写入权限,这是保存失败的主要原因!")
|
||
print("解决方案:")
|
||
print("1. 以管理员身份运行程序")
|
||
print("2. 或者更改程序存储目录到用户可写的位置")
|
||
return
|
||
|
||
# 测试文件写入
|
||
test_file = os.path.join(styles_dir, "permission_test.tmp")
|
||
try:
|
||
with open(test_file, 'w', encoding='utf-8') as f:
|
||
f.write("test")
|
||
os.remove(test_file)
|
||
print("✅ 文件写入测试成功")
|
||
except Exception as e:
|
||
print(f"❌ 文件写入测试失败: {e}")
|
||
return
|
||
|
||
# 2. 检查内置样式覆盖问题
|
||
print("\n2. 检查内置样式覆盖问题")
|
||
builtin_names = list(style_manager.builtin_styles.keys())
|
||
print(f"内置样式列表: {builtin_names}")
|
||
|
||
def test_save_with_builtin_name():
|
||
"""测试使用内置样式名称保存"""
|
||
test_style = DocumentStyle(
|
||
name="爆款文章风格", # 这是内置样式名
|
||
description="测试覆盖内置样式",
|
||
body_font=FontStyle(name="微软雅黑", size=14),
|
||
body_paragraph=ParagraphStyle(line_spacing=1.5)
|
||
)
|
||
|
||
result = style_manager.create_custom_style(test_style)
|
||
if not result:
|
||
print("✅ 正确阻止了内置样式覆盖")
|
||
else:
|
||
print("❌ 错误地允许了内置样式覆盖")
|
||
|
||
test_save_with_builtin_name()
|
||
|
||
# 3. 检查样式对象结构
|
||
print("\n3. 检查样式对象结构")
|
||
|
||
def test_complete_style_object():
|
||
"""测试完整的样式对象"""
|
||
try:
|
||
# 创建完整的样式对象
|
||
complete_style = DocumentStyle(
|
||
name="结构测试样式",
|
||
description="用于测试对象结构",
|
||
body_font=FontStyle(name="微软雅黑", size=14, bold=False, italic=False, color="#000000"),
|
||
body_paragraph=ParagraphStyle(line_spacing=1.5, space_before=0, space_after=0, first_line_indent=0.0)
|
||
)
|
||
|
||
# 添加标题样式
|
||
complete_style.heading_styles = {}
|
||
for level in range(1, 4):
|
||
complete_style.heading_styles[level] = HeadingStyle(
|
||
font=FontStyle(name="微软雅黑", size=18-level*2, bold=True, color="#1F497D"),
|
||
paragraph=ParagraphStyle(line_spacing=1.3, space_before=12, space_after=6),
|
||
outline_level=level
|
||
)
|
||
|
||
print("✅ 样式对象结构创建成功")
|
||
|
||
# 测试序列化
|
||
from dataclasses import asdict
|
||
import json
|
||
|
||
style_dict = asdict(complete_style)
|
||
json_str = json.dumps(style_dict, ensure_ascii=False, indent=2)
|
||
print("✅ 样式对象序列化成功")
|
||
|
||
# 测试保存
|
||
save_result = style_manager.create_custom_style(complete_style)
|
||
if save_result:
|
||
print("✅ 完整样式对象保存成功")
|
||
return True
|
||
else:
|
||
print("❌ 完整样式对象保存失败")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ 样式对象结构测试失败: {e}")
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
structure_test_result = test_complete_style_object()
|
||
|
||
# 4. 检查不完整的样式对象
|
||
print("\n4. 检查不完整样式对象的处理")
|
||
|
||
def test_incomplete_style_object():
|
||
"""测试不完整的样式对象"""
|
||
try:
|
||
# 创建不完整的样式对象(缺少必要字段)
|
||
incomplete_style = DocumentStyle(
|
||
name="不完整测试样式",
|
||
description="用于测试不完整对象"
|
||
# 故意不设置 body_font 和 body_paragraph
|
||
)
|
||
|
||
print(f"body_font是否为None: {incomplete_style.body_font is None}")
|
||
print(f"body_paragraph是否为None: {incomplete_style.body_paragraph is None}")
|
||
|
||
# 由于__post_init__方法,这些应该被自动初始化
|
||
if incomplete_style.body_font and incomplete_style.body_paragraph:
|
||
print("✅ 不完整样式对象已自动补全")
|
||
|
||
save_result = style_manager.create_custom_style(incomplete_style)
|
||
if save_result:
|
||
print("✅ 补全后的样式对象保存成功")
|
||
return True
|
||
else:
|
||
print("❌ 补全后的样式对象保存失败")
|
||
return False
|
||
else:
|
||
print("❌ 样式对象自动补全失败")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ 不完整样式对象测试失败: {e}")
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
incomplete_test_result = test_incomplete_style_object()
|
||
|
||
# 5. 总结和建议
|
||
print("\n=== 诊断结果总结 ===")
|
||
|
||
if structure_test_result and incomplete_test_result:
|
||
print("✅ 样式对象结构正常")
|
||
print("✅ 文件权限正常")
|
||
print("✅ 内置样式保护正常")
|
||
print("\n可能的问题和解决方案:")
|
||
print("1. 确保样式名称不与内置样式重复")
|
||
print("2. 检查是否在高级编辑器中输入了有效的样式名称")
|
||
print("3. 确保修改后调用了正确的保存方法")
|
||
else:
|
||
print("❌ 发现问题,需要进一步修复")
|
||
if not structure_test_result:
|
||
print("- 样式对象结构存在问题")
|
||
if not incomplete_test_result:
|
||
print("- 样式对象自动补全机制存在问题")
|
||
|
||
def provide_save_tips():
|
||
"""提供保存操作的使用提示"""
|
||
print("\n=== 正确的保存操作步骤 ===")
|
||
print("1. 在高级编辑器中修改样式")
|
||
print("2. 确保填写了样式名称(不能为空)")
|
||
print("3. 确保样式名称不与内置样式重复")
|
||
print("4. 点击'保存样式'或'另存为'按钮")
|
||
print("5. 查看是否有错误提示对话框")
|
||
|
||
print("\n内置样式列表(不能覆盖):")
|
||
for i, name in enumerate(style_manager.builtin_styles.keys(), 1):
|
||
print(f" {i}. {name}")
|
||
|
||
print("\n如果仍然保存失败,请:")
|
||
print("1. 检查控制台输出的详细错误信息")
|
||
print("2. 确认程序有写入权限")
|
||
print("3. 尝试使用不同的样式名称")
|
||
|
||
if __name__ == '__main__':
|
||
diagnose_save_failure()
|
||
provide_save_tips() |