Skip to content

Schemas

azuma.schemas.Rule

Bases: YamlBaseModel

match(event)

Check whether an event is matched with the rule or not

Parameters:

Name Type Description Default
event dict[Any, Any]

Event

required

Returns:

Name Type Description
bool bool

Returns True if an event is matched with the rule. False if not

Source code in azuma/schemas/rule.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def match(self, event: dict[Any, Any]) -> bool:
    """Check whether an event is matched with the rule or not

    Args:
        event (dict[Any, Any]): Event

    Returns:
        bool: Returns True if an event is matched with the rule. False if not
    """
    if not isinstance(event, dict):
        raise ValueError("event should be a dict")

    return self.detection.condition(self, event)

azuma.schemas.RuleSet

Bases: RootModel

from_dir(dir, *, pattern='*.{yml,yaml}') classmethod

Load rules from a directory

Parameters:

Name Type Description Default
dir str | Path

Directory

required
pattern str

YAML file pattern. Defaults to "*.{yml,yaml}".

'*.{yml,yaml}'

Returns:

Name Type Description
RuleSet RuleSet

Rule set

Source code in azuma/schemas/rule_set.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@classmethod
def from_dir(cls, dir: str | Path, *, pattern="*.{yml,yaml}") -> "RuleSet":
    """Load rules from a directory

    Args:
        dir (str | Path): Directory
        pattern (str, optional): YAML file pattern. Defaults to "*.{yml,yaml}".

    Returns:
        RuleSet: Rule set
    """
    dir = Path(dir) if isinstance(dir, str) else dir
    expanded = senkawa.glob(str(dir.joinpath(pattern)))
    return cls(root=[Rule.model_validate_file(p) for p in expanded])

match_all(event)

Check whether an event is matched with the rules

Parameters:

Name Type Description Default
event dict[Any, Any]

Event

required

Returns:

Type Description
list[Rule]

list[Rule]: A list of matched rules

Source code in azuma/schemas/rule_set.py
19
20
21
22
23
24
25
26
27
28
def match_all(self, event: dict[Any, Any]) -> list[Rule]:
    """Check whether an event is matched with the rules

    Args:
        event (dict[Any, Any]): Event

    Returns:
        list[Rule]: A list of matched rules
    """
    return [rule for rule in self if rule.match(event)]

unique()

Returns unique rule set.

Returns:

Name Type Description
RuleSet RuleSet

Rule set

Source code in azuma/schemas/rule_set.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def unique(self) -> "RuleSet":
    """Returns unique rule set.

    Returns:
        RuleSet: Rule set
    """
    seen: set[str] = set()

    filtered: list[Rule] = []
    for rule in self.root:
        if rule.id is None:
            filtered.append(rule)
            continue

        if rule.id not in seen:
            seen.add(rule.id)
            filtered.append(rule)

    return RuleSet(root=filtered)

azuma.schemas.yaml_model.YamlBaseModel

Bases: BaseModel

BaseModel with YAML support

model_validate_file(path, *, encoding='utf8', strict=None, context=None) classmethod

Parse a YAML file.

Parameters:

Name Type Description Default
path str | Path

Path to a file.

required
encoding str

Encoding. Defaults to "utf8".

'utf8'
strict bool | None

Strict or not. Defaults to None.

None
context dict[str, Any] | None

Context. Defaults to None.

None

Returns:

Name Type Description
YamlBaseModel

Parsed instance.

Source code in azuma/schemas/yaml_model.py
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
@classmethod
def model_validate_file(  # type: ignore
    cls,
    path: str | Path,
    *,
    encoding: str = "utf8",
    strict: bool | None = None,
    context: dict[str, Any] | None = None,
):
    """Parse a YAML file.

    Args:
        path (str | Path): Path to a file.
        encoding (str, optional): Encoding. Defaults to "utf8".
        strict (bool | None, optional): Strict or not. Defaults to None.
        context (dict[str, Any] | None, optional): Context. Defaults to None.

    Returns:
        YamlBaseModel: Parsed instance.
    """
    with open(path, encoding=encoding) as f:
        text = f.read()

    obj = yaml.safe_load(text)
    return cls.model_validate(obj, strict=strict, context=context)

model_validate_yaml(b, *, strict=None, context=None) classmethod

Parse a YAML text.

Parameters:

Name Type Description Default
b str | bytes

String or bytes.

required
strict bool | None

Strict or not. Defaults to None.

None
context dict[str, Any] | None

Context. Defaults to None.

None

Returns:

Name Type Description
YamlBaseModel

Parsed instance.

Source code in azuma/schemas/yaml_model.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@classmethod
def model_validate_yaml(  # type: ignore
    cls,
    b: str | bytes,
    *,
    strict: bool | None = None,
    context: dict[str, Any] | None = None,
):
    """Parse a YAML text.

    Args:
        b (str | bytes): String or bytes.
        strict (bool | None, optional): Strict or not. Defaults to None.
        context (dict[str, Any] | None, optional): Context. Defaults to None.

    Returns:
        YamlBaseModel: Parsed instance.
    """
    obj = yaml.safe_load(b)
    return cls.model_validate(obj, strict=strict, context=context)