Add config module
This commit is contained in:
@@ -1,13 +1,9 @@
|
|||||||
"""Configuration loader module."""
|
"""Configuration loader."""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from ..models import Rule
|
from ..models import Rule
|
||||||
|
|
||||||
|
|
||||||
class ConfigLoader:
|
class ConfigLoader:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._config_paths = [
|
self._config_paths = [
|
||||||
@@ -16,12 +12,12 @@ class ConfigLoader:
|
|||||||
]
|
]
|
||||||
self._default_config_path = Path(__file__).parent.parent.parent / "config" / "default_rules.yaml"
|
self._default_config_path = Path(__file__).parent.parent.parent / "config" / "default_rules.yaml"
|
||||||
|
|
||||||
def load_default_rules(self) -> List[Rule]:
|
def load_default_rules(self):
|
||||||
if self._default_config_path.exists():
|
if self._default_config_path.exists():
|
||||||
return self.load_from_file(self._default_config_path)
|
return self.load_from_file(self._default_config_path)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def load_from_file(self, path: Path) -> List[Rule]:
|
def load_from_file(self, path):
|
||||||
rules = []
|
rules = []
|
||||||
try:
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
@@ -34,14 +30,14 @@ class ConfigLoader:
|
|||||||
raise ValueError(f"Invalid YAML syntax: {e}")
|
raise ValueError(f"Invalid YAML syntax: {e}")
|
||||||
return rules
|
return rules
|
||||||
|
|
||||||
def load_user_rules(self) -> List[Rule]:
|
def load_user_rules(self):
|
||||||
rules = []
|
rules = []
|
||||||
for config_path in self._config_paths:
|
for config_path in self._config_paths:
|
||||||
if config_path.exists():
|
if config_path.exists():
|
||||||
rules.extend(self.load_from_file(config_path))
|
rules.extend(self.load_from_file(config_path))
|
||||||
return rules
|
return rules
|
||||||
|
|
||||||
def load_merged_rules(self, custom_rules: Optional[List[Rule]] = None) -> List[Rule]:
|
def load_merged_rules(self, custom_rules=None):
|
||||||
rules = self.load_default_rules()
|
rules = self.load_default_rules()
|
||||||
user_rules = self.load_user_rules()
|
user_rules = self.load_user_rules()
|
||||||
rule_dict = {r.id: r for r in rules}
|
rule_dict = {r.id: r for r in rules}
|
||||||
@@ -49,8 +45,7 @@ class ConfigLoader:
|
|||||||
rule_dict[r.id] = r
|
rule_dict[r.id] = r
|
||||||
return list(rule_dict.values())
|
return list(rule_dict.values())
|
||||||
|
|
||||||
|
def load_rules(config_path=None):
|
||||||
def load_rules(config_path: Optional[str] = None) -> List[Rule]:
|
|
||||||
loader = ConfigLoader()
|
loader = ConfigLoader()
|
||||||
if config_path:
|
if config_path:
|
||||||
return loader.load_from_file(Path(config_path))
|
return loader.load_from_file(Path(config_path))
|
||||||
|
|||||||
Reference in New Issue
Block a user