Skip to content

Commit

Permalink
Merge pull request #187 from Res260/fixtypes
Browse files Browse the repository at this point in the history
Fix typos and type hints in SigmaCollection
  • Loading branch information
thomaspatzke authored Jan 13, 2024
2 parents a8d5447 + ea01041 commit 0ad327e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 14 additions & 9 deletions sigma/collection.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
from dataclasses import dataclass, field
from typing import Callable, Dict, Iterable, List, Optional, Union, IO
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Union, IO
from uuid import UUID
from sigma.correlations import SigmaCorrelationRule

from sigma.rule import SigmaRule, SigmaRuleBase
import yaml

from sigma.correlations import SigmaCorrelationRule
from sigma.exceptions import (
SigmaCollectionError,
SigmaError,
SigmaRuleLocation,
SigmaRuleNotFoundError,
)
import yaml
from sigma.rule import SigmaRule, SigmaRuleBase


@dataclass
class SigmaCollection:
"""Collection of Sigma rules"""

rules: List[SigmaRule]
rules: List[SigmaRuleBase]
errors: List[SigmaError] = field(default_factory=list)
ids_to_rules: Dict[UUID, SigmaRule] = field(init=False, repr=False, hash=False, compare=False)
names_to_rules: Dict[str, SigmaRule] = field(init=False, repr=False, hash=False, compare=False)
ids_to_rules: Dict[UUID, SigmaRuleBase] = field(
init=False, repr=False, hash=False, compare=False
)
names_to_rules: Dict[str, SigmaRuleBase] = field(
init=False, repr=False, hash=False, compare=False
)

def __post_init__(self):
"""
Expand Down Expand Up @@ -160,7 +165,7 @@ def load_ruleset(
:param inputs: List of strings and :class:`pathlib.Path` objects that reference files or
directories that should be loaded.
:param collect_errors: parse or verification errors are collected in :class:`SigmaRule`
:param collect_errors: parse or verification errors are collected in :class:`SigmaRuleBase`
objects instead of raising them immediately. Defaults to ``False``.
:param on_beforeload: Optional function that is called for each path to a Sigma rule before the parsing and
construction of the :class:`SigmaCollection` object is done. The path returned by this function is
Expand Down Expand Up @@ -212,7 +217,7 @@ def get_output_rules(self) -> Iterable[SigmaRuleBase]:
"""Returns an iterator across all rules where the output property is set to true"""
return (rule for rule in self.rules if rule._output)

def get_unrefereced_rules(self) -> Iterable[SigmaRuleBase]:
def get_unreferenced_rules(self) -> Iterable[SigmaRuleBase]:
"""Returns an iterator across all rules that are not referenced by any other rule"""
return (rule for rule in self.rules if not rule._backreferences)

Expand Down
7 changes: 4 additions & 3 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path
from uuid import UUID

import pytest

from sigma.collection import SigmaCollection, deep_dict_update
from sigma.correlations import (
SigmaCorrelationCondition,
Expand All @@ -10,15 +12,14 @@
SigmaCorrelationType,
SigmaRuleReference,
)
from sigma.rule import SigmaRule, SigmaLogSource
from sigma.types import SigmaString
from sigma.exceptions import (
SigmaCollectionError,
SigmaModifierError,
SigmaRuleLocation,
SigmaError,
SigmaRuleNotFoundError,
)
from sigma.rule import SigmaRule, SigmaLogSource


def test_single_rule():
Expand Down Expand Up @@ -384,7 +385,7 @@ def test_get_output_rules(rules_with_correlation):


def test_get_unreferenced_rules(rules_with_correlation):
output_rules = list(rules_with_correlation.get_unrefereced_rules())
output_rules = list(rules_with_correlation.get_unreferenced_rules())
assert len(output_rules) == 1
assert isinstance(output_rules[0], SigmaCorrelationRule)

Expand Down

0 comments on commit 0ad327e

Please sign in to comment.