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