|
67 | 67 | import json |
68 | 68 | from copy import deepcopy |
69 | 69 |
|
| 70 | +import vyos.xml |
70 | 71 | import vyos.util |
71 | 72 | import vyos.configtree |
72 | 73 | from vyos.configsource import ConfigSource, ConfigSourceSession |
@@ -193,50 +194,62 @@ def show_config(self, path=[], default=None, effective=False): |
193 | 194 | """ |
194 | 195 | return self._config_source.show_config(path, default, effective) |
195 | 196 |
|
196 | | - def get_cached_dict(self, effective=False): |
| 197 | + def get_cached_root_dict(self, effective=False): |
197 | 198 | cached = self._dict_cache.get(effective, {}) |
198 | 199 | if cached: |
199 | | - config_dict = cached |
| 200 | + return cached |
| 201 | + |
| 202 | + if effective: |
| 203 | + config = self._running_config |
200 | 204 | else: |
201 | | - config_dict = {} |
| 205 | + config = self._session_config |
202 | 206 |
|
203 | | - if effective: |
204 | | - if self._running_config: |
205 | | - config_dict = json.loads((self._running_config).to_json()) |
206 | | - else: |
207 | | - if self._session_config: |
208 | | - config_dict = json.loads((self._session_config).to_json()) |
| 207 | + if config: |
| 208 | + config_dict = json.loads(config.to_json()) |
| 209 | + else: |
| 210 | + config_dict = {} |
209 | 211 |
|
210 | | - self._dict_cache[effective] = config_dict |
| 212 | + self._dict_cache[effective] = config_dict |
211 | 213 |
|
212 | 214 | return config_dict |
213 | 215 |
|
214 | | - def get_config_dict(self, path=[], effective=False, key_mangling=None, get_first_key=False): |
| 216 | + def get_config_dict(self, path=[], effective=False, key_mangling=None, |
| 217 | + get_first_key=False, no_multi_convert=False): |
215 | 218 | """ |
216 | 219 | Args: |
217 | 220 | path (str list): Configuration tree path, can be empty |
218 | 221 | effective=False: effective or session config |
219 | 222 | key_mangling=None: mangle dict keys according to regex and replacement |
220 | 223 | get_first_key=False: if k = path[:-1], return sub-dict d[k] instead of {k: d[k]} |
| 224 | + no_multi_convert=False: if convert, return single value of multi node as list |
221 | 225 |
|
222 | 226 | Returns: a dict representation of the config under path |
223 | 227 | """ |
224 | | - config_dict = self.get_cached_dict(effective) |
| 228 | + lpath = self._make_path(path) |
| 229 | + root_dict = self.get_cached_root_dict(effective) |
| 230 | + conf_dict = vyos.util.get_sub_dict(root_dict, lpath, get_first_key) |
225 | 231 |
|
226 | | - config_dict = vyos.util.get_sub_dict(config_dict, self._make_path(path), get_first_key) |
| 232 | + if not key_mangling and no_multi_convert: |
| 233 | + return deepcopy(conf_dict) |
227 | 234 |
|
228 | | - if key_mangling: |
229 | | - if not (isinstance(key_mangling, tuple) and \ |
230 | | - (len(key_mangling) == 2) and \ |
231 | | - isinstance(key_mangling[0], str) and \ |
232 | | - isinstance(key_mangling[1], str)): |
233 | | - raise ValueError("key_mangling must be a tuple of two strings") |
234 | | - else: |
235 | | - config_dict = vyos.util.mangle_dict_keys(config_dict, key_mangling[0], key_mangling[1]) |
236 | | - else: |
237 | | - config_dict = deepcopy(config_dict) |
| 235 | + xmlpath = lpath if get_first_key else lpath[:-1] |
238 | 236 |
|
239 | | - return config_dict |
| 237 | + if not key_mangling: |
| 238 | + conf_dict = vyos.xml.multi_to_list(xmlpath, conf_dict) |
| 239 | + return conf_dict |
| 240 | + |
| 241 | + if no_multi_convert is False: |
| 242 | + conf_dict = vyos.xml.multi_to_list(xmlpath, conf_dict) |
| 243 | + |
| 244 | + if not (isinstance(key_mangling, tuple) and \ |
| 245 | + (len(key_mangling) == 2) and \ |
| 246 | + isinstance(key_mangling[0], str) and \ |
| 247 | + isinstance(key_mangling[1], str)): |
| 248 | + raise ValueError("key_mangling must be a tuple of two strings") |
| 249 | + |
| 250 | + conf_dict = vyos.util.mangle_dict_keys(conf_dict, key_mangling[0], key_mangling[1]) |
| 251 | + |
| 252 | + return conf_dict |
240 | 253 |
|
241 | 254 | def is_multi(self, path): |
242 | 255 | """ |
|
0 commit comments