summaryrefslogtreecommitdiff
path: root/python/vyos/compose_config.py
blob: 79a8718c5b9c24976c597fa89eaf3905ec8a837f (plain)
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

"""This module allows iterating over function calls to modify an existing
config.
"""

import traceback
from pathlib import Path
from typing import TypeAlias, Union, Callable

from vyos.configtree import ConfigTree
from vyos.configtree import deep_copy as ct_deep_copy
from vyos.utils.system import load_as_module_source

ConfigObj: TypeAlias = Union[str, ConfigTree]

class ComposeConfigError(Exception):
    """Raised when an error occurs modifying a config object.
    """

class ComposeConfig:
    """Apply function to config tree: for iteration over functions or files.
    """
    def __init__(self, config_obj: ConfigObj, checkpoint_file=None):
        if isinstance(config_obj, ConfigTree):
            self.config_tree = config_obj
        else:
            self.config_tree = ConfigTree(config_obj)

        self.checkpoint = self.config_tree
        self.checkpoint_file = checkpoint_file

    def apply_func(self, func: Callable):
        """Apply the function to the config tree.
        """
        if not callable(func):
            raise ComposeConfigError(f'{func.__name__} is not callable')

        if self.checkpoint_file is not None:
            self.checkpoint = ct_deep_copy(self.config_tree)

        try:
            func(self.config_tree)
        except Exception as e:
            if self.checkpoint_file is not None:
                self.config_tree = self.checkpoint
            raise ComposeConfigError(e) from e

    def apply_file(self, func_file: str, func_name: str):
        """Apply named function from file.
        """
        try:
            mod_name = Path(func_file).stem.replace('-', '_')
            mod = load_as_module_source(mod_name, func_file)
            func = getattr(mod, func_name)
        except Exception as e:
            raise ComposeConfigError(f'Error with {func_file}: {e}') from e

        try:
            self.apply_func(func)
        except ComposeConfigError as e:
            msg = str(e)
            tb = f'{traceback.format_exc()}'
            raise ComposeConfigError(f'Error in {func_file}: {msg}\n{tb}') from e

    def to_string(self, with_version=False) -> str:
        """Return the rendered config tree.
        """
        return self.config_tree.to_string(no_version=not with_version)

    def write(self, config_file: str, with_version=False):
        """Write the config tree to a file.
        """
        config_str = self.to_string(with_version=with_version)
        Path(config_file).write_text(config_str)