aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/conventionalrp/core/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/conventionalrp/core/parser.py')
-rw-r--r--src/conventionalrp/core/parser.py49
1 files changed, 44 insertions, 5 deletions
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