-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatenate_rules.py
64 lines (54 loc) · 1.83 KB
/
concatenate_rules.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import os
import glob
import sys
from typing import List, Dict
def load_rule_file(file_path: str) -> Dict:
"""Loads a single rule file and checks for valid JSON."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError as e:
print(f"\nERROR: Invalid JSON in file {file_path}")
print(f"Details: {str(e)}")
sys.exit(1)
except Exception as e:
print(f"\nERROR: Error reading file {file_path}")
print(f"Details: {str(e)}")
sys.exit(1)
def concatenate_rules():
# Directory with rules
rules_dir = "rules"
# Output file
output_rules = "Rules.json"
# Check if rules directory exists
if not os.path.exists(rules_dir):
print(f"\nERROR: Directory '{rules_dir}' not found!")
sys.exit(1)
# Collect all rules
all_rules: List[Dict] = []
rule_files = glob.glob(os.path.join(rules_dir, "*.json"))
if not rule_files:
print(f"\nWARNING: No JSON files found in '{rules_dir}'!")
sys.exit(1)
for rule_file in rule_files:
rule = load_rule_file(rule_file)
all_rules.append(rule)
# Sort rules by ruleId
try:
all_rules.sort(key=lambda x: x['ruleId'])
except KeyError:
print("\nERROR: At least one rule has no 'ruleId'!")
sys.exit(1)
# Write rules to output file
try:
with open(output_rules, 'w', encoding='utf-8') as f:
json.dump(all_rules, f, indent=2, ensure_ascii=False)
except Exception as e:
print(f"\nERROR: Error writing output file {output_rules}")
print(f"Details: {str(e)}")
sys.exit(1)
print(f"\nSuccessfully completed:")
print(f"- Rules merged into {output_rules}")
if __name__ == "__main__":
concatenate_rules()