diff options
| author | 2025-10-24 23:15:35 +0800 | |
|---|---|---|
| committer | 2025-10-24 23:15:35 +0800 | |
| commit | 08299b37dfda86e56e4f2b442f68ccd2da7a82e3 (patch) | |
| tree | e155d11412a26f692d08b8eb796fa689fc5a4019 /src/conventionalrp/extractors/rule_extractor.py | |
| parent | 990048eb2163127615de60d9359c150bdfb99536 (diff) | |
| download | conventional_role_play-08299b37dfda86e56e4f2b442f68ccd2da7a82e3.tar.gz conventional_role_play-08299b37dfda86e56e4f2b442f68ccd2da7a82e3.zip | |
feat: Enhance Processor, RuleExtractor, and Renderers with type hints and improved documentation
- Added type hints to Processor methods for better clarity and type safety.
- Improved documentation for Processor methods, including detailed descriptions of parameters and return types.
- Refactored RuleExtractor to support optional configuration file loading and added error handling for file operations.
- Enhanced MarkdownRenderer to handle both list and dictionary inputs, with improved rendering logic.
- Created comprehensive examples and tests for all components, ensuring robust functionality and error handling.
- Added example rules for D&D 5E and structured output files for various formats (JSON, HTML, Markdown).
- Established a testing framework with clear instructions and coverage reporting.
Diffstat (limited to 'src/conventionalrp/extractors/rule_extractor.py')
| -rw-r--r-- | src/conventionalrp/extractors/rule_extractor.py | 75 |
1 files changed, 63 insertions, 12 deletions
diff --git a/src/conventionalrp/extractors/rule_extractor.py b/src/conventionalrp/extractors/rule_extractor.py index b0d03d5..bfc60c8 100644 --- a/src/conventionalrp/extractors/rule_extractor.py +++ b/src/conventionalrp/extractors/rule_extractor.py @@ -1,3 +1,8 @@ +import json5 +from pathlib import Path +from typing import Dict, Any, Optional + + class BaseExtractor: def extract(self): raise NotImplementedError("This method should be overridden by subclasses.") @@ -7,19 +12,65 @@ class BaseExtractor: class RuleExtractor(BaseExtractor): - def __init__(self, config_file): + """规则提取器,用于从配置文件加载解析规则""" + + def __init__(self, config_file: Optional[str] = None): + """ + 初始化规则提取器 + + Args: + config_file: 规则配置文件路径(可选) + """ self.config_file = config_file - self.rules = self.load_rules_from_file() + self.rules: Dict[str, Any] = {} + if config_file: + self.rules = self.load_rules_from_file(config_file) - def load_rules_from_file(self): - import json + def load_rules_from_file(self, config_file: str) -> Dict[str, Any]: + """ + 从文件加载规则 + + Args: + config_file: 规则配置文件路径 + + Returns: + 解析后的规则字典 + + Raises: + FileNotFoundError: 文件不存在 + ValueError: 文件内容为空或格式错误 + """ + if not Path(config_file).exists(): + raise FileNotFoundError(f"Rule file not found: {config_file}") + + with open(config_file, "r", encoding="utf-8") as file: + content = file.read() + + rules = json5.loads(content) + + if not rules: + raise ValueError("Rule file cannot be empty") + + return rules - with open(self.config_file, "r") as file: - return json.load(file) + def load_rules(self, config_file: str) -> Dict[str, Any]: + """ + 加载规则(兼容旧接口) + + Args: + config_file: 规则配置文件路径 + + Returns: + 解析后的规则字典 + """ + self.rules = self.load_rules_from_file(config_file) + return self.rules - def extract(self): - # Implement rule extraction logic here - extracted_rules = [] - for rule in self.rules: - extracted_rules.append(rule) # Placeholder for actual extraction logic - return extracted_rules + def extract(self) -> Dict[str, Any]: + """ + 提取规则 + + Returns: + 规则字典 + """ + return self.rules |
