Skip to content

Variable Declaration

Classes for ODF document variables declarations.

This module provides classes for managing variable declaration “text:variable-decl” and variable declaration container “text:variable-decls”.

Classes:

Name Description
VarDecl

A variable declaration, “text:variable-decl”.

VarDeclMixin

Mixin class for elements that can contain variable declarations.

VarDecls

A container for variable declarations, “text:variable-decls”.

VarDecl

Bases: Element

A variable declaration, “text:variable-decl”.

This element defines a variable by its name and value type.

Attributes:

Name Type Description
name str

The unique name of the variable.

value_type str

The ODF value type (e.g., ‘string’, ‘float’).

Methods:

Name Description
__init__

Initialize the VarDecl element.

Source code in odfdo/variable_declaration.py
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
146
147
148
149
150
151
152
153
class VarDecl(Element):
    """A variable declaration, "text:variable-decl".

    This element defines a variable by its name and value type.

    Attributes:
        name (str): The unique name of the variable.
        value_type (str): The ODF value type (e.g., 'string', 'float').

    """

    _tag = "text:variable-decl"
    _properties = (
        PropDef("name", "text:name"),
        PropDef("value_type", "office:value-type"),
    )

    def __init__(
        self,
        name: str | None = None,
        value_type: str | None = None,
        **kwargs: Any,
    ) -> None:
        """Initialize the VarDecl element.

        Args:
            name: The name of the variable.
            value_type: The ODF value type.
            kwargs: Arbitrary keyword arguments for the Element base class.

        """
        super().__init__(**kwargs)
        if self._do_init:
            if name:
                self.name = name
            if value_type:
                self.value_type = value_type

_properties class-attribute instance-attribute

_properties = (
    PropDef("name", "text:name"),
    PropDef("value_type", "office:value-type"),
)

_tag class-attribute instance-attribute

_tag = 'text:variable-decl'

name instance-attribute

name = name

value_type instance-attribute

value_type = value_type

__init__

__init__(
    name: str | None = None,
    value_type: str | None = None,
    **kwargs: Any,
) -> None

Initialize the VarDecl element.

Parameters:

Name Type Description Default
name str | None

The name of the variable.

None
value_type str | None

The ODF value type.

None
kwargs Any

Arbitrary keyword arguments for the Element base class.

{}
Source code in odfdo/variable_declaration.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def __init__(
    self,
    name: str | None = None,
    value_type: str | None = None,
    **kwargs: Any,
) -> None:
    """Initialize the VarDecl element.

    Args:
        name: The name of the variable.
        value_type: The ODF value type.
        kwargs: Arbitrary keyword arguments for the Element base class.

    """
    super().__init__(**kwargs)
    if self._do_init:
        if name:
            self.name = name
        if value_type:
            self.value_type = value_type

VarDeclMixin

Bases: Element

Mixin class for elements that can contain variable declarations.

This mixin provides methods to access and manipulate “text:variable-decls” and “text:variable-decl”.

Used by the following classes
  • “office:chart”
  • “office:drawing”
  • “office:presentation”
  • “office:spreadsheet”
  • “office:text”
  • “style:footer”
  • “style:footer-first”
  • “style:footer-left”
  • “style:header”
  • “style:header-first”
  • “style:header-left”

Methods:

Name Description
get_variable_decl

Return a single variable declaration that matches the specified criteria.

get_variable_decl_list

Return all variable declarations as a list.

get_variable_decls

Return the container for variable declarations.

Source code in odfdo/variable_declaration.py
 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
