-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add back MSG parsing * Fix init * Fix msg parsing
- Loading branch information
Showing
8 changed files
with
74 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,65 @@ | ||
# # type: ignore | ||
# from typing import AsyncGenerator | ||
# type: ignore | ||
import os | ||
import tempfile | ||
from typing import AsyncGenerator | ||
|
||
# import extract_msg | ||
from msg_parser import MsOxMessage | ||
|
||
# from core.base.parsers.base_parser import AsyncParser | ||
# from core.base.providers import ( | ||
# CompletionProvider, | ||
# DatabaseProvider, | ||
# IngestionConfig, | ||
# ) | ||
from core.base.parsers.base_parser import AsyncParser | ||
from core.base.providers import ( | ||
CompletionProvider, | ||
DatabaseProvider, | ||
IngestionConfig, | ||
) | ||
|
||
# class MSGParser(AsyncParser[str | bytes]): | ||
# """Parser for MSG (Outlook Message) files.""" | ||
|
||
# def __init__( | ||
# self, | ||
# config: IngestionConfig, | ||
# database_provider: DatabaseProvider, | ||
# llm_provider: CompletionProvider, | ||
# ): | ||
# self.database_provider = database_provider | ||
# self.llm_provider = llm_provider | ||
# self.config = config | ||
# self.extract_msg = extract_msg | ||
class MSGParser(AsyncParser[str | bytes]): | ||
"""Parser for MSG (Outlook Message) files using msg_parser.""" | ||
|
||
# async def ingest( | ||
# self, data: str | bytes, **kwargs | ||
# ) -> AsyncGenerator[str, None]: | ||
# """Ingest MSG data and yield email content.""" | ||
# if isinstance(data, str): | ||
# raise ValueError("MSG data must be in bytes format.") | ||
def __init__( | ||
self, | ||
config: IngestionConfig, | ||
database_provider: DatabaseProvider, | ||
llm_provider: CompletionProvider, | ||
): | ||
self.database_provider = database_provider | ||
self.llm_provider = llm_provider | ||
self.config = config | ||
|
||
# from io import BytesIO | ||
async def ingest( | ||
self, data: str | bytes, **kwargs | ||
) -> AsyncGenerator[str, None]: | ||
"""Ingest MSG data and yield email content.""" | ||
if isinstance(data, str): | ||
raise ValueError("MSG data must be in bytes format.") | ||
|
||
# file_obj = BytesIO(data) | ||
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".msg") | ||
try: | ||
tmp_file.write(data) | ||
tmp_file.close() | ||
|
||
# try: | ||
# msg = self.extract_msg.Message(file_obj) | ||
msg = MsOxMessage(tmp_file.name) | ||
|
||
# # Extract metadata | ||
# metadata = [] | ||
# if msg.subject: | ||
# metadata.append(f"Subject: {msg.subject}") | ||
# if msg.sender: | ||
# metadata.append(f"From: {msg.sender}") | ||
# if msg.to: | ||
# metadata.append(f"To: {msg.to}") | ||
# if msg.date: | ||
# metadata.append(f"Date: {msg.date}") | ||
metadata = [] | ||
|
||
# if metadata: | ||
# yield "\n".join(metadata) | ||
if msg.subject: | ||
metadata.append(f"Subject: {msg.subject}") | ||
if msg.sender: | ||
metadata.append(f"From: {msg.sender}") | ||
if msg.to: | ||
metadata.append(f"To: {', '.join(msg.to)}") | ||
if msg.sent_date: | ||
metadata.append(f"Date: {msg.sent_date}") | ||
if metadata: | ||
yield "\n".join(metadata) | ||
if msg.body: | ||
yield msg.body.strip() | ||
|
||
# # Extract body | ||
# if msg.body: | ||
# yield msg.body.strip() | ||
for attachment in msg.attachments: | ||
if attachment.Filename: | ||
yield f"\nAttachment: {attachment.Filename}" | ||
|
||
# # Extract attachments (optional) | ||
# for attachment in msg.attachments: | ||
# if hasattr(attachment, "name"): | ||
# yield f"\nAttachment: {attachment.name}" | ||
|
||
# except Exception as e: | ||
# raise ValueError(f"Error processing MSG file: {str(e)}") | ||
# finally: | ||
# file_obj.close() | ||
except Exception as e: | ||
raise ValueError(f"Error processing MSG file: {str(e)}") from e | ||
finally: | ||
os.remove(tmp_file.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.