sift_py.yaml.report_templates

 1from datetime import datetime
 2from pathlib import Path
 3from typing import Any, Dict, List, cast
 4
 5import yaml
 6from typing_extensions import NotRequired, TypedDict
 7
 8from sift_py.ingestion.config.yaml.error import YamlConfigError
 9from sift_py.report_templates.config import ReportTemplateConfig
10from sift_py.yaml.utils import _handle_subdir
11
12
13def load_report_templates(paths: List[Path]) -> List[ReportTemplateConfig]:
14    """
15    Takes in a list of paths to YAML files which contains report templates and processes them into a list of
16    `ReportTemplateConfig` objects. For more information on report templates see
17    `sift_py.report_templates.config.ReportTemplateConfig`.
18    """
19    report_templates: List[ReportTemplateConfig] = []
20
21    def update_report_templates(path: Path):
22        report_templates.extend(_read_report_template_yaml(path))
23
24    for path in paths:
25        if path.is_dir():
26            _handle_subdir(path, update_report_templates)
27        elif path.is_file():
28            update_report_templates(path)
29    return report_templates
30
31
32def _read_report_template_yaml(path: Path) -> List[ReportTemplateConfig]:
33    report_templates = []
34    with open(path, "r") as f:
35        report_templates_yaml = cast(Dict[str, Any], yaml.safe_load(f.read()))
36
37        report_template_list = report_templates_yaml.get("report_templates")
38        if not isinstance(report_template_list, list):
39            raise YamlConfigError(
40                f"Expected 'report_templates' to be a list in report template yaml: '{path}'"
41            )
42
43        for report_template in report_template_list:
44            try:
45                report_template_config = ReportTemplateConfig(**report_template)
46                report_templates.append(report_template_config)
47            except Exception as e:
48                raise YamlConfigError(f"Error parsing report template '{report_template}'") from e
49
50        return report_templates
51
52
53class ReportTemplateYamlSpec(TypedDict):
54    """
55    Formal spec for a report template.
56
57    `name`: Name of the report template.
58    `template_client_key`: Unique client key to identify the report template.
59    `organization_id`: Organization ID that the report template belongs to.
60    `tags`: Tags to associate with the report template.
61    `description`: Description of the report template.
62    `rule_client_keys`: List of rule client keys associated with the report template.
63    `archived_date`: Date when the report template was archived. Setting this field
64      will archive the report template, and unsetting it will unarchive the report template.
65    """
66
67    name: str
68    template_client_key: str
69    organization_id: NotRequired[str]
70    tags: NotRequired[List[str]]
71    description: NotRequired[str]
72    rule_client_keys: List[str]
73    archived_date: NotRequired[datetime]
def load_report_templates( paths: List[pathlib.Path]) -> List[sift_py.report_templates.config.ReportTemplateConfig]:
14def load_report_templates(paths: List[Path]) -> List[ReportTemplateConfig]:
15    """
16    Takes in a list of paths to YAML files which contains report templates and processes them into a list of
17    `ReportTemplateConfig` objects. For more information on report templates see
18    `sift_py.report_templates.config.ReportTemplateConfig`.
19    """
20    report_templates: List[ReportTemplateConfig] = []
21
22    def update_report_templates(path: Path):
23        report_templates.extend(_read_report_template_yaml(path))
24
25    for path in paths:
26        if path.is_dir():
27            _handle_subdir(path, update_report_templates)
28        elif path.is_file():
29            update_report_templates(path)
30    return report_templates

Takes in a list of paths to YAML files which contains report templates and processes them into a list of ReportTemplateConfig objects. For more information on report templates see sift_py.report_templates.config.ReportTemplateConfig.

class ReportTemplateYamlSpec(typing_extensions.TypedDict):
54class ReportTemplateYamlSpec(TypedDict):
55    """
56    Formal spec for a report template.
57
58    `name`: Name of the report template.
59    `template_client_key`: Unique client key to identify the report template.
60    `organization_id`: Organization ID that the report template belongs to.
61    `tags`: Tags to associate with the report template.
62    `description`: Description of the report template.
63    `rule_client_keys`: List of rule client keys associated with the report template.
64    `archived_date`: Date when the report template was archived. Setting this field
65      will archive the report template, and unsetting it will unarchive the report template.
66    """
67
68    name: str
69    template_client_key: str
70    organization_id: NotRequired[str]
71    tags: NotRequired[List[str]]
72    description: NotRequired[str]
73    rule_client_keys: List[str]
74    archived_date: NotRequired[datetime]

Formal spec for a report template.

name: Name of the report template. template_client_key: Unique client key to identify the report template. organization_id: Organization ID that the report template belongs to. tags: Tags to associate with the report template. description: Description of the report template. rule_client_keys: List of rule client keys associated with the report template. archived_date: Date when the report template was archived. Setting this field will archive the report template, and unsetting it will unarchive the report template.

name: str
template_client_key: str
organization_id: typing_extensions.NotRequired[str]
tags: typing_extensions.NotRequired[typing.List[str]]
description: typing_extensions.NotRequired[str]
rule_client_keys: List[str]
archived_date: typing_extensions.NotRequired[datetime.datetime]