MCPcopy Create free account
hub / github.com/HzaCode/OneCite / format

Method format

onecite/pipeline/formatter.py:87–144  ·  view source on GitHub ↗

Format completed records to the specified output format. Args: completed_entries: List of completed entries from :meth:`EnricherModule.enrich`. output_format: Output format. Only ``"bibtex"`` is supported; any other value raises ``Form

(self, completed_entries: List[CompletedEntry], 
               output_format: str)

Source from the content-addressed store, hash-verified

85 return result
86
87 def format(self, completed_entries: List[CompletedEntry],
88 output_format: str) -> Dict[str, Any]:
89 """Format completed records to the specified output format.
90
91 Args:
92 completed_entries: List of completed entries from
93 :meth:`EnricherModule.enrich`.
94 output_format: Output format. Only ``"bibtex"`` is
95 supported; any other value raises ``FormatError``.
96
97 Returns:
98 A dictionary with two keys:
99
100 * ``results`` — a list of formatted citation strings.
101 * ``report`` — a dict with ``total``, ``succeeded``, and
102 ``failed_entries``.
103 """
104 self.logger.info(f"Starting to format {len(completed_entries)} entries to {output_format} format")
105
106 formatted_strings = []
107 failed_entries = []
108
109 for entry in completed_entries:
110 if entry['status'] == 'completed':
111 try:
112 if output_format.lower() == 'bibtex':
113 formatted_string = self._format_bibtex(entry)
114 else:
115 raise FormatError(f"Unsupported output format: {output_format!r}. Only 'bibtex' is supported.")
116
117 formatted_strings.append(formatted_string)
118
119 except Exception as e:
120 self.logger.error(f"Formatting entry {entry['id']} failed: {str(e)}")
121 failed_entries.append({
122 'id': entry['id'],
123 'error': str(e),
124 'doi': entry.get('doi', 'unknown')
125 })
126 else:
127 failed_entries.append({
128 'id': entry['id'],
129 'error': 'Entry processing failed',
130 'status': entry['status']
131 })
132
133 report = {
134 'total': len(completed_entries),
135 'succeeded': len(formatted_strings),
136 'failed_entries': failed_entries
137 }
138
139 self.logger.info(f"Formatting completed: {len(formatted_strings)}/{len(completed_entries)} entries successful")
140
141 return {
142 'results': formatted_strings,
143 'report': report
144 }

Calls 2

_format_bibtexMethod · 0.95
FormatErrorClass · 0.85