Skip to content

Commit

Permalink
Fixing IOError management to be compatible to python 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-yaelg committed Oct 15, 2020
1 parent e4923ef commit 3a78334
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
16 changes: 10 additions & 6 deletions cpapi/mgmt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -27,8 +29,6 @@
import subprocess
import time

from cpapi.utils import compatible_loads


class APIClientArgs:
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions cpapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="cpapi",
version="1.1.0",
version="1.1.1",
author="API team",
author_email="[email protected]",
description="Check Point Management API SDK",
Expand Down

0 comments on commit 3a78334

Please sign in to comment.