class VarDeclMixin(Element):
    """Mixin class for elements that can contain variable declarations.

    This mixin provides methods to access and manipulate "text:variable-decls"
    and "text:variable-decl".

    Used by the following classes:
        - "office:chart"
        - "office:drawing"
        - "office:presentation"
        - "office:spreadsheet"
        - "office:text"
        - "style:footer"
        - "style:footer-first"
        - "style:footer-left"
        - "style:header"
        - "style:header-first"
        - "style:header-left"
    """

    def get_variable_decls(self) -> VarDecls:
        """Return the container for variable declarations.

        If the container is not found, it is created within the document body.

        Returns:
            VarDecls: The VarDecls instance (container for variable declarations).

        Raises:
            ValueError: If the document body is empty and a new container cannot be inserted.

        """
        variable_decls = self.get_element("//text:variable-decls")
        if variable_decls is None:
            body = self.document_body
            if not body:
                msg = "Empty document.body"
                raise ValueError(msg)
            body.insert(Element.from_tag("text:variable-decls"), FIRST_CHILD)
            variable_decls = body.get_element("//text:variable-decls")

        return cast("VarDecls", variable_decls)

    def get_variable_decl_list(self) -> list[VarDecls]:
        """Return all variable declarations as a list.

        Returns:
            list[VarDecls]: A list of all VarDecls instances that are descendants of this element.

        """
        return cast(
            "list[VarDecls]", self._filtered_elements("descendant::text:variable-decl")
        )

    def get_variable_decl(self, name: str, position: int = 0) -> VarDecls | None:
        """Return a single variable declaration that matches the specified criteria.

        Args:
            name: The name of the variable declaration to retrieve.
            position: The 0-based index of the matching variable declaration to return.

        Returns:
            VarDecls | None: A VarDecls instance, or None if no declaration matches the criteria.

        """
        return cast(
            "VarDecls | None",
            self._filtered_element(
                "descendant::text:variable-decl", position, text_name=name
            ),
        )

get_variable_decl

get_variable_decl(
    name: str, position: int = 0
) -> VarDecls | None

Return a single variable declaration that matches the specified criteria.

Parameters:

Name Type Description Default
name str

The name of the variable declaration to retrieve.

required
position int

The 0-based index of the matching variable declaration to return.

0

Returns:

Type Description
VarDecls | None

VarDecls | None: A VarDecls instance, or None if no declaration matches the criteria.

Source code in odfdo/variable_declaration.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def get_variable_decl(self, name: str, position: int = 0) -> VarDecls | None:
    """Return a single variable declaration that matches the specified criteria.

    Args:
        name: The name of the variable declaration to retrieve.
        position: The 0-based index of the matching variable declaration to return.

    Returns:
        VarDecls | None: A VarDecls instance, or None if no declaration matches the criteria.

    """
    return cast(
        "VarDecls | None",
        self._filtered_element(
            "descendant::text:variable-decl", position, text_name=name
        ),
    )

get_variable_decl_list

get_variable_decl_list() -> list[VarDecls]

Return all variable declarations as a list.

Returns:

Type Description
list[VarDecls]

list[VarDecls]: A list of all VarDecls instances that are descendants of this element.

Source code in odfdo/variable_declaration.py
77
78
79
80
81
82
83
84
85
86
def get_variable_decl_list(self) -> list[VarDecls]:
    """Return all variable declarations as a list.

    Returns:
        list[VarDecls]: A list of all VarDecls instances that are descendants of this element.

    """
    return cast(
        "list[VarDecls]", self._filtered_elements("descendant::text:variable-decl")
    )

get_variable_decls

get_variable_decls() -> VarDecls

Return the container for variable declarations.

If the container is not found, it is created within the document body.

Returns:

Name Type Description
VarDecls VarDecls

The VarDecls instance (container for variable declarations).

Raises:

Type Description
ValueError

If the document body is empty and a new container cannot be inserted.

Source code in odfdo/variable_declaration.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def get_variable_decls(self) -> VarDecls:
    """Return the container for variable declarations.

    If the container is not found, it is created within the document body.

    Returns:
        VarDecls: The VarDecls instance (container for variable declarations).

    Raises:
        ValueError: If the document body is empty and a new container cannot be inserted.

    """
    variable_decls = self.get_element("//text:variable-decls")
    if variable_decls is None:
        body = self.document_body
        if not body:
            msg = "Empty document.body"
            raise ValueError(msg)
        body.insert(Element.from_tag("text:variable-decls"), FIRST_CHILD)
        variable_decls = body.get_element("//text:variable-decls")

    return cast("VarDecls", variable_decls)

VarDecls

Bases: Element

A container for variable declarations, “text:variable-decls”.

This element groups all the ‘text:variable-decl’ elements in the document.

Source code in odfdo/variable_declaration.py
107
108
109
110
111
112
113
114
class VarDecls(Element):
    """A container for variable declarations, "text:variable-decls".

    This element groups all the 'text:variable-decl' elements in the
    document.
    """

    _tag = "text:variable-decls"

_tag class-attribute instance-attribute

_tag = 'text:variable-decls'