Skip to content

Commit 1abccd6

Browse files
committed
Finnaly NmapRepport.save() work + backend.get/getall() seems to work need to write test
modified: ../.gitignore modified: plugins/mongodb.py
1 parent 1fee4d4 commit 1abccd6

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

.gitignore

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1-
*.pyc
1+
*.py[cod]
2+
*.swp
23
*~
3-
build*
4+
*.lock
5+
*.DS_Store
6+
*.swp
7+
*.out
8+
*.sqlite3
9+
*~
10+
11+
# Packages
12+
*.egg
13+
*.egg-info
14+
dist
15+
build
16+
eggs
17+
parts
18+
bin
19+
var
20+
sdist
21+
develop-eggs
22+
.installed.cfg
23+
lib
24+
lib64
25+
26+
# Installer logs
27+
pip-log.txt
28+
29+
# Unit test / coverage reports
30+
.coverage
31+
.tox
32+
nosetests.xml
33+
34+
# Translations
35+
*.mo
36+
37+
# Mr Developer
38+
.mr.developer.cfg
39+
.project
40+
.pydevproject
41+
.swp

libnmap/plugins/mongodb.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from pymongo import MongoClient
44
from bson.objectid import ObjectId
55

6-
from libnmap.reportjson import ReportDecoder, ReportEncoder
6+
from libnmap.reportjson import ReportEncoder
7+
from libnmap.parser import NmapParser
78
from libnmap.plugins.backendplugin import NmapBackendPlugin
89

910

@@ -27,16 +28,36 @@ def insert(self, report):
2728
raise
2829
return id
2930

30-
def get(self, report_id=None):
31-
rid = report_id
32-
if report_id is not None and isinstance(report_id, str):
33-
rid = ObjectId(report_id)
31+
def get(self, str_report_id=None):
32+
"""get return a NmapReport object
33+
"""
34+
rid = str_report_id
35+
nmapreport = None
36+
if str_report_id is not None and isinstance(str_report_id, str):
37+
rid = ObjectId(str_report_id)
3438

3539
if isinstance(rid, ObjectId):
40+
#get a specific report by mongo's id
3641
r = self.collection.find({'_id': rid})
37-
else:
38-
r = self.collection.find()
39-
return r
42+
if r is not None:
43+
#search by id means only one in the iterator
44+
record = r[0]
45+
#remove mongo's id
46+
del record['_id']
47+
nmapreport = NmapParser.parse_fromdict(record)
48+
return nmapreport
49+
50+
def getall(self, dict_filter=None):
51+
"""return a list of all NmapReport saved in the backend
52+
TODO : add a filter capability
53+
"""
54+
nmapreportList = []
55+
r = self.collection.find()
56+
for report in r:
57+
del report['_id']
58+
nmapreport = NmapParser.parse_fromdict(report)
59+
nmapreportList.append(nmapreport)
60+
return nmapreportList
4061

4162
def delete(self, report_id=None):
4263
if report_id is not None and isinstance(report_id, str):

0 commit comments

Comments
 (0)