From d7799f1ff7fca7525fd09c2e51f366be1d0886b5 Mon Sep 17 00:00:00 2001 From: pine Date: Thu, 13 Mar 2025 22:59:24 +0800 Subject: feat: simple parser rules load and log processing --- src/conventionalrp/core/parser.py | 49 +++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/conventionalrp/core/parser.py b/src/conventionalrp/core/parser.py index 32b1b9f..73f349f 100644 --- a/src/conventionalrp/core/parser.py +++ b/src/conventionalrp/core/parser.py @@ -1,15 +1,54 @@ +import json +import re +from pathlib import Path + class Parser: def __init__(self): self.rules = [] - def load_rules(self, rules): + def load_rules(self, rules_path : str): """Load parsing rules.""" + if not Path(rules_path).exists(): + raise FileNotFoundError(f"No such file or directory: {rules_path} ") + + with open(rules_path, "r", encoding="utf-8") as f: + rules = json.load(f) + + # validation rule format + if rules is None: + raise ValueError(f"Rule file cannot be empty.") + # to be continue... + self.rules = rules - def parse_log(self, log): + def parse_log(self, log_path: str): """Parse the TRPG log based on loaded rules.""" parsed_data = [] - for rule in self.rules: - # Implement rule-based parsing logic here - pass + + if not Path(log_path).exists(): + raise FileNotFoundError(f"No such file or directory: {log_path} ") + + with open(log_path, "r", encoding="utf-8") as f: + log_content = f.read().splitlines() + + # Iterate each line of the log + for line in log_content: + # pass blank line + if not line.strip(): + continue + + # try to match the current line by rules + for rule in self.rules: + pattern = rule.get("pattern") + rule_type = rule.get("type") + match = re.search(pattern, line) + if match: + # matched + content = match.group(1).strip() + parsed_data.append({"content": content, "type": rule_type}) + break + # no matched, marked as an unknown type + else: + parsed_data.append({"content": line.strip(), "type": "unknown"}) + return parsed_data \ No newline at end of file -- cgit v1.2.3-70-g09d2