Skip to content

Commit

Permalink
BUG: lib/bot: ParserBot: mark internal variables correctly
Browse files Browse the repository at this point in the history
the internal variable handle and current_line were not prefixed with an
underscore, so they were detected as parameters.
  • Loading branch information
Sebastian Wagner authored and Wagner committed Jun 23, 2021
1 parent d336dc0 commit 36d7940
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion intelmq/bots/parsers/zoneh/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def parse_line(self, row, report):
event.add('classification.type', 'unauthorised-information-modification')
event.add('event_description.text', 'defacement')
event.add('time.source', row["add_date"] + ' UTC')
event.add('raw', self.recover_line(self.current_line))
event.add('raw', self.recover_line())
event.add('source.ip', row["ip_address"], raise_failure=False)
event.add('source.fqdn', parsed_url.netloc, raise_failure=False)
event.add('source.geolocation.cc', row["country_code"],
Expand Down
36 changes: 18 additions & 18 deletions intelmq/lib/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,8 @@ def _create_argparser(cls):
class ParserBot(Bot):
_csv_params = {}
_ignore_lines_starting = []
handle = None
current_line = None
_handle = None
_current_line = None

def __init__(self, bot_id: str, start: bool = False, sighup_event=None,
disable_multithreading: bool = None):
Expand All @@ -947,9 +947,9 @@ def parse_csv(self, report: libmessage.Report):
raw_report = '\n'.join([line for line in raw_report.splitlines()
if not any([line.startswith(prefix) for prefix
in self._ignore_lines_starting])])
self.handle = RewindableFileHandle(io.StringIO(raw_report))
for line in csv.reader(self.handle, **self._csv_params):
self.current_line = self.handle.current_line
self._handle = RewindableFileHandle(io.StringIO(raw_report))
for line in csv.reader(self._handle, **self._csv_params):
self._current_line = self._handle.current_line
yield line

def parse_csv_dict(self, report: libmessage.Report):
Expand All @@ -962,15 +962,15 @@ def parse_csv_dict(self, report: libmessage.Report):
raw_report = '\n'.join([line for line in raw_report.splitlines()
if not any([line.startswith(prefix) for prefix
in self._ignore_lines_starting])])
self.handle = RewindableFileHandle(io.StringIO(raw_report))
self._handle = RewindableFileHandle(io.StringIO(raw_report))

csv_reader = csv.DictReader(self.handle, **self._csv_params)
csv_reader = csv.DictReader(self._handle, **self._csv_params)
# create an array of fieldnames,
# those were automagically created by the dictreader
self.csv_fieldnames = csv_reader.fieldnames

for line in csv_reader:
self.current_line = self.handle.current_line
self._current_line = self._handle.current_line
yield line

def parse_json(self, report: libmessage.Report):
Expand All @@ -987,7 +987,7 @@ def parse_json_stream(self, report: libmessage.Report):
"""
raw_report: str = utils.base64_decode(report.get("raw"))
for line in raw_report.splitlines():
self.current_line = line
self._current_line = line
yield json.loads(line)

def parse(self, report: libmessage.Report):
Expand Down Expand Up @@ -1077,14 +1077,14 @@ def recover_line(self, line: Optional[str] = None) -> str:
----------
line : Optional[str], optional
The currently process line which should be transferred into it's
original appearance. As fallback, "self.current_line" is used if
original appearance. As fallback, "self._current_line" is used if
available (depending on self.parse).
The default is None.
Raises
------
ValueError
If neither the parameter "line" nor the member "self.current_line"
If neither the parameter "line" nor the member "self._current_line"
is available.
Returns
Expand All @@ -1093,14 +1093,14 @@ def recover_line(self, line: Optional[str] = None) -> str:
The reconstructed raw data.
"""
if self.handle and self.handle.first_line and not self.tempdata:
tempdata = [self.handle.first_line.strip()]
if self._handle and self._handle.first_line and not self.tempdata:
tempdata = [self._handle.first_line.strip()]
else:
tempdata = self.tempdata
if not line and not self.current_line:
if not line and not self._current_line:
raise ValueError('Parameter "line" is not given and '
'"self.current_line" is also None. Please give one of them.')
line = line if line else self.current_line
'"self._current_line" is also None. Please give one of them.')
line = line if line else self._current_line
return '\n'.join(tempdata + [line])

def recover_line_csv(self, line: str) -> str:
Expand All @@ -1117,7 +1117,7 @@ def recover_line_csv_dict(self, line: str) -> str:
out = io.StringIO()
writer = csv.DictWriter(out, self.csv_fieldnames, **self._csv_params)
writer.writeheader()
out.write(self.current_line)
out.write(self._current_line)

return out.getvalue().strip()

Expand All @@ -1142,7 +1142,7 @@ def recover_line_json_stream(self, line=None) -> str:
str
unparsed JSON line.
"""
return self.current_line
return self._current_line


class CollectorBot(Bot):
Expand Down

0 comments on commit 36d7940

Please sign in to comment.