diff --git a/test/basetest/hooks.py b/test/basetest/hooks.py index 45f8edb..88e0aae 100644 --- a/test/basetest/hooks.py +++ b/test/basetest/hooks.py @@ -1,22 +1,12 @@ -from __future__ import division - -import errno import os -import shutil import stat -from sys import stderr - -try: - import simplejson as json -except ImportError: - import json +import shutil +import json -from datetime import datetime -from .utils import DEFAULT_HOOK_PATH -from .exceptions import HookError +from test.basetest.exceptions import HookError -class InvalidJSON(object): +class InvalidJSON: """ Object representing the original unparsed JSON string and the JSON error """ @@ -30,15 +20,12 @@ def json_decoder(string): Attempt to decode a JSON string and in case of error return an InvalidJSON object """ - decoder = json.JSONDecoder().decode - try: - return decoder(string) - except ValueError as e: + return json.loads(string) + except json.JSONDecodeError as e: return InvalidJSON(string, str(e)) - -class Hooks(object): +class Hooks: """ Abstraction to help interact with hooks (add, remove) during tests and keep track of which are active. @@ -153,13 +140,13 @@ def clear(self): shutil.rmtree(self.hookdir) except OSError as e: # If the hookdir folder doesn't exist, no harm done and keep going - if e.errno != errno.ENOENT: + if e.errno != 2: # 2 is the errno for ENOENT raise os.mkdir(self.hookdir) -class Hook(object): +class Hook: """ Represents a hook script and provides methods to enable/disable hooks """ @@ -258,14 +245,13 @@ def _check_hook_type(self): if self.hookname.startswith(hooktype): break else: - stderr.write("WARNING: {0} is not a valid hook type. " - "It will not be triggered\n".format(self.hookname)) + print("WARNING: {0} is not a valid hook type. It will not be triggered".format(self.hookname), file=os.sys.stderr) def _remove_file(self, file): try: os.remove(file) except OSError as e: - if e.errno == errno.ENOENT: + if e.errno == 2: # 2 is the errno for ENOENT raise HookError("Hook with name {0} was not found on hooks/ folder".format(file)) else: raise