ran ruff format via uv
This commit is contained in:
@@ -81,7 +81,11 @@ class UnstructuredClient:
|
||||
|
||||
# Prepare the multipart form data
|
||||
files = {
|
||||
"files": (filename, io.BytesIO(content), content_type or "application/octet-stream")
|
||||
"files": (
|
||||
filename,
|
||||
io.BytesIO(content),
|
||||
content_type or "application/octet-stream",
|
||||
)
|
||||
}
|
||||
|
||||
# Prepare the request data
|
||||
@@ -132,7 +136,7 @@ class UnstructuredClient:
|
||||
"element_types": element_types,
|
||||
"strategy": strategy,
|
||||
"languages": languages,
|
||||
"parsing_method": "unstructured_api"
|
||||
"parsing_method": "unstructured_api",
|
||||
}
|
||||
|
||||
logger.debug(
|
||||
@@ -144,7 +148,9 @@ class UnstructuredClient:
|
||||
|
||||
except httpx.HTTPError as e:
|
||||
logger.error(f"HTTP error calling Unstructured API: {e}")
|
||||
raise Exception(f"Failed to parse document via Unstructured API: {str(e)}") from e
|
||||
raise Exception(
|
||||
f"Failed to parse document via Unstructured API: {str(e)}"
|
||||
) from e
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error parsing document: {e}")
|
||||
raise Exception(f"Failed to parse document: {str(e)}") from e
|
||||
|
||||
@@ -116,7 +116,9 @@ def get_unstructured_languages() -> list[str]:
|
||||
languages = [lang.strip() for lang in languages_str.split(",") if lang.strip()]
|
||||
|
||||
if not languages:
|
||||
logging.warning("No languages specified in UNSTRUCTURED_LANGUAGES. Using default: eng,deu")
|
||||
logging.warning(
|
||||
"No languages specified in UNSTRUCTURED_LANGUAGES. Using default: eng,deu"
|
||||
)
|
||||
return ["eng", "deu"]
|
||||
|
||||
return languages
|
||||
|
||||
@@ -3,7 +3,10 @@ import logging
|
||||
from mcp.server.fastmcp import Context, FastMCP
|
||||
|
||||
from nextcloud_mcp_server.client import NextcloudClient
|
||||
from nextcloud_mcp_server.utils.document_parser import is_parseable_document, parse_document
|
||||
from nextcloud_mcp_server.utils.document_parser import (
|
||||
is_parseable_document,
|
||||
parse_document,
|
||||
)
|
||||
from nextcloud_mcp_server.config import is_unstructured_parsing_enabled
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -62,7 +65,7 @@ def configure_webdav_tools(mcp: FastMCP):
|
||||
content, content_type = await client.webdav.read_file(path)
|
||||
|
||||
# Check if this is a parseable document (PDF, DOCX, etc.)
|
||||
if (is_unstructured_parsing_enabled() and is_parseable_document(content_type)):
|
||||
if is_unstructured_parsing_enabled() and is_parseable_document(content_type):
|
||||
try:
|
||||
logger.info(f"Parsing document '{path}' of type '{content_type}'")
|
||||
parsed_text, metadata = await parse_document(
|
||||
|
||||
@@ -35,6 +35,7 @@ PARSEABLE_MIME_TYPES = {
|
||||
"image/bmp": "image",
|
||||
}
|
||||
|
||||
|
||||
def is_parseable_document(content_type: Optional[str]) -> bool:
|
||||
"""Check if a document type can be parsed.
|
||||
|
||||
@@ -51,10 +52,9 @@ def is_parseable_document(content_type: Optional[str]) -> bool:
|
||||
base_content_type = content_type.split(";")[0].strip().lower()
|
||||
return base_content_type in PARSEABLE_MIME_TYPES
|
||||
|
||||
|
||||
async def parse_document(
|
||||
content: bytes,
|
||||
content_type: Optional[str],
|
||||
filename: Optional[str] = None
|
||||
content: bytes, content_type: Optional[str], filename: Optional[str] = None
|
||||
) -> Tuple[str, dict]:
|
||||
"""Parse a document using the Unstructured API.
|
||||
|
||||
@@ -75,7 +75,9 @@ async def parse_document(
|
||||
if not is_parseable_document(content_type):
|
||||
raise ValueError(f"Document type '{content_type}' is not supported for parsing")
|
||||
|
||||
base_content_type = content_type.split(";")[0].strip().lower() if content_type else ""
|
||||
base_content_type = (
|
||||
content_type.split(";")[0].strip().lower() if content_type else ""
|
||||
)
|
||||
doc_type = PARSEABLE_MIME_TYPES.get(base_content_type, "unknown")
|
||||
|
||||
logger.debug(f"Parsing document of type '{doc_type}' (MIME: {content_type})")
|
||||
@@ -84,7 +86,10 @@ async def parse_document(
|
||||
if is_unstructured_parsing_enabled():
|
||||
logger.debug("Using Unstructured API for parsing")
|
||||
try:
|
||||
from nextcloud_mcp_server.client.unstructured_client import UnstructuredClient
|
||||
from nextcloud_mcp_server.client.unstructured_client import (
|
||||
UnstructuredClient,
|
||||
)
|
||||
|
||||
client = UnstructuredClient()
|
||||
# The client will automatically use environment configuration
|
||||
# (UNSTRUCTURED_STRATEGY and UNSTRUCTURED_LANGUAGES)
|
||||
@@ -97,6 +102,7 @@ async def parse_document(
|
||||
logger.error(f"Unstructured API parsing failed: {e}")
|
||||
# If unstructured parsing fails, return base64 as fallback
|
||||
import base64
|
||||
|
||||
parsed_text = f"Document could not be parsed. Base64 content: {base64.b64encode(content).decode('ascii')[:200]}..."
|
||||
metadata = {
|
||||
"document_type": doc_type,
|
||||
@@ -104,18 +110,21 @@ async def parse_document(
|
||||
"element_count": 0,
|
||||
"text_length": len(parsed_text),
|
||||
"parsing_method": "fallback_base64",
|
||||
"error": str(e)
|
||||
"error": str(e),
|
||||
}
|
||||
return parsed_text, metadata
|
||||
else:
|
||||
logger.debug("Unstructured parsing is disabled, returning base64 encoded content as fallback")
|
||||
logger.debug(
|
||||
"Unstructured parsing is disabled, returning base64 encoded content as fallback"
|
||||
)
|
||||
import base64
|
||||
|
||||
parsed_text = f"Document could not be parsed. Base64 content: {base64.b64encode(content).decode('ascii')[:200]}..."
|
||||
metadata = {
|
||||
"document_type": doc_type,
|
||||
"mime_type": content_type,
|
||||
"element_count": 0,
|
||||
"text_length": len(parsed_text),
|
||||
"parsing_method": "fallback_base64"
|
||||
"parsing_method": "fallback_base64",
|
||||
}
|
||||
return parsed_text, metadata
|
||||
Reference in New Issue
Block a user