26 lines
857 B
Python
26 lines
857 B
Python
from typing import Any, Dict, List
|
|
|
|
from modules.datamodels.datamodelExtraction import ContentPart
|
|
from ..subUtils import makeId
|
|
from ..subRegistry import Extractor
|
|
|
|
|
|
class CsvExtractor(Extractor):
|
|
def detect(self, fileName: str, mimeType: str, headBytes: bytes) -> bool:
|
|
return mimeType == "text/csv" or (fileName or "").lower().endswith(".csv")
|
|
|
|
def extract(self, fileBytes: bytes, context: Dict[str, Any]) -> List[ContentPart]:
|
|
fileName = context.get("fileName")
|
|
mimeType = context.get("mimeType") or "text/csv"
|
|
data = fileBytes.decode("utf-8", errors="replace")
|
|
return [ContentPart(
|
|
id=makeId(),
|
|
parentId=None,
|
|
label="main",
|
|
typeGroup="table",
|
|
mimeType=mimeType,
|
|
data=data,
|
|
metadata={"size": len(fileBytes)}
|
|
)]
|
|
|
|
|