Skip to content

Commit

Permalink
🎨 improve to_csv_file
Browse files Browse the repository at this point in the history
  • Loading branch information
zrquan committed Nov 1, 2024
1 parent 99832cc commit 03c04d5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions nmass/model/elements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csv
from io import BufferedWriter
from typing import Literal, Optional
from typing import Literal, Optional, Union
from urllib.request import urlopen

import lxml.etree as ET
Expand Down Expand Up @@ -212,8 +212,13 @@ def to_html(self, xslt_path: str = "https://nmap.org/svn/docs/nmap.xsl", pretty_
newdom = transform(self.to_xml_tree()) # type: ignore
return ET.tostring(newdom, pretty_print=pretty_print).decode()

def to_csv_file(self, file: BufferedWriter) -> None:
writer = csv.writer(file) # type: ignore
def to_csv_file(self, file: BufferedWriter, dialect: Union[str, csv.Dialect] = "excel") -> None:
"""Write information to a CSV file.
:param file: A file-like object where the CSV data will be written
:param dialect: The CSV dialect used for formatting the file, defaults to "excel"
"""
writer = csv.writer(file, dialect=dialect) # type: ignore
writer.writerow(["IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "CPE"])
for host in self.hosts:
host_info = host.address[0].addr
Expand Down

0 comments on commit 03c04d5

Please sign in to comment.