Initial commit
This commit is contained in:
13
skills/toolkit/discover/formatters/__init__.py
Normal file
13
skills/toolkit/discover/formatters/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
"""
|
||||
Output Formatters Module
|
||||
"""
|
||||
|
||||
from .base import ToolFormatter
|
||||
from .table import TableFormatter
|
||||
from .json import JsonFormatter
|
||||
|
||||
__all__ = [
|
||||
'ToolFormatter',
|
||||
'TableFormatter',
|
||||
'JsonFormatter'
|
||||
]
|
||||
37
skills/toolkit/discover/formatters/base.py
Normal file
37
skills/toolkit/discover/formatters/base.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
Abstract base class for tool formatters
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List
|
||||
from ..models import Tool
|
||||
|
||||
|
||||
class ToolFormatter(ABC):
|
||||
"""抽象基类:工具输出格式化器"""
|
||||
|
||||
@abstractmethod
|
||||
def format(self, tools: List[Tool]) -> str:
|
||||
"""
|
||||
格式化工具列表
|
||||
|
||||
Args:
|
||||
tools: 工具列表
|
||||
|
||||
Returns:
|
||||
str: 格式化后的字符串
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def format_single(self, tool: Tool) -> str:
|
||||
"""
|
||||
格式化单个工具
|
||||
|
||||
Args:
|
||||
tool: 工具对象
|
||||
|
||||
Returns:
|
||||
str: 格式化后的字符串
|
||||
"""
|
||||
pass
|
||||
67
skills/toolkit/discover/formatters/json.py
Normal file
67
skills/toolkit/discover/formatters/json.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
JSON Formatter - JSON格式输出
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import List
|
||||
from .base import ToolFormatter
|
||||
from ..models import Tool, InternalTool, ExternalTool
|
||||
|
||||
|
||||
class JsonFormatter(ToolFormatter):
|
||||
"""JSON格式化器"""
|
||||
|
||||
def format(self, tools: List[Tool]) -> str:
|
||||
"""格式化工具列表为JSON"""
|
||||
result = []
|
||||
for tool in tools:
|
||||
result.append(self._tool_to_dict(tool))
|
||||
return json.dumps(result, indent=2, ensure_ascii=False)
|
||||
|
||||
def format_single(self, tool: Tool) -> str:
|
||||
"""格式化单个工具为JSON"""
|
||||
return json.dumps(self._tool_to_dict(tool), indent=2, ensure_ascii=False)
|
||||
|
||||
def _tool_to_dict(self, tool: Tool) -> dict:
|
||||
"""将工具对象转换为字典"""
|
||||
# 基础信息
|
||||
data = {
|
||||
"tool_id": tool.tool_id,
|
||||
"tool_name": tool.tool_name,
|
||||
"description": tool.description
|
||||
}
|
||||
|
||||
# 内部工具特有信息
|
||||
if isinstance(tool, InternalTool):
|
||||
data.update({
|
||||
"type": "internal",
|
||||
"language": tool.language,
|
||||
"file": tool.file,
|
||||
"complexity": tool.complexity,
|
||||
"meta_file": tool.meta_file,
|
||||
"tool_file": tool.tool_file,
|
||||
"purpose": tool.metadata.purpose,
|
||||
"usage": tool.usage
|
||||
})
|
||||
|
||||
if tool.metadata.satisfaction > 0:
|
||||
data["satisfaction"] = tool.metadata.satisfaction
|
||||
|
||||
# 外部工具特有信息
|
||||
elif isinstance(tool, ExternalTool):
|
||||
data.update({
|
||||
"type": "external",
|
||||
"command": tool.command,
|
||||
"category": tool.category,
|
||||
"use_cases": tool.use_cases,
|
||||
"install_guide": tool.install_guide,
|
||||
"installed": tool.installed
|
||||
})
|
||||
|
||||
if tool.path:
|
||||
data["path"] = tool.path
|
||||
|
||||
if tool.metadata.version != "1.0.0":
|
||||
data["version"] = tool.metadata.version
|
||||
|
||||
return data
|
||||
178
skills/toolkit/discover/formatters/table.py
Normal file
178
skills/toolkit/discover/formatters/table.py
Normal file
@@ -0,0 +1,178 @@
|
||||
"""
|
||||
Table Formatter - 表格格式输出
|
||||
"""
|
||||
|
||||
from typing import List, Any
|
||||
from .base import ToolFormatter
|
||||
from ..models import Tool, InternalTool, ExternalTool
|
||||
|
||||
|
||||
class TableFormatter(ToolFormatter):
|
||||
"""表格格式化器"""
|
||||
|
||||
def format(self, tools: List[Tool]) -> str:
|
||||
"""格式化工具列表为表格"""
|
||||
if not tools:
|
||||
return "⚠️ 未找到匹配的工具\n"
|
||||
|
||||
# 分离内部工具和外部工具
|
||||
internal = [t for t in tools if isinstance(t, InternalTool)]
|
||||
external = [t for t in tools if isinstance(t, ExternalTool)]
|
||||
|
||||
output = []
|
||||
|
||||
# 内部工具
|
||||
if internal:
|
||||
output.append(self._format_internal_tools(internal))
|
||||
|
||||
# 外部工具
|
||||
if external:
|
||||
if internal:
|
||||
output.append("")
|
||||
output.append(self._format_external_tools(external))
|
||||
|
||||
return "\n".join(output) + "\n"
|
||||
|
||||
def _format_internal_tools(self, tools: List[InternalTool]) -> str:
|
||||
"""格式化内部工具"""
|
||||
lines = [
|
||||
f"📦 找到 {len(tools)} 个内部工具:",
|
||||
"=" * 110,
|
||||
f"{'名称':<25} {'ID':<25} {'语言':<8} {'用途':<15} {'描述':<30}",
|
||||
"-" * 110
|
||||
]
|
||||
|
||||
for tool in tools:
|
||||
purposes = ",".join(tool.metadata.purpose)[:13]
|
||||
desc = tool.description[:28]
|
||||
lines.append(
|
||||
f"{tool.tool_name:<25} {tool.tool_id:<25} {tool.language:<8} {purposes:<15} {desc:<30}"
|
||||
)
|
||||
|
||||
lines.append("=" * 110)
|
||||
return "\n".join(lines)
|
||||
|
||||
def _format_external_tools(self, tools: List[ExternalTool]) -> str:
|
||||
"""格式化外部工具"""
|
||||
lines = [
|
||||
f"🌟 找到 {len(tools)} 个外部工具:",
|
||||
"=" * 100,
|
||||
f"{'名称':<25} {'ID':<20} {'分类':<12} {'安装状态':<10} {'描述':<30}",
|
||||
"-" * 100
|
||||
]
|
||||
|
||||
for tool in tools:
|
||||
status = tool.status
|
||||
desc = tool.description[:30]
|
||||
lines.append(
|
||||
f"{tool.tool_name:<25} {tool.tool_id:<20} {tool.category:<12} {status:<10} {desc:<30}"
|
||||
)
|
||||
|
||||
lines.append("=" * 100)
|
||||
lines.extend([
|
||||
"",
|
||||
"💡 提示: 使用 --external 仅显示外部工具",
|
||||
"💡 提示: 外部工具是系统级的CLI工具,需单独安装"
|
||||
])
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def format_single(self, tool: Tool) -> str:
|
||||
"""格式化单个工具详情"""
|
||||
if isinstance(tool, InternalTool):
|
||||
return self._format_internal_tool(tool)
|
||||
elif isinstance(tool, ExternalTool):
|
||||
return self._format_external_tool(tool)
|
||||
else:
|
||||
return self._format_generic_tool(tool)
|
||||
|
||||
def _format_internal_tool(self, tool: InternalTool) -> str:
|
||||
"""格式化内部工具详情"""
|
||||
lines = [
|
||||
"",
|
||||
"=" * 70,
|
||||
f"📦 {tool.tool_name}",
|
||||
"=" * 70,
|
||||
f"ID: {tool.tool_id}",
|
||||
f"语言: {tool.language}",
|
||||
f"文件: {tool.file}",
|
||||
f"复杂度: {tool.complexity}",
|
||||
f"用途: {', '.join(tool.metadata.purpose)}",
|
||||
"",
|
||||
"📋 描述:",
|
||||
f" {tool.description}",
|
||||
""
|
||||
]
|
||||
|
||||
if tool.usage:
|
||||
lines.append("🚀 使用方法:")
|
||||
if '命令' in tool.usage:
|
||||
lines.append(f" 命令: {tool.usage['命令']}")
|
||||
if '参数' in tool.usage:
|
||||
lines.append(" 参数:")
|
||||
for param, desc in tool.usage['参数'].items():
|
||||
lines.append(f" - {param}: {desc}")
|
||||
if '示例' in tool.usage:
|
||||
lines.append(" 示例:")
|
||||
for example in tool.usage.get('示例', [])[:3]:
|
||||
lines.append(f" • {example}")
|
||||
lines.append("")
|
||||
|
||||
if tool.metadata.satisfaction > 0:
|
||||
lines.extend([
|
||||
"📊 使用统计:",
|
||||
f" 满意度: {tool.metadata.satisfaction}/1.0",
|
||||
""
|
||||
])
|
||||
|
||||
lines.extend([
|
||||
"📂 文件位置:",
|
||||
f" 元数据: {tool.meta_file}",
|
||||
f"{'=' * 70}",
|
||||
""
|
||||
])
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _format_external_tool(self, tool: ExternalTool) -> str:
|
||||
"""格式化外部工具详情"""
|
||||
lines = [
|
||||
"",
|
||||
"=" * 70,
|
||||
f"🌟 {tool.tool_name}",
|
||||
"=" * 70,
|
||||
f"ID: {tool.tool_id}",
|
||||
f"分类: {tool.category}",
|
||||
f"命令: {tool.command}",
|
||||
f"状态: {tool.status}",
|
||||
"",
|
||||
"📋 描述:",
|
||||
f" {tool.description}",
|
||||
"",
|
||||
"💡 使用场景:",
|
||||
]
|
||||
|
||||
for use_case in tool.use_cases:
|
||||
lines.append(f" • {use_case}")
|
||||
|
||||
lines.extend([
|
||||
"",
|
||||
"📥 安装指南:",
|
||||
f" {tool.install_guide}",
|
||||
""
|
||||
])
|
||||
|
||||
if tool.path:
|
||||
lines.extend([
|
||||
"📂 安装路径:",
|
||||
f" {tool.path}",
|
||||
""
|
||||
])
|
||||
|
||||
lines.append(f"{'=' * 70}\n")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _format_generic_tool(self, tool: Tool) -> str:
|
||||
"""格式化通用工具详情"""
|
||||
return f"\n{'=' * 70}\n📦 {tool.tool_name} ({tool.tool_id})\n{'=' * 70}\n 描述: {tool.description}\n{'=' * 70}\n\n"
|
||||
Reference in New Issue
Block a user