meantime commit: code review, simplification, pythonization, etc. (test cases passed)

pull/1414/head
sebres 2016-05-10 22:09:14 +02:00
parent 50e5a7e538
commit 9da7bb1068
3 changed files with 30 additions and 23 deletions

View File

@ -89,45 +89,45 @@ class Beautifier:
msg = "\n".join(msg) msg = "\n".join(msg)
elif inC[1] == "syslogsocket": elif inC[1] == "syslogsocket":
msg = "Current syslog socket is:\n" msg = "Current syslog socket is:\n"
msg = msg + "`- " + response msg += "`- " + response
elif inC[1] == "logtarget": elif inC[1] == "logtarget":
msg = "Current logging target is:\n" msg = "Current logging target is:\n"
msg = msg + "`- " + response msg += "`- " + response
elif inC[1:2] == ['loglevel']: elif inC[1:2] == ['loglevel']:
msg = "Current logging level is " msg = "Current logging level is "
if response == 1: if response == 1:
msg = msg + "ERROR" msg += "ERROR"
elif response == 2: elif response == 2:
msg = msg + "WARN" msg += "WARN"
elif response == 3: elif response == 3:
msg = msg + "INFO" msg += "INFO"
elif response == 4: elif response == 4:
msg = msg + "DEBUG" msg += "DEBUG"
else: else:
msg = msg + repr(response) msg += repr(response)
elif inC[1] == "dbfile": elif inC[1] == "dbfile":
if response is None: if response is None:
msg = "Database currently disabled" msg = "Database currently disabled"
else: else:
msg = "Current database file is:\n" msg = "Current database file is:\n"
msg = msg + "`- " + response msg += "`- " + response
elif inC[1] == "dbpurgeage": elif inC[1] == "dbpurgeage":
if response is None: if response is None:
msg = "Database currently disabled" msg = "Database currently disabled"
else: else:
msg = "Current database purge age is:\n" msg = "Current database purge age is:\n"
msg = msg + "`- %iseconds" % response msg += "`- %iseconds" % response
elif inC[2] in ("logpath", "addlogpath", "dellogpath"): elif inC[2] in ("logpath", "addlogpath", "dellogpath"):
if len(response) == 0: if len(response) == 0:
msg = "No file is currently monitored" msg = "No file is currently monitored"
else: else:
msg = "Current monitored log file(s):\n" msg = "Current monitored log file(s):\n"
for path in response[:-1]: for path in response[:-1]:
msg = msg + "|- " + path + "\n" msg += "|- " + path + "\n"
msg = msg + "`- " + response[len(response)-1] msg += "`- " + response[-1]
elif inC[2] == "logencoding": elif inC[2] == "logencoding":
msg = "Current log encoding is set to:\n" msg = "Current log encoding is set to:\n"
msg = msg + response msg += response
elif inC[2] in ("journalmatch", "addjournalmatch", "deljournalmatch"): elif inC[2] in ("journalmatch", "addjournalmatch", "deljournalmatch"):
if len(response) == 0: if len(response) == 0:
msg = "No journal match filter set" msg = "No journal match filter set"
@ -137,19 +137,19 @@ class Beautifier:
elif inC[2] == "datepattern": elif inC[2] == "datepattern":
msg = "Current date pattern set to: " msg = "Current date pattern set to: "
if response is None: if response is None:
msg = msg + "Not set/required" msg += "Not set/required"
elif response[0] is None: elif response[0] is None:
msg = msg + "%s" % response[1] msg += "%s" % response[1]
else: else:
msg = msg + "%s (%s)" % response msg += "%s (%s)" % response
elif inC[2] in ("ignoreip", "addignoreip", "delignoreip"): elif inC[2] in ("ignoreip", "addignoreip", "delignoreip"):
if len(response) == 0: if len(response) == 0:
msg = "No IP address/network is ignored" msg = "No IP address/network is ignored"
else: else:
msg = "These IP addresses/networks are ignored:\n" msg = "These IP addresses/networks are ignored:\n"
for ip in response[:-1]: for ip in response[:-1]:
msg = msg + "|- " + ip + "\n" msg += "|- " + ip + "\n"
msg = msg + "`- " + response[len(response)-1] msg += "`- " + response[-1]
elif inC[2] in ("failregex", "addfailregex", "delfailregex", elif inC[2] in ("failregex", "addfailregex", "delfailregex",
"ignoreregex", "addignoreregex", "delignoreregex"): "ignoreregex", "addignoreregex", "delignoreregex"):
if len(response) == 0: if len(response) == 0:
@ -157,10 +157,10 @@ class Beautifier:
else: else:
msg = "The following regular expression are defined:\n" msg = "The following regular expression are defined:\n"
c = 0 c = 0
for ip in response[:-1]: for l in response[:-1]:
msg = msg + "|- [" + str(c) + "]: " + ip + "\n" msg += "|- [" + str(c) + "]: " + l + "\n"
c += 1 c += 1
msg = msg + "`- [" + str(c) + "]: " + response[len(response)-1] msg += "`- [" + str(c) + "]: " + response[-1]
elif inC[2] == "actions": elif inC[2] == "actions":
if len(response) == 0: if len(response) == 0:
msg = "No actions for jail %s" % inC[1] msg = "No actions for jail %s" % inC[1]
@ -187,7 +187,7 @@ class Beautifier:
logSys.warning("Beautifier error. Please report the error") logSys.warning("Beautifier error. Please report the error")
logSys.error("Beautify " + repr(response) + " with " logSys.error("Beautify " + repr(response) + " with "
+ repr(self.__inputCmd) + " failed") + repr(self.__inputCmd) + " failed")
msg = msg + repr(response) msg += repr(response)
return msg return msg
def beautifyError(self, response): def beautifyError(self, response):

