Skip to content

Commit

Permalink
fix: fixed pylint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
savon-noir committed Jan 27, 2025
1 parent 35f4918 commit f4af056
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ repos:
rev: 3.8.4
hooks:
- id: flake8
exclude: ^libnmap/(test/|docs/|examples/)
4 changes: 2 additions & 2 deletions libnmap/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from libnmap.objects.host import NmapHost
from libnmap.objects.report import NmapReport
from libnmap.objects.service import NmapService, NmapExtraPort
from libnmap.objects.service import NmapExtraPort, NmapService

__all__ = ['NmapReport', 'NmapHost', 'NmapService', 'NmapExtraPort']
__all__ = ["NmapReport", "NmapHost", "NmapService", "NmapExtraPort"]
2 changes: 1 addition & 1 deletion libnmap/objects/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
self._status = status if status is not None else {}
self._services = services if services is not None else []
self._extras = extras if extras is not None else {}
self._extraports = self._extras.get('extraports', None)
self._extraports = self._extras.get("extraports", None)
self._osfingerprinted = False
self.os = None
if "os" in self._extras:
Expand Down
37 changes: 19 additions & 18 deletions libnmap/objects/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,47 +370,48 @@ def diff(self, other):

class NmapExtraPort(object):
"""
NmapExtraPort is an object which documents unlisted ports/services
which are possibly closed, filtered, ignored,...
NmapExtraPort is an object which documents unlisted ports/services
which are possibly closed, filtered, ignored,...
"""

def __init__(self, xdict):
"""
Constructor
:param xdict: python dict containing the following structure:
{
'state': <str>,
'count': <int>,
'reasons': [{'reason': <str>, 'count' <int>}]
}
Constructor
:param xdict: python dict containing the following structure:
{
'state': <str>,
'count': <int>,
'reasons': [{'reason': <str>, 'count' <int>}]
}
"""
self._count = xdict.get('count', 0)
self._state = xdict.get('state', 'unknown')
self._reasons = xdict.get('reasons', [])
self._count = xdict.get("count", 0)
self._state = xdict.get("state", "unknown")
self._reasons = xdict.get("reasons", [])

@property
def extra_count(self):
"""
Accessor for the number of extraports
Accessor for the number of extraports
:return: int
:return: int
"""
return int(self._count)

@property
def extra_state(self):
"""
Accessor for the state of extraports listed
Accessor for the state of extraports listed
:return: string
:return: string
"""
return self._state

@property
def extra_reasons(self):
"""
Return the first reason available for the extraport listed.
Return the first reason available for the extraport listed.
:return: dict, empty if no extraports reason available
:return: dict, empty if no extraports reason available
"""

return self._reasons
10 changes: 6 additions & 4 deletions libnmap/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET

from xml.etree.ElementTree import iselement as et_iselement
from libnmap.objects import NmapHost, NmapService, NmapReport, NmapExtraPort

from libnmap.objects import NmapExtraPort, NmapHost, NmapReport, NmapService


class NmapParser(object):
Expand Down Expand Up @@ -383,14 +385,14 @@ def _parse_xml_ports(cls, scanports_data):

xelement = cls.__format_element(scanports_data)

rdict = {'ports': [], 'extraports': []}
rdict = {"ports": [], "extraports": []}
for xservice in xelement:
if xservice.tag == "port":
nport = cls._parse_xml_port(xservice)
rdict["ports"].append(nport)
elif xservice.tag == "extraports":
extraports = cls.__parse_extraports(xservice)
rdict['extraports'].append(extraports)
rdict["extraports"].append(extraports)
return rdict

@classmethod
Expand Down Expand Up @@ -478,7 +480,7 @@ def __parse_extraports(cls, extraports_data):
for xelt in xelement:
if xelt.tag == "extrareasons":
extrareasons_dict = cls.__format_attributes(xelt)
rdict['reasons'].append(extrareasons_dict)
rdict["reasons"].append(extrareasons_dict)
robj = NmapExtraPort(rdict)
return robj

Expand Down
12 changes: 9 additions & 3 deletions libnmap/test/test_extraports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
class TestExtraPorts(unittest.TestCase):
def setUp(self):
fdir = os.path.dirname(os.path.realpath(__file__))
_extrareason = [{'reason': 'filtered', 'count': '3'}, {'reason': 'resets', 'count': '7'}]
_extrareason = [
{"reason": "filtered", "count": "3"},
{"reason": "resets", "count": "7"},
]
self.flist = [
{"path": "%s/%s" % (fdir, "files/extra_ports.xml"), "extrareason": _extrareason}
{
"path": "%s/%s" % (fdir, "files/extra_ports.xml"),
"extrareason": _extrareason,
}
]

def test_extraports(self):
Expand All @@ -21,7 +27,7 @@ def test_extraports(self):
ep_list = rep1.hosts[0].extraports
self.assertEqual(len(ep_list), 2)
self.assertEqual(ep_list[0].extra_count, 65509)
self.assertEqual(ep_list[0].extra_state, 'closed')
self.assertEqual(ep_list[0].extra_state, "closed")
self.assertEqual(len(ep_list[0].extra_reasons), 1)
self.assertEqual(ep_list[1].extra_count, 10)
self.assertEqual(len(ep_list[1].extra_reasons), 2)
Expand Down

0 comments on commit f4af056

Please sign in to comment.