This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any | ||
|
||
from catalystwan.api.templates.models.supported import available_models | ||
from catalystwan.exceptions import TemplateTypeError | ||
|
||
|
||
def choose_model(type_value: str) -> Any: | ||
"""Chooses correct model based on provided type | ||
With provided type of feature template searches supported by catalystwan models | ||
and returns correct for given type of feature template class. | ||
Args: | ||
type_value: type of feature template | ||
Returns: | ||
model | ||
Raises: | ||
TemplateTypeError: Raises when the model is not supported by catalystwan. | ||
""" | ||
if type_value not in available_models: | ||
for model in available_models.values(): | ||
if model.type == type_value: # type: ignore | ||
return model | ||
raise TemplateTypeError(f"Feature template type '{type_value}' is not supported.") | ||
|
||
return available_models[type_value] |
77 changes: 77 additions & 0 deletions
77
catalystwan/utils/feature_template/find_template_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import Dict, List, Optional, Union | ||
|
||
from catalystwan.api.templates.device_variable import DeviceVariable | ||
|
||
|
||
def find_template_values( | ||
template_definition: dict, | ||
templated_values: dict = {}, | ||
target_key: str = "vipType", | ||
target_key_value_to_ignore: str = "ignore", | ||
target_key_for_template_value: str = "vipValue", | ||
device_specific_variables: Optional[Dict[str, DeviceVariable]] = None, | ||
path: Optional[List[str]] = None, | ||
) -> Dict[str, Union[str, list, dict]]: | ||
"""Based on provided template definition generates a dictionary with template fields and values | ||
Args: | ||
template_definition: template definition provided as dict | ||
templated_values: dictionary, empty at the beginning and filed out with names of fields as keys | ||
and values of those fields as values | ||
target_key: name of the key specifying if field is used in template, defaults to 'vipType' | ||
target_key_value_to_ignore: value of the target key indicating | ||
that field is not used in template, defaults to 'ignore' | ||
target_key_for_template_value: name of the key specifying value of field used in template, | ||
defaults to 'vipValue' | ||
path: a list of keys indicating current path, defaults to None | ||
Returns: | ||
templated_values: dictionary containing template fields as key and values assigned to those fields as values | ||
""" | ||
if path is None: | ||
path = [] | ||
|
||
# if value object is reached, try to extract the value | ||
if target_key in template_definition: | ||
if template_definition[target_key] == target_key_value_to_ignore: | ||
return templated_values | ||
|
||
value = template_definition[target_key] | ||
template_value = template_definition[target_key_for_template_value] | ||
|
||
field_key = path[-1] | ||
# TODO: Handle nested DeviceVariable | ||
if value == "variableName" and (device_specific_variables is not None): | ||
device_specific_variables[field_key] = DeviceVariable(name=template_definition["vipVariableName"]) | ||
elif template_definition["vipObjectType"] != "tree": | ||
current_nesting = get_nested_dict(templated_values, path[:-1]) | ||
current_nesting[field_key] = template_value | ||
elif isinstance(template_value, dict): | ||
find_template_values( | ||
value, templated_values, device_specific_variables=device_specific_variables, path=path | ||
) | ||
elif isinstance(template_value, list): | ||
current_nesting = get_nested_dict(templated_values, path[:-1]) | ||
current_nesting[field_key] = [] | ||
for item in template_value: | ||
current_nesting[field_key].append( | ||
find_template_values(item, {}, device_specific_variables=device_specific_variables) | ||
) | ||
|
||
return templated_values | ||
|
||
# iterate the dict to extract values and assign them to their fields | ||
for key, value in template_definition.items(): | ||
if isinstance(value, dict) and value != target_key_value_to_ignore: | ||
find_template_values( | ||
value, templated_values, device_specific_variables=device_specific_variables, path=path + [key] | ||
) | ||
return templated_values | ||
|
||
|
||
def get_nested_dict(d: dict, path: List[str], populate: bool = True): | ||
current_dict = d | ||
for path_key in path: | ||
if path_key not in current_dict and populate: | ||
current_dict[path_key] = {} | ||
current_dict = current_dict[path_key] | ||
return current_dict |