81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
|
|
"""
|
|
Document Parsing helper for JIRA operations.
|
|
Handles parsing of document references and JSON content.
|
|
"""
|
|
|
|
import logging
|
|
import json
|
|
from typing import Any, Optional, Dict
|
|
from modules.datamodels.datamodelDocref import DocumentReferenceList
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class DocumentParsingHelper:
|
|
"""Helper for document parsing operations"""
|
|
|
|
def __init__(self, methodInstance):
|
|
"""
|
|
Initialize document parsing helper.
|
|
|
|
Args:
|
|
methodInstance: Instance of MethodJira (for access to services)
|
|
"""
|
|
self.method = methodInstance
|
|
self.services = methodInstance.services
|
|
|
|
def getDocumentData(self, documentReference: str) -> Any:
|
|
"""
|
|
Get document data from a document reference.
|
|
|
|
Args:
|
|
documentReference: Document reference string
|
|
|
|
Returns:
|
|
Document data (bytes, str, or None)
|
|
"""
|
|
try:
|
|
docList = DocumentReferenceList.from_string_list([documentReference])
|
|
chatDocuments = self.services.chat.getChatDocumentsFromDocumentList(docList)
|
|
if not chatDocuments:
|
|
return None
|
|
|
|
doc = chatDocuments[0]
|
|
fileId = getattr(doc, 'fileId', None)
|
|
if not fileId:
|
|
return None
|
|
|
|
return self.services.chat.getFileData(fileId)
|
|
except Exception as e:
|
|
logger.error(f"Error getting document data: {str(e)}")
|
|
return None
|
|
|
|
def parseJsonFromDocument(self, documentReference: str) -> Optional[Dict[str, Any]]:
|
|
"""
|
|
Parse JSON content from a document reference.
|
|
|
|
Args:
|
|
documentReference: Document reference string
|
|
|
|
Returns:
|
|
Parsed JSON dictionary or None
|
|
"""
|
|
try:
|
|
fileData = self.getDocumentData(documentReference)
|
|
if not fileData:
|
|
return None
|
|
|
|
# Handle bytes
|
|
if isinstance(fileData, bytes):
|
|
jsonStr = fileData.decode('utf-8')
|
|
else:
|
|
jsonStr = str(fileData)
|
|
|
|
# Parse JSON
|
|
return json.loads(jsonStr)
|
|
except Exception as e:
|
|
logger.error(f"Error parsing JSON from document: {str(e)}")
|
|
return None
|
|
|