Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Improve generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Feb 11, 2020
1 parent ab9f88b commit 670ab0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
26 changes: 8 additions & 18 deletions ospd/command/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,33 +308,23 @@ def handle_xml(self, xml: Element) -> str:

if vt_id and vt_id not in self._daemon.vts:
text = "Failed to find vulnerability test '{0}'".format(vt_id)
raise OspdCommandError(text, 'get_vts', 404)

filtered_vts = None
if vt_filter:
filtered_vts = self._daemon.vts_filter.get_filtered_vts_list(
self._daemon.vts, vt_filter
)

# Generator
vts_list = (vt for vt in self._daemon.get_vts_xml(vt_id, filtered_vts))

# List of xml pieces with the generator to be iterated
response = [
xml_helper.create_response('get_vts'),
xml_helper.create_element('vts'),
vts_list,
xml_helper.create_element('vts', end=True),
xml_helper.create_response('get_vts', end=True),
]
yield xml_helper.create_response('get_vts')
yield xml_helper.create_element('vts')

for elem in response:
if isinstance(elem, GeneratorType):
for vts_chunk in elem:
yield xml_helper.add_element(
self._daemon.get_vt_xml(vts_chunk)
)
else:
yield elem
for vt in self._daemon.get_vts_selection_list(vt_id, filtered_vts):
yield xml_helper.add_element(self._daemon.get_vt_xml(vt))

yield xml_helper.create_element('vts', end=True)
yield xml_helper.create_response('get_vts', end=True)


class StopScan(BaseCommand):
Expand Down
11 changes: 6 additions & 5 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,11 @@ def get_vt_xml(self, vt_id: str):

return vt_xml

def get_vts_xml(self, vt_id: str = None, filtered_vts: Dict = None):
""" Python Generator for VTS.
Gets collection of vulnerability test information in XML format.
def get_vts_selection_list(
self, vt_id: str = None, filtered_vts: Dict = None
) -> List:
"""
Get list of VT's OID.
If vt_id is specified, the collection will contain only this vt, if
found.
If no vt_id is specified or filtered_vts is None (default), the
Expand All @@ -1157,8 +1159,7 @@ def get_vts_xml(self, vt_id: str = None, filtered_vts: Dict = None):
filtered_vts (list, optional): Filtered VTs collection.
Return:
String of collection of vulnerability test information in
XML format.
List of selected VT's OID.
"""
vts_xml = []
if not self.vts:
Expand Down

0 comments on commit 670ab0a

Please sign in to comment.