Skip to content

misc

Miscellaneous utilities.

DEFAULT_LOGGING module-attribute #

DEFAULT_LOGGING = load('---\nversion: 1\nformatters:\n  simple_format:\n    format: "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s"\n    datefmt: "[%Y-%m-%d %H:%M:%S]"\n\nhandlers:\n  console:\n    class : logging.StreamHandler\n    formatter: simple_format\n    level   : DEBUG\n\nloggers:\n  fortimanager_template_sync:\n    level: INFO\n\nroot:\n  level: WARNING\n  handlers: [console]\n')

logger module-attribute #

logger = getLogger(__name__)

yaml module-attribute #

yaml = YAML(typ='safe', pure=True)

find_all_vars #

find_all_vars(template_content: str) -> set

Find all undeclared variables in the given template content.

Parameters:

Name Type Description Default
template_content str

The content of the template.

required

Returns:

Name Type Description
list set

A list of undeclared variables found in the template content.

Source code in fortimanager_template_sync/misc.py
def find_all_vars(template_content: str) -> set:
    """
    Find all undeclared variables in the given template content.

    Args:
        template_content (str): The content of the template.

    Returns:
        list: A list of undeclared variables found in the template content.
    """
    env = Environment()
    parsed_content = env.parse(template_content)

    return meta.find_undeclared_variables(parsed_content)

get_logging_config #

get_logging_config(config: str)

prepare logging config and convert it to dict

Source code in fortimanager_template_sync/misc.py
def get_logging_config(config: str):
    """prepare logging config and convert it to dict"""
    if config is None:
        return DEFAULT_LOGGING
    if Path(config).is_file():
        with open(config, encoding="UTF-8") as fi:
            config = yaml.load(fi)
        return config
    raise ValueError(f"File '{config}' not found!")

sanitize_variables #

sanitize_variables(variables: List[Variable]) -> List[Variable]

De-dup and check variables, so they are unique in name and default value

Parameters:

Name Type Description Default
variables List[Variable]

input list of variables

required

Returns:

Type Description
List[Variable]

list of variables

Raises:

Type Description
FMGSyncVariableException

on variable definition problem

Source code in fortimanager_template_sync/misc.py
def sanitize_variables(variables: List["Variable"]) -> List["Variable"]:
    """De-dup and check variables, so they are unique in name and default value

    Args:
        variables: input list of variables

    Returns:
        list of variables

    Raises:
        FMGSyncVariableException: on variable definition problem
    """
    good_variables = []
    for variable in variables:
        if variable.name not in good_variables:
            good_variables.append(variable)
            continue
        existing_var = first([var for var in good_variables if var.name == variable.name])
        if variable.value != existing_var.value:
            error = f"Variable {variable.name} has multiple default values amongst templates!"
            logger.error(error)
            raise FMGSyncVariableException(error)

    return good_variables