@@ -82,6 +82,12 @@ def parse_xml(xml: AnyStr) -> Element:
82
82
83
83
84
84
class XmlCommandElement :
85
+ """
86
+ Base class for XML commands
87
+
88
+ It's used to create XML command requests for the XML based protocols.
89
+ """
90
+
85
91
def __init__ (self , element : Element ):
86
92
self ._element = element
87
93
@@ -97,14 +103,20 @@ def add_element(
97
103
return XmlCommandElement (node )
98
104
99
105
def set_attribute (self , name : str , value : str ) -> "XmlCommandElement" :
106
+ """Set an attribute on the element.
107
+
108
+ Args:
109
+ name: Name of the attribute
110
+ value: Value of the attribute
111
+ """
100
112
self ._element .set (name , value )
101
113
return self
102
114
103
115
def set_attributes (self , attrs : dict [str , str ]) -> "XmlCommandElement" :
104
116
"""Set several attributes at once.
105
117
106
- Arguments :
107
- attrs (dict) : Attributes to be set on the element
118
+ Args :
119
+ attrs: Attributes to be set on the element
108
120
"""
109
121
for key , value in attrs .items ():
110
122
self ._element .set (key , value )
@@ -116,28 +128,60 @@ def append_xml_str(self, xml_text: str) -> None:
116
128
node = parse_xml (xml_text )
117
129
self ._element .append (node )
118
130
119
- def to_string (self ) -> str :
120
- return self .to_bytes ().decode ("utf-8" )
131
+ def to_string (self , * , encoding : str = "utf-8" ) -> str :
132
+ """
133
+ Convert the XML element to a string
134
+
135
+ Args:
136
+ encoding: The encoding to use for the string. Default is 'utf-8'.
137
+ """
138
+ return self .to_bytes ().decode (encoding )
121
139
122
140
def to_bytes (self ) -> bytes :
141
+ """
142
+ Convert the XML element to a bytes object
143
+ """
123
144
return xml_to_string (self ._element )
124
145
125
146
def __str__ (self ) -> str :
147
+ """
148
+ Convert the XML element to a string using the default encoding.
149
+ """
126
150
return self .to_string ()
127
151
128
152
def __bytes__ (self ) -> bytes :
153
+ """
154
+ Convert the XML element to a bytes object
155
+ """
129
156
return self .to_bytes ()
130
157
131
158
132
159
class XmlCommand (XmlCommandElement ):
160
+ """
161
+ Class to create XML commands
162
+ """
163
+
133
164
def __init__ (self , name : str ) -> None :
165
+ """
166
+ Create a new XML command
167
+
168
+ Args:
169
+ name: The name of the root element of the command.
170
+ """
134
171
super ().__init__ (create_element (name ))
135
172
136
173
def add_filter (
137
174
self ,
138
175
filter_string : Optional [str ],
139
176
filter_id : Optional [Union [str , UUID ]],
140
177
) -> "XmlCommand" :
178
+ """
179
+ Add a filter to the command.
180
+
181
+ Args:
182
+ filter_string: The filter string to be added.
183
+ filter_id: The filter ID to be added.
184
+ """
141
185
if filter_string :
142
186
self .set_attribute ("filter" , filter_string )
143
187
0 commit comments