-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.py
More file actions
34 lines (24 loc) · 967 Bytes
/
rules.py
File metadata and controls
34 lines (24 loc) · 967 Bytes
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
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any
@dataclass
class RuleMatchResult:
rule: dict[str, Any]
class RuleEngine:
def __init__(self, rules: list[dict[str, Any]]) -> None:
self.rules = rules
def match(self, file_path: Path) -> RuleMatchResult | None:
name_lower = file_path.name.lower()
suffix_lower = file_path.suffix.lower()
for rule in self.rules:
if not rule.get("enabled", True):
continue
extensions = [ext.lower() for ext in rule.get("extensions", [])]
if extensions and suffix_lower not in extensions:
continue
contains = [item.lower() for item in rule.get("contains", [])]
if contains and not any(fragment in name_lower for fragment in contains):
continue
return RuleMatchResult(rule=rule)
return None