stats.pyโข4.84 kB
"""Statistics tools for POEditor MCP server."""
from typing import Any, Dict, List
from mcp.types import Tool, TextContent, INVALID_PARAMS
from mcp import McpError
from ..poeditor_client import POEditorClient, POEditorError
def get_stats_tools() -> List[Tool]:
"""Get all statistics-related tools."""
return [
Tool(
name="get_project_stats",
description="Get project statistics",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
}
},
"required": ["project_id"]
}
),
Tool(
name="get_language_stats",
description="Get language statistics",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
}
},
"required": ["project_id", "language_code"]
}
)
]
async def handle_get_project_stats(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle get_project_stats tool call."""
project_id = arguments.get("project_id")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
try:
stats = await client.get_project_stats(project_id)
output = f"**Project Statistics for {stats.get('name', 'Unknown Project')}:**\n\n"
output += f"- **ID:** {stats.get('id', 'Unknown')}\n"
output += f"- **Total Terms:** {stats.get('terms', 0)}\n"
output += f"- **Created:** {stats.get('created', 'Unknown')}\n"
output += f"- **Open:** {stats.get('open', 'Unknown')}\n"
output += f"- **Public:** {stats.get('public', 'Unknown')}\n"
output += f"- **Reference Language:** {stats.get('reference_language', 'None')}\n"
# Show language statistics if available
if 'languages' in stats:
output += f"\n**Languages ({len(stats['languages'])}):**\n"
for lang in stats['languages']:
name = lang.get('name', 'Unknown')
code = lang.get('code', 'unknown')
percentage = lang.get('percentage', 0)
output += f"- {name} ({code}): {percentage}% complete\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_get_language_stats(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle get_language_stats tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
try:
stats = await client.get_language_stats(project_id, language_code)
if not stats:
return [TextContent(
type="text",
text=f"Language {language_code} not found in project {project_id}."
)]
output = f"**Language Statistics for {stats.get('name', language_code)}:**\n\n"
output += f"- **Code:** {stats.get('code', language_code)}\n"
output += f"- **Name:** {stats.get('name', 'Unknown')}\n"
output += f"- **Percentage Complete:** {stats.get('percentage', 0)}%\n"
output += f"- **Translated Terms:** {stats.get('translated', 0)}\n"
output += f"- **Total Terms:** {stats.get('terms', 0)}\n"
# Calculate remaining terms
total_terms = stats.get('terms', 0)
translated_terms = stats.get('translated', 0)
remaining_terms = total_terms - translated_terms
output += f"- **Remaining Terms:** {remaining_terms}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
# Tool handler mapping
STATS_HANDLERS = {
"get_project_stats": handle_get_project_stats,
"get_language_stats": handle_get_language_stats,
}