View File

@ -201,8 +201,10 @@ class BeautifierTest(unittest.TestCase):
IPAddr("::ffff:10.0.2.1") IPAddr("::ffff:10.0.2.1")
] ]
output = "These IP addresses/networks are ignored:\n" output = "These IP addresses/networks are ignored:\n"
output += "|- 127.0.0.0/8\n|- ::1\n" output += "|- 127.0.0.0/8\n"
output += "|- 2001:db8::/32\n`- 10.0.2.1" output += "|- ::1\n"
output += "|- 2001:db8::/32\n"
output += "`- 10.0.2.1"
self.assertEqual(self.b.beautify(response), output) self.assertEqual(self.b.beautify(response), output)
def testFailRegex(self): def testFailRegex(self):

View File

@ -1457,6 +1457,11 @@ class DNSUtilsNetworkTests(unittest.TestCase):
self.assertEqual(str(IPAddr('2606:2800:220:1:248:1893:25c8:0/120')), '2606:2800:220:1:248:1893:25c8:0/120') self.assertEqual(str(IPAddr('2606:2800:220:1:248:1893:25c8:0/120')), '2606:2800:220:1:248:1893:25c8:0/120')
self.assertEqual(IPAddr('2606:2800:220:1:248:1893:25c8:0/120').ntoa, '2606:2800:220:1:248:1893:25c8:0/120') self.assertEqual(IPAddr('2606:2800:220:1:248:1893:25c8:0/120').ntoa, '2606:2800:220:1:248:1893:25c8:0/120')
def testIPAddr_CIDR_Repr(self):
self.assertEqual(["127.0.0.0/8", "::/32", "2001:db8::/32"],
[IPAddr("127.0.0.0", 8), IPAddr("::1", 32), IPAddr("2001:db8::", 32)]
)
def testIPAddr_CompareDNS(self): def testIPAddr_CompareDNS(self):
ips = IPAddr('example.com') ips = IPAddr('example.com')
self.assertTrue(IPAddr("93.184.216.34").isInNet(ips)) self.assertTrue(IPAddr("93.184.216.34").isInNet(ips))