allow comments in file with ip-set: text followed # or ; chars after space or newline would be ignored

pull/3955/head
sebres 2025-03-03 19:00:09 +01:00
parent bdae15b522
commit d684339edd
3 changed files with 27 additions and 4 deletions

View File

@ -282,7 +282,18 @@ def excepthook(exctype, value, traceback):
"Unhandled exception in Fail2Ban:", exc_info=True)
return sys.__excepthook__(exctype, value, traceback)
def splitwords(s):
RE_REM_COMMENTS = re.compile(r'(?m)(?:^|\s)[\#;].*')
def removeComments(s):
"""Helper to remove comments:
# comment ...
; comment ...
no comment # comment ...
no comment ; comment ...
"""
return RE_REM_COMMENTS.sub('', s)
RE_SPLT_WORDS = re.compile(r'[\s,]+')
def splitwords(s, ignoreComments=False):
"""Helper to split words on any comma, space, or a new line
Returns empty list if input is empty (or None) and filters
@ -290,7 +301,9 @@ def splitwords(s):
"""
if not s:
return []
return list(filter(bool, [v.strip() for v in re.split(r'[\s,]+', s)]))
if ignoreComments:
s = removeComments(s)
return list(filter(bool, [v.strip() for v in RE_SPLT_WORDS.split(s)]))
def _merge_dicts(x, y):
"""Helper to merge dicts.

View File

@ -778,7 +778,7 @@ class FileIPAddrSet(IPAddrSet):
self._fileStats = stats
with open(self.fileName, 'r') as f:
ips = f.read()
ips = splitwords(ips)
ips = splitwords(ips, ignoreComments=True)
self.set(ips)
except Exception as e: # pragma: no cover
if not noError: raise e

View File

@ -34,7 +34,7 @@ from io import StringIO
from .utils import LogCaptureTestCase, logSys as DefLogSys
from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger, \
getVerbosityFormat, splitwords, uni_decode, uni_string
getVerbosityFormat, removeComments, splitwords, uni_decode, uni_string
from ..server.mytime import MyTime
@ -68,6 +68,16 @@ class HelpersTest(unittest.TestCase):
self.assertEqual(splitwords(' 1\n 2, 3'), ['1', '2', '3'])
self.assertEqual(splitwords('\t1\t 2,\r\n 3\n'), ['1', '2', '3']); # other spaces
def testSplitNoComments(self):
s = '''
# comment ...
; comment ...
line1 A # comment ...
line2 B ; comment ...
'''
self.assertEqual(splitwords(s, ignoreComments=True), ['line1', 'A', 'line2', 'B'])
self.assertEqual(splitwords(removeComments(s)), ['line1', 'A', 'line2', 'B'])
def _sh_call(cmd):
import subprocess