File size: 2,360 Bytes
5d74609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from typing import Dict, Any
from fpdf import FPDF
import xml.etree.ElementTree as ET

logger = logging.getLogger(__name__)

def generate_report(analysis_results: Dict[str, Any]) -> Dict[str, Any]:
    """
    Generate a review report from the LLM's analysis results.
    """
    report = {
        "summary": {},
        "issues": [],
        "recommendations": [],
        "pdf_content": b""
    }
    
    # Parse the XML output
    raw_xml = analysis_results.get("raw_xml", "").strip()
    if not raw_xml:
        logger.error("Received empty or missing XML for report generation.")
        return report

    try:
        root = ET.fromstring(raw_xml)
        
        # Extract summary
        summary_node = root.find("summary")
        if summary_node is not None:
            report["summary"] = {
                "overall_assessment": summary_node.findtext("overall_assessment", "N/A"),
                "total_issues": summary_node.findtext("total_issues", "N/A"),
                "critical_issues": summary_node.findtext("critical_issues", "N/A"),
                "warning_issues": summary_node.findtext("warning_issues", "N/A"),
            }
            
        # Extract issues
        for issue_node in root.findall(".//issue"):
            issue = {
                "severity": issue_node.get("severity"),
                "message": issue_node.findtext("message"),
                "location": issue_node.findtext("location"),
                "suggestion": issue_node.findtext("suggestion")
            }
            report["issues"].append(issue)
            
        # Extract recommendations
        for rec_node in root.findall(".//recommendation"):
            report["recommendations"].append(rec_node.text)
            
    except ET.ParseError as e:
        logger.error(f"Failed to parse XML for report generation: {e}", exc_info=True)
        # Handle XML parsing errors
        pass
        
    # Generate PDF report
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    
    pdf.cell(200, 10, txt="Dissertation Review Report", ln=True, align="C")
    
    # TODO: Add more details to the PDF report
    
    report["pdf_content"] = pdf.output(dest='S').encode('latin-1')
    
    return report

if __name__ == "__main__":
    # This module is intended to be imported, not run directly.
    pass