Context
nautobot_design_builder.context
¶
Module that contains classes and functions for use with Design Builder context available when using Jinja templating.
Context
¶
Bases: _DictNode
A context represents a tree of variables that can include templates for values.
The Design Builder context is a tree structure that can be used for a Jinja2 render context. One of the strengths of using the Design Builder context is that context information can be provided both in a python class (as normal properties and methods) as well as in YAML files that can be loaded.
YAML files are loaded in and merged with the context, so many files can be loaded to provide a complete context. This allows the context files to be organized in whatever structure makes sense to the design author.
Another strength of the context is that string values can be Jinja templates that will render native Python types. The template render context is the context tree root. This means that values within the context tree can be used to compute other values at render time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
a dictionary of values to be loaded into the context. This dictionary will be recursively evaluated and each level will be stored as either a _DictNode or _ListNode. Leaves will be stored either as a _TemplateNode or their native type. |
None
|
Source code in nautobot_design_builder/context.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
__init__(data=None, job_result=None)
¶
Constructor for Context class that creates data nodes from input data.
Source code in nautobot_design_builder/context.py
base_context()
classmethod
¶
The base context is the combination of any context_files that have been added to a context.
Calling base_context will merge all of the context trees that have been added by the @context_file decorator.
Returns:
Name | Type | Description |
---|---|---|
Context |
Context
|
Merged context tree |
Source code in nautobot_design_builder/context.py
base_context_files()
classmethod
¶
Calculate the complete list of context files for the class.
Source code in nautobot_design_builder/context.py
load(yaml_or_mapping)
classmethod
¶
Load a context from a yaml file or mapping.
Source code in nautobot_design_builder/context.py
validate()
¶
Validate that the context can be used to render a design.
This method will look for any method names that start with "validate_" and will call them successively.
Raises:
Type | Description |
---|---|
DesignValidationErrors
|
raised by the validators will be collected. and a single DesignValidationError will be raised that includes all the error messages. |
Source code in nautobot_design_builder/context.py
ContextNodeMixin
¶
A mixin to help create tree nodes for the Design Builder context.
This mixin provides overridden getitem and setitem magic methods that will automatically get and set the tree node types. The mixin also provides a mechanism for a node within the tree to find the context's root node and root render environment.
Source code in nautobot_design_builder/context.py
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
env: NativeEnvironment
property
¶
Lookup the Jinja2 native environment from the root context node.
root: ContextNodeMixin
cached
property
¶
Lookup and return the root node in the context tree.
Returns:
Name | Type | Description |
---|---|---|
ContextNodeMixin |
ContextNodeMixin
|
root node |
__getitem__(key)
¶
Get the desired item from within the node's children.
__getitem__
will first look for items in the context
node's data
attribute. If the data
attribute does
not exist, than the lookup will default to the superclass
__getitem__
. If the found item is a _TemplateNode
then
the template is rendered and the resulting native type is
returned.
Source code in nautobot_design_builder/context.py
__repr__()
¶
Get the printable representation of the node.
This will return the repr
of either the node's container data
attribute (if it exists) or the super class representation.
Source code in nautobot_design_builder/context.py
__setitem__(key, value)
¶
Store a new value within the node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
int | str
|
Index/key/attribute name |
required |
value |
Any
|
Value of item to store |
required |
Raises:
Type | Description |
---|---|
KeyError
|
if the item cannot be stored. |
Returns:
Name | Type | Description |
---|---|---|
ContextNodeMixin |
ContextNodeMixin
|
description |
Source code in nautobot_design_builder/context.py
context_file(*ctx_files)
¶
Add a context file to a class.
Context Files are only loaded once per class hierarchy, even if they are specified more than once in a class/subclass tree. Context files must be in YAML format. Each context file will be loaded and parsed and merged together to form a base context.