Skip to content

Commit

Permalink
Change: Improve type hints for XmlCommand and XmlCommandElement
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks authored and greenbonebot committed Jun 14, 2024
1 parent c35257b commit fb562e3
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions gvm/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def parse_xml(xml: AnyStr) -> Element:


class XmlCommandElement:
def __init__(self, element):
def __init__(self, element: Element):
self._element = element

def add_element(
Expand All @@ -44,15 +44,16 @@ def add_element(
text: Optional[str] = None,
*,
attrs: Optional[dict] = None,
):
) -> "XmlCommandElement":
node = SubElement(self._element, name, attrib=attrs)
node.text = text
return XmlCommandElement(node)

def set_attribute(self, name: str, value: str):
def set_attribute(self, name: str, value: str) -> "XmlCommandElement":
self._element.set(name, value)
return self

def set_attributes(self, attrs: dict):
def set_attributes(self, attrs: dict[str, str]) -> "XmlCommandElement":
"""Set several attributes at once.
Arguments:
Expand All @@ -61,20 +62,22 @@ def set_attributes(self, attrs: dict):
for key, value in attrs.items():
self._element.set(key, value)

def append_xml_str(self, xml_text: str):
return self

def append_xml_str(self, xml_text: str) -> None:
"""Append a xml element in string format."""
node = secET.fromstring(xml_text)
self._element.append(node)

def to_string(self) -> str:
return xmltostring(self._element).decode("utf-8")

def __str__(self):
def __str__(self) -> str:
return self.to_string()


class XmlCommand(XmlCommandElement):
def __init__(self, name):
def __init__(self, name: str) -> None:
super().__init__(create_element(name))


Expand Down

0 comments on commit fb562e3

Please sign in to comment.