-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 directive to load/validate yaml examples
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,38 @@ | ||
import importlib | ||
|
||
import yaml | ||
from docutils import nodes | ||
from docutils.parsers.rst import Directive | ||
|
||
|
||
class InjectValidatedExample(Directive): | ||
"""Directive to inject and validate an example from a model class, then display it as a YAML code block.""" | ||
|
||
required_arguments = 1 # Full path to the model class (e.g., mylibrary.submodule.models.AdminOIDCConfigurationModel) | ||
optional_arguments = 0 | ||
final_argument_whitespace = False | ||
|
||
def run(self): | ||
full_path = self.arguments[0] | ||
|
||
# Split the full path into module and class parts | ||
*module_parts, class_name = full_path.split(".") | ||
module_name = ".".join(module_parts) | ||
|
||
# Dynamically import the module | ||
module = importlib.import_module(module_name) | ||
step_class = getattr(module, class_name) | ||
|
||
# Validate the example using the model | ||
model_class = step_class.config_model | ||
data = yaml.safe_load(step_class.example) | ||
validated_data = model_class.parse_obj(data[step_class.namespace]) | ||
|
||
# Convert validated data to YAML | ||
# yaml_output = yaml.dump(step_class.example, default_flow_style=False) | ||
|
||
# Create a literal block with YAML output | ||
literal = nodes.literal_block(step_class.example, step_class.example) | ||
literal["language"] = "yaml" | ||
|
||
return [literal] |