Skip to content

Commit

Permalink
Merge pull request #1439 from yarikoptic/enh-ignoreipsplit:
Browse files Browse the repository at this point in the history
ENH: splitcommaspace -> splitwords allow to split ignoreip entries with new lines
  • Loading branch information
sebres committed May 23, 2016
2 parents b56f4c5 + 156065e commit 9df7973
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions fail2ban/client/jailreader.py
Expand Up @@ -34,7 +34,7 @@
from .actionreader import ActionReader
from ..version import version
from ..helpers import getLogger
from ..helpers import splitcommaspace
from ..helpers import splitwords

# Gets the instance of the logger.
logSys = getLogger(__name__)
Expand Down Expand Up @@ -214,7 +214,7 @@ def convert(self, allow_no_files=False):
elif opt == "maxretry":
stream.append(["set", self.__name, "maxretry", self.__opts[opt]])
elif opt == "ignoreip":
for ip in splitcommaspace(self.__opts[opt]):
for ip in splitwords(self.__opts[opt]):
stream.append(["set", self.__name, "addignoreip", ip])
elif opt == "findtime":
stream.append(["set", self.__name, "findtime", self.__opts[opt]])
Expand Down
6 changes: 3 additions & 3 deletions fail2ban/helpers.py
Expand Up @@ -128,12 +128,12 @@ def excepthook(exctype, value, traceback):
"Unhandled exception in Fail2Ban:", exc_info=True)
return sys.__excepthook__(exctype, value, traceback)

def splitcommaspace(s):
"""Helper to split on any comma or space
def splitwords(s):
"""Helper to split words on any comma, space, or a new line
Returns empty list if input is empty (or None) and filters
out empty entries
"""
if not s:
return []
return filter(bool, re.split('[ ,]', s))
return filter(bool, map(str.strip, re.split('[ ,\n]+', s)))
18 changes: 10 additions & 8 deletions fail2ban/tests/misctestcase.py
Expand Up @@ -33,7 +33,7 @@
from StringIO import StringIO

from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger
from ..helpers import splitcommaspace
from ..helpers import splitwords
from ..server.datetemplate import DatePatternRegex


Expand All @@ -56,13 +56,15 @@ def testFormatExceptionConvertArgs(self):
# might be fragile due to ' vs "
self.assertEqual(args, "('Very bad', None)")

def testsplitcommaspace(self):
self.assertEqual(splitcommaspace(None), [])
self.assertEqual(splitcommaspace(''), [])
self.assertEqual(splitcommaspace(' '), [])
self.assertEqual(splitcommaspace('1'), ['1'])
self.assertEqual(splitcommaspace(' 1 2 '), ['1', '2'])
self.assertEqual(splitcommaspace(' 1, 2 , '), ['1', '2'])
def testsplitwords(self):
self.assertEqual(splitwords(None), [])
self.assertEqual(splitwords(''), [])
self.assertEqual(splitwords(' '), [])
self.assertEqual(splitwords('1'), ['1'])
self.assertEqual(splitwords(' 1 2 '), ['1', '2'])
self.assertEqual(splitwords(' 1, 2 , '), ['1', '2'])
self.assertEqual(splitwords(' 1\n 2'), ['1', '2'])
self.assertEqual(splitwords(' 1\n 2, 3'), ['1', '2', '3'])


class SetupTest(unittest.TestCase):
Expand Down

0 comments on commit 9df7973

Please sign in to comment.