fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -23,12 +23,20 @@ try:
|
||||
from reportlab.lib.pagesizes import A4
|
||||
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
|
||||
from reportlab.lib.units import inch
|
||||
from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate, Spacer, Table, TableStyle
|
||||
from reportlab.platypus import (
|
||||
PageBreak,
|
||||
Paragraph,
|
||||
SimpleDocTemplate,
|
||||
Spacer,
|
||||
Table,
|
||||
TableStyle,
|
||||
)
|
||||
|
||||
REPORTLAB_AVAILABLE = True
|
||||
except ImportError:
|
||||
REPORTLAB_AVAILABLE = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExportEntity:
|
||||
id: str
|
||||
@@ -39,6 +47,7 @@ class ExportEntity:
|
||||
mention_count: int
|
||||
attributes: dict[str, Any]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExportRelation:
|
||||
id: str
|
||||
@@ -48,6 +57,7 @@ class ExportRelation:
|
||||
confidence: float
|
||||
evidence: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExportTranscript:
|
||||
id: str
|
||||
@@ -57,6 +67,7 @@ class ExportTranscript:
|
||||
segments: list[dict]
|
||||
entity_mentions: list[dict]
|
||||
|
||||
|
||||
class ExportManager:
|
||||
"""导出管理器 - 处理各种导出需求"""
|
||||
|
||||
@@ -159,7 +170,9 @@ class ExportManager:
|
||||
color = type_colors.get(entity.type, type_colors["default"])
|
||||
|
||||
# 节点圆圈
|
||||
svg_parts.append(f'<circle cx="{x}" cy="{y}" r="35" fill="{color}" stroke="white" stroke-width="3"/>')
|
||||
svg_parts.append(
|
||||
f'<circle cx="{x}" cy="{y}" r="35" fill="{color}" stroke="white" stroke-width="3"/>'
|
||||
)
|
||||
|
||||
# 实体名称
|
||||
svg_parts.append(
|
||||
@@ -184,16 +197,20 @@ class ExportManager:
|
||||
f'fill="white" stroke="#bdc3c7" rx="5"/>'
|
||||
)
|
||||
svg_parts.append(
|
||||
f'<text x="{legend_x}" y="{legend_y}" font-size="12" font-weight="bold" ' f'fill="#2c3e50">实体类型</text>'
|
||||
f'<text x="{legend_x}" y="{legend_y}" font-size="12" font-weight="bold" '
|
||||
f'fill="#2c3e50">实体类型</text>'
|
||||
)
|
||||
|
||||
for i, (etype, color) in enumerate(type_colors.items()):
|
||||
if etype != "default":
|
||||
y_pos = legend_y + 25 + i * 20
|
||||
svg_parts.append(f'<circle cx="{legend_x + 10}" cy="{y_pos}" r="8" fill="{color}"/>')
|
||||
svg_parts.append(
|
||||
f'<circle cx="{legend_x + 10}" cy="{y_pos}" r="8" fill="{color}"/>'
|
||||
)
|
||||
text_y = y_pos + 4
|
||||
svg_parts.append(
|
||||
f'<text x="{legend_x + 25}" y="{text_y}" font-size="10" ' f'fill="#2c3e50">{etype}</text>'
|
||||
f'<text x="{legend_x + 25}" y="{text_y}" font-size="10" '
|
||||
f'fill="#2c3e50">{etype}</text>'
|
||||
)
|
||||
|
||||
svg_parts.append("</svg>")
|
||||
@@ -283,7 +300,9 @@ class ExportManager:
|
||||
all_attrs.update(e.attributes.keys())
|
||||
|
||||
# 表头
|
||||
headers = ["ID", "名称", "类型", "定义", "别名", "提及次数"] + [f"属性:{a}" for a in sorted(all_attrs)]
|
||||
headers = ["ID", "名称", "类型", "定义", "别名", "提及次数"] + [
|
||||
f"属性:{a}" for a in sorted(all_attrs)
|
||||
]
|
||||
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(headers)
|
||||
@@ -314,7 +333,9 @@ class ExportManager:
|
||||
|
||||
return output.getvalue()
|
||||
|
||||
def export_transcript_markdown(self, transcript: ExportTranscript, entities_map: dict[str, ExportEntity]) -> str:
|
||||
def export_transcript_markdown(
|
||||
self, transcript: ExportTranscript, entities_map: dict[str, ExportEntity]
|
||||
) -> str:
|
||||
"""
|
||||
导出转录文本为 Markdown 格式
|
||||
|
||||
@@ -392,15 +413,25 @@ class ExportManager:
|
||||
raise ImportError("reportlab is required for PDF export")
|
||||
|
||||
output = io.BytesIO()
|
||||
doc = SimpleDocTemplate(output, pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18)
|
||||
doc = SimpleDocTemplate(
|
||||
output, pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18
|
||||
)
|
||||
|
||||
# 样式
|
||||
styles = getSampleStyleSheet()
|
||||
title_style = ParagraphStyle(
|
||||
"CustomTitle", parent=styles["Heading1"], fontSize=24, spaceAfter=30, textColor=colors.HexColor("#2c3e50")
|
||||
"CustomTitle",
|
||||
parent=styles["Heading1"],
|
||||
fontSize=24,
|
||||
spaceAfter=30,
|
||||
textColor=colors.HexColor("#2c3e50"),
|
||||
)
|
||||
heading_style = ParagraphStyle(
|
||||
"CustomHeading", parent=styles["Heading2"], fontSize=16, spaceAfter=12, textColor=colors.HexColor("#34495e")
|
||||
"CustomHeading",
|
||||
parent=styles["Heading2"],
|
||||
fontSize=16,
|
||||
spaceAfter=12,
|
||||
textColor=colors.HexColor("#34495e"),
|
||||
)
|
||||
|
||||
story = []
|
||||
@@ -408,7 +439,9 @@ class ExportManager:
|
||||
# 标题页
|
||||
story.append(Paragraph("InsightFlow 项目报告", title_style))
|
||||
story.append(Paragraph(f"项目名称: {project_name}", styles["Heading2"]))
|
||||
story.append(Paragraph(f"生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M')}", styles["Normal"]))
|
||||
story.append(
|
||||
Paragraph(f"生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M')}", styles["Normal"])
|
||||
)
|
||||
story.append(Spacer(1, 0.3 * inch))
|
||||
|
||||
# 统计概览
|
||||
@@ -458,7 +491,9 @@ class ExportManager:
|
||||
story.append(Paragraph("实体列表", heading_style))
|
||||
|
||||
entity_data = [["名称", "类型", "提及次数", "定义"]]
|
||||
for e in sorted(entities, key=lambda x: x.mention_count, reverse=True)[:50]: # 限制前50个
|
||||
for e in sorted(entities, key=lambda x: x.mention_count, reverse=True)[
|
||||
:50
|
||||
]: # 限制前50个
|
||||
entity_data.append(
|
||||
[
|
||||
e.name,
|
||||
@@ -468,7 +503,9 @@ class ExportManager:
|
||||
]
|
||||
)
|
||||
|
||||
entity_table = Table(entity_data, colWidths=[1.5 * inch, 1 * inch, 1 * inch, 2.5 * inch])
|
||||
entity_table = Table(
|
||||
entity_data, colWidths=[1.5 * inch, 1 * inch, 1 * inch, 2.5 * inch]
|
||||
)
|
||||
entity_table.setStyle(
|
||||
TableStyle(
|
||||
[
|
||||
@@ -495,7 +532,9 @@ class ExportManager:
|
||||
for r in relations[:100]: # 限制前100个
|
||||
relation_data.append([r.source, r.relation_type, r.target, f"{r.confidence:.2f}"])
|
||||
|
||||
relation_table = Table(relation_data, colWidths=[2 * inch, 1.5 * inch, 2 * inch, 1 * inch])
|
||||
relation_table = Table(
|
||||
relation_data, colWidths=[2 * inch, 1.5 * inch, 2 * inch, 1 * inch]
|
||||
)
|
||||
relation_table.setStyle(
|
||||
TableStyle(
|
||||
[
|
||||
@@ -557,16 +596,24 @@ class ExportManager:
|
||||
for r in relations
|
||||
],
|
||||
"transcripts": [
|
||||
{"id": t.id, "name": t.name, "type": t.type, "content": t.content, "segments": t.segments}
|
||||
{
|
||||
"id": t.id,
|
||||
"name": t.name,
|
||||
"type": t.type,
|
||||
"content": t.content,
|
||||
"segments": t.segments,
|
||||
}
|
||||
for t in transcripts
|
||||
],
|
||||
}
|
||||
|
||||
return json.dumps(data, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
# 全局导出管理器实例
|
||||
_export_manager = None
|
||||
|
||||
|
||||
def get_export_manager(db_manager=None) -> None:
|
||||
"""获取导出管理器实例"""
|
||||
global _export_manager
|
||||
|
||||
Reference in New Issue
Block a user