77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
# @line_count 100
|
||
"""JSON解析模块,提取功能点和章节信息"""
|
||
import json
|
||
from pathlib import Path
|
||
from typing import List, Dict, Any, Optional
|
||
from .parser_adapters.parser_factory import ParserFactory
|
||
|
||
|
||
class JSONParser:
|
||
"""JSON文档解析器(门面模式)"""
|
||
|
||
def __init__(self, json_path: str):
|
||
"""
|
||
初始化JSON解析器
|
||
|
||
Args:
|
||
json_path: JSON文件路径
|
||
"""
|
||
self.json_path = Path(json_path)
|
||
self.data: Dict[str, Any] = {}
|
||
self.adapter = None
|
||
self.load_json()
|
||
|
||
def load_json(self):
|
||
"""加载JSON文件并创建适配器"""
|
||
if not self.json_path.exists():
|
||
raise FileNotFoundError(f"JSON文件不存在: {self.json_path}")
|
||
|
||
with open(self.json_path, 'r', encoding='utf-8') as f:
|
||
self.data = json.load(f)
|
||
|
||
# 自动检测格式并创建适配器
|
||
self.adapter = ParserFactory.create_adapter(self.data)
|
||
|
||
def extract_function_points(self) -> List[Dict[str, Any]]:
|
||
"""
|
||
提取功能点列表
|
||
|
||
Returns:
|
||
功能点列表,每个功能点包含:
|
||
- module_name: 所属模块
|
||
- function_name: 功能名称
|
||
- description: 功能描述
|
||
- operation_steps: 操作步骤(可选)
|
||
- requirement_id: 需求编号(可选)
|
||
- requirement_type: 需求类型(可选)
|
||
"""
|
||
return self.adapter.extract_function_points()
|
||
|
||
def get_document_info(self) -> Dict[str, Any]:
|
||
"""
|
||
获取文档基本信息
|
||
|
||
Returns:
|
||
文档信息字典
|
||
"""
|
||
return self.adapter.get_document_info()
|
||
|
||
def get_sections(self) -> List[Dict[str, Any]]:
|
||
"""
|
||
获取所有章节信息
|
||
|
||
Returns:
|
||
章节列表
|
||
"""
|
||
return self.adapter.get_sections()
|
||
|
||
def get_module_summary(self) -> List[Dict[str, Any]]:
|
||
"""
|
||
获取模块摘要
|
||
|
||
Returns:
|
||
模块摘要列表
|
||
"""
|
||
return self.adapter.get_module_summary()
|
||
|