summaryrefslogtreecommitdiff
path: root/python/vyos/xml_ref/__init__.py
blob: 91ce394f70dfa7630eb793f17d565e71913ca8e6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2023 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/>.

from typing import Optional, Union, TYPE_CHECKING
from vyos.xml_ref import definition
from vyos.xml_ref import op_definition

if TYPE_CHECKING:
    from vyos.config import ConfigDict

def load_reference(cache=[]):
    if cache:
        return cache[0]

    xml = definition.Xml()

    try:
        from vyos.xml_ref.cache import reference
    except Exception:
        raise ImportError('no xml reference cache !!')

    if not reference:
        raise ValueError('empty xml reference cache !!')

    xml.define(reference)
    cache.append(xml)

    return xml

def is_tag(path: list) -> bool:
    return load_reference().is_tag(path)

def is_tag_value(path: list) -> bool:
    return load_reference().is_tag_value(path)

def is_multi(path: list) -> bool:
    return load_reference().is_multi(path)

def is_valueless(path: list) -> bool:
    return load_reference().is_valueless(path)

def is_leaf(path: list) -> bool:
    return load_reference().is_leaf(path)

def owner(path: list) -> str:
    return load_reference().owner(path)

def priority(path: list) -> str:
    return load_reference().priority(path)

def cli_defined(path: list, node: str, non_local=False) -> bool:
    return load_reference().cli_defined(path, node, non_local=non_local)

def component_version() -> dict:
    return load_reference().component_version()

def default_value(path: list) -> Optional[Union[str, list]]:
    return load_reference().default_value(path)

def multi_to_list(rpath: list, conf: dict) -> dict:
    return load_reference().multi_to_list(rpath, conf)

def get_defaults(path: list, get_first_key=False, recursive=False) -> dict:
    return load_reference().get_defaults(path, get_first_key=get_first_key,
                                         recursive=recursive)

def relative_defaults(rpath: list, conf: dict, get_first_key=False,
                      recursive=False) -> dict:

    return load_reference().relative_defaults(rpath, conf,
                                              get_first_key=get_first_key,
                                              recursive=recursive)

def from_source(d: dict, path: list) -> bool:
    return definition.from_source(d, path)

def ext_dict_merge(source: dict, destination: Union[dict, 'ConfigDict']):
    return definition.ext_dict_merge(source, destination)

def load_op_reference(op_cache=[]):
    if op_cache:
        return op_cache[0]

    op_xml = op_definition.OpXml()

    try:
        from vyos.xml_ref.op_cache import op_reference
    except Exception:
        raise ImportError('no xml op reference cache !!')

    if not op_reference:
        raise ValueError('empty xml op reference cache !!')

    op_xml.define(op_reference)
    op_cache.append(op_xml)

    return op_xml

def get_op_ref_path(path: list) -> list[op_definition.PathData]:
    return load_op_reference()._get_op_ref_path(path)