Skip to content

Commit 07caf94

Browse files
committed
Improve docstrings of XmlCommand and XmlCommandElement
1 parent 9bad15e commit 07caf94

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

gvm/xml.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ def parse_xml(xml: AnyStr) -> Element:
8282

8383

8484
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+
8591
def __init__(self, element: Element):
8692
self._element = element
8793

@@ -97,14 +103,20 @@ def add_element(
97103
return XmlCommandElement(node)
98104

99105
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+
"""
100112
self._element.set(name, value)
101113
return self
102114

103115
def set_attributes(self, attrs: dict[str, str]) -> "XmlCommandElement":
104116
"""Set several attributes at once.
105117
106-
Arguments:
107-
attrs (dict): Attributes to be set on the element
118+
Args:
119+
attrs: Attributes to be set on the element
108120
"""
109121
for key, value in attrs.items():
110122
self._element.set(key, value)
@@ -116,28 +128,60 @@ def append_xml_str(self, xml_text: str) -> None:
116128
node = parse_xml(xml_text)
117129
self._element.append(node)
118130

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)
121139

122140
def to_bytes(self) -> bytes:
141+
"""
142+
Convert the XML element to a bytes object
143+
"""
123144
return xml_to_string(self._element)
124145

125146
def __str__(self) -> str:
147+
"""
148+
Convert the XML element to a string using the default encoding.
149+
"""
126150
return self.to_string()
127151

128152
def __bytes__(self) -> bytes:
153+
"""
154+
Convert the XML element to a bytes object
155+
"""
129156
return self.to_bytes()
130157

131158

132159
class XmlCommand(XmlCommandElement):
160+
"""
161+
Class to create XML commands
162+
"""
163+
133164
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+
"""
134171
super().__init__(create_element(name))
135172

136173
def add_filter(
137174
self,
138175
filter_string: Optional[str],
139176
filter_id: Optional[Union[str, UUID]],
140177
) -> "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+
"""
141185
if filter_string:
142186
self.set_attribute("filter", filter_string)
143187

0 commit comments

Comments
 (0)