From 4180cc362e63e49c566a8edd178ec21fa68a2875 Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 11 Jul 2016 13:22:58 +0200 Subject: [PATCH] code review --- fail2ban/client/fail2banclient.py | 12 ++++++++---- fail2ban/client/fail2bancmdline.py | 5 +---- fail2ban/protocol.py | 3 +++ fail2ban/server/server.py | 2 +- fail2ban/tests/utils.py | 7 +++---- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/fail2ban/client/fail2banclient.py b/fail2ban/client/fail2banclient.py index a8e0a331..e3797279 100755 --- a/fail2ban/client/fail2banclient.py +++ b/fail2ban/client/fail2banclient.py @@ -411,11 +411,9 @@ class Fail2banClient(Fail2banCmdLine, Thread): signal.signal(s, sh) -## -# Wonderful visual :) -# - class _VisualWait: + """Small progress indication (as "wonderful visual") during waiting process + """ pos = 0 delta = 1 def __init__(self, maxpos=10): @@ -427,6 +425,8 @@ class _VisualWait: sys.stdout.write('\r'+(' '*(35+self.maxpos))+'\r') sys.stdout.flush() def heartbeat(self): + """Show or step for progress indicator + """ if not self.pos: sys.stdout.write("\nINFO [#" + (' '*self.maxpos) + "] Waiting on the server...\r\x1b[8C") self.pos += self.delta @@ -441,6 +441,8 @@ class _VisualWait: elif self.pos < 2: self.delta = 1 class _NotVisualWait: + """Mockup for invisible progress indication (not verbose) + """ def __enter__(self): return self def __exit__(self, *args): @@ -449,6 +451,8 @@ class _NotVisualWait: pass def VisualWait(verbose, *args, **kwargs): + """Wonderful visual progress indication (if verbose) + """ return _VisualWait(*args, **kwargs) if verbose > 1 else _NotVisualWait() diff --git a/fail2ban/client/fail2bancmdline.py b/fail2ban/client/fail2bancmdline.py index 83379a9a..9110a2b8 100644 --- a/fail2ban/client/fail2bancmdline.py +++ b/fail2ban/client/fail2bancmdline.py @@ -83,9 +83,6 @@ class Fail2banCmdLine(): output("Copyright (c) 2004-2008 Cyril Jaquier, 2008- Fail2Ban Contributors") output("Copyright of modifications held by their respective authors.") output("Licensed under the GNU General Public License v2 (GPL).") - output("") - output("Written by Cyril Jaquier .") - output("Many contributions by Yaroslav O. Halchenko .") def dispUsage(self): """ Prints Fail2Ban command line options and exits @@ -262,7 +259,7 @@ class Fail2banCmdLine(): @staticmethod def exit(code=0): # pragma: no cover - can't test logSys.debug("Exit with code %s", code) - if os._exit: + if hasattr(os, '_exit') and os._exit: os._exit(code) else: sys.exit(code) diff --git a/fail2ban/protocol.py b/fail2ban/protocol.py index 648666a1..d671f3c3 100644 --- a/fail2ban/protocol.py +++ b/fail2ban/protocol.py @@ -27,6 +27,9 @@ __license__ = "GPL" import textwrap def output(s): + """Default output handler for printing protocol. + Used to ease mocking in the test cases. + """ print(s) ## diff --git a/fail2ban/server/server.py b/fail2ban/server/server.py index 4a2a7c11..bade12cb 100644 --- a/fail2ban/server/server.py +++ b/fail2ban/server/server.py @@ -419,7 +419,7 @@ class Server: getLogger("fail2ban").setLevel(getattr(logging, value)) self.__logLevel = value except AttributeError: - raise ValueError("Invalid log level") + raise ValueError("Invalid log level %r" % value) ## # Get the logging level. diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index 2d5a38f7..a06fbef0 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -54,10 +54,9 @@ if not CONFIG_DIR: else: CONFIG_DIR = '/etc/fail2ban' -# In not installed env (setup, test-cases) use fail2ban modules from main directory: -if 1 or os.environ.get('PYTHONPATH', None) is None: - os.putenv('PYTHONPATH', os.path.dirname(os.path.dirname(os.path.dirname( - os.path.abspath(__file__))))) +# During the test cases (or setup) use fail2ban modules from main directory: +os.putenv('PYTHONPATH', os.path.dirname(os.path.dirname(os.path.dirname( + os.path.abspath(__file__))))) class F2B(optparse.Values): def __init__(self, opts={}):