From 3a783343e65a40fbff3237cc5358e71a659fa2af Mon Sep 17 00:00:00 2001 From: yaelg Date: Thu, 15 Oct 2020 15:34:46 +0300 Subject: [PATCH] Fixing IOError management to be compatible to python 2 and 3 --- cpapi/mgmt_api.py | 16 ++++++++++------ cpapi/utils.py | 11 +++++++++++ setup.py | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cpapi/mgmt_api.py b/cpapi/mgmt_api.py index 22ebcab..3c1bb00 100644 --- a/cpapi/mgmt_api.py +++ b/cpapi/mgmt_api.py @@ -15,6 +15,8 @@ # compatible import for python 2 and 3 from .api_exceptions import APIException, APIClientException from .api_response import APIResponse +from cpapi.utils import get_massage_from_io_error, compatible_loads + if sys.version_info >= (3, 0): import http.client as http_client else: @@ -27,8 +29,6 @@ import subprocess import time -from cpapi.utils import compatible_loads - class APIClientArgs: """ @@ -243,7 +243,9 @@ def login_as_root(self, domain=None, payload=None): raise APIClientException( "Could not load JSON from login as root command, perhaps no root privileges?\n" + str( type(err)) + " - " + str(err)) - except (WindowsError, subprocess.CalledProcessError) as err: + except subprocess.CalledProcessError as err: + raise APIClientException("Could not login as root:\n" + str(type(err)) + " - " + str(err)) + except (WindowsError) as err: raise APIClientException("Could not login as root:\n" + str(type(err)) + " - " + str(err)) def api_call(self, command, payload=None, sid=None, wait_for_task=True): @@ -650,7 +652,7 @@ def save_fingerprint_to_file(server, fingerprint, filename="fingerprints.txt"): print(e.message, file=sys.stderr) return False except IOError as e: - print("Couldn't open file: " + filename + "\n" + e.message, file=sys.stderr) + print("Couldn't open file: " + filename + "\n" + get_massage_from_io_error(e), file=sys.stderr) return False except Exception as e: print(e, file=sys.stderr) @@ -669,7 +671,8 @@ def save_fingerprint_to_file(server, fingerprint, filename="fingerprints.txt"): filedump.close() return True except IOError as e: - print("Couldn't open file: " + filename + " for writing.\n" + e.message, file=sys.stderr) + print("Couldn't open file: " + filename + " for writing.\n" + get_massage_from_io_error(e), + file=sys.stderr) except Exception as e: print(e, file=sys.stderr) return False @@ -699,7 +702,8 @@ def read_fingerprint_from_file(server, filename="fingerprints.txt"): else: print(e.message, file=sys.stderr) except IOError as e: - print("Couldn't open file: " + filename + "\n" + e.message, file=sys.stderr) + print("Couldn't open file: " + filename + "\n" + get_massage_from_io_error(e), + file=sys.stderr) except Exception as e: print(e, file=sys.stderr) else: diff --git a/cpapi/utils.py b/cpapi/utils.py index 640e73b..45593e6 100644 --- a/cpapi/utils.py +++ b/cpapi/utils.py @@ -11,3 +11,14 @@ def compatible_loads(json_data): if isinstance(json_data, bytes) and (3, 0) <= sys.version_info < (3, 6): json_data = json_data.decode("utf-8") return json.loads(json_data) + + +def get_massage_from_io_error(error): + """ + :param: IOError + :return: error message + """ + if sys.version_info >= (3, 0): + return error.strerror + else: + return error.message diff --git a/setup.py b/setup.py index c5353ae..2abdaed 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="cpapi", - version="1.1.0", + version="1.1.1", author="API team", author_email="api_team@checkpoint.com", description="Check Point Management API SDK",