blob: 12b9e4b076487f384e523ed953dca6e88c09569b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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.")
def load_rules(self, rules):
raise NotImplementedError("This method should be overridden by subclasses.")
class RuleExtractor(BaseExtractor):
"""规则提取器,用于从配置文件加载解析规则"""
def __init__(self, config_file: Optional[str] = None):
self.config_file = config_file
self.rules: Dict[str, Any] = {}
if config_file:
self.rules = self.load_rules_from_file(config_file)
def load_rules_from_file(self, config_file: str) -> Dict[str, Any]:
"""
从文件加载规则
"""
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
def load_rules(self, config_file: str) -> Dict[str, Any]:
"""
加载规则(兼容旧接口)
"""
self.rules = self.load_rules_from_file(config_file)
return self.rules
def extract(self) -> Dict[str, Any]:
"""
提取规则
"""
return self.rules
|