Skip to content

variable

Class info

Classes

Name Children Inherits
ConfigConstructor
configz.yaml_tools.constructors.variable
Custom YAML constructor with variable support.
    VariableReference
    configz.yaml_tools.constructors.variable
    Represents a variable reference in YAML.

      🛈 DocStrings

      ConfigConstructor

      Bases: Constructor

      Custom YAML constructor with variable support.

      Source code in src/configz/yaml_tools/constructors/variable.py
      class ConfigConstructor(Constructor):
          """Custom YAML constructor with variable support."""
      
          def __init__(self, variables: Mapping[str, Any] | None = None) -> None:
              super().__init__()
              self.variables: Mapping[str, Any] = variables or {}
      
          def construct_variable(self, loader: Any, node: Any) -> Any:
              """Construct a variable reference."""
              value = loader.construct_scalar(node)
              if not isinstance(value, str):
                  msg = f"Invalid variable reference: {value}"
                  raise TypeError(msg)
              return self.variables.get(value, VariableReference(value))
      

      construct_variable

      construct_variable(loader: Any, node: Any) -> Any
      

      Construct a variable reference.

      Source code in src/configz/yaml_tools/constructors/variable.py
      def construct_variable(self, loader: Any, node: Any) -> Any:
          """Construct a variable reference."""
          value = loader.construct_scalar(node)
          if not isinstance(value, str):
              msg = f"Invalid variable reference: {value}"
              raise TypeError(msg)
          return self.variables.get(value, VariableReference(value))
      

      VariableReference

      Represents a variable reference in YAML.

      Source code in src/configz/yaml_tools/constructors/variable.py
      class VariableReference:
          """Represents a variable reference in YAML."""
      
          def __init__(self, name: str) -> None:
              self.name = name
      
          def resolve(self, variables: Mapping[str, Any]) -> Any:
              """Resolve variable value."""
              if self.name not in variables:
                  msg = f"Missing variable: {self.name}"
                  raise KeyError(msg)
              return variables[self.name]
      

      resolve

      resolve(variables: Mapping[str, Any]) -> Any
      

      Resolve variable value.

      Source code in src/configz/yaml_tools/constructors/variable.py
      def resolve(self, variables: Mapping[str, Any]) -> Any:
          """Resolve variable value."""
          if self.name not in variables:
              msg = f"Missing variable: {self.name}"
              raise KeyError(msg)
          return variables[self.name]