Skip to content
Snippets Groups Projects
Select Git revision
  • b08e489c0c44b215b3427b69ff7eb2801843f105
  • master default protected
  • eccs-docker
  • refactor/web-statistics-removal
  • refactor/StatisticsButtonPlacement
  • feature/webdataAPIMethod
  • feature_request2
  • v2.1.0
  • v2.0.6
  • v2.0.5
  • v2.0.4
  • v2.0.3
  • v2.0.2
  • v2.0.1
  • v2.0.0
  • v1.0.2
  • v1.0.1
  • v1.0.0
18 results

pyff_cron

Blame
  • config.py 3.14 KiB
    import json
    import logging.config
    import pathlib
    
    import jsonschema
    
    logger = logging.getLogger(__name__)
    
    ERROR_REPORT_CONFIG_SCHEMA = {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "definitions": {
            "email-params": {
                "type": "object",
                "properties": {
                    "from": {"type": "string"},
                    "reply_to": {"type": "string"},
                    "to": {"$ref": "#/definitions/string-or-array"},
                    "cc": {"$ref": "#/definitions/string-or-array"},
                    "hostname": {"type": "string"},
                    "port": {"type": "integer"},
                    "username": {"type": "string"},
                    "password": {"type": "string"},
                    "starttls": {"type": "boolean"},
                },
                "required": [
                    "from",
                    "to",
                    "hostname",
                ],
                "additionalProperties": False,
            },
            "string-or-array": {
                "oneOf": [
                    {"type": "string"},
                    {"type": "array", "items": {"type": "string"}, "minItems": 1},
                ]
            },
            "influx-db-params": {
                "type": "object",
                "properties": {
                    "ssl": {"type": "boolean"},
                    "hostname": {"type": "string"},
                    "port": {"type": "integer"},
                    "username": {"type": "string"},
                    "password": {"type": "string"},
                    "database": {"type": "string"},
                    "measurement": {"type": "string"},
                },
                "required": [
                    # ssl, port are optional
                    "hostname",
                    "username",
                    "password",
                    "database",
                    "measurement",
                ],
                "additionalProperties": False,
            },
        },
        "type": "object",
        "properties": {
            "email": {"$ref": "#/definitions/email-params"},
            "inventory": {
                "type": "array",
                "items": {"type": "string", "format": "uri"},
                "minItems": 1,
            },
            "influx": {"$ref": "#/definitions/influx-db-params"},
            "exclude-interfaces": {
                "type": "array",
                "items": {
                    "type": "array",
                    "prefixItems": [
                        {"type": "string"},
                        {"type": "string"},
                    ],
                },
            },
        },
        "required": ["email", "influx"],
        "additionalProperties": False,
    }
    
    
    def load(config_file: pathlib.Path):
        """
        loads, validates and returns configuration parameters
    
        :param config_file: filename (file-like object, opened for reading)
        :return: a dict containing configuration parameters
        :raises: json.JSONDecodeError, jsonschema.ValidationError
        """
    
        config = json.loads(config_file.read_text())
        jsonschema.validate(config, ERROR_REPORT_CONFIG_SCHEMA)
    
        # convert str addresses into list of str
        if isinstance(config["email"]["to"], str):
            config["email"]["to"] = [config["email"]["to"]]
        if isinstance(config["email"].get("cc"), str):
            config["email"]["cc"] = [config["email"]["cc"]]
        return config