diff --git a/nc_password_client/nc_password_client.py b/nc_password_client/nc_password_client.py index 3b1af06..44f02f4 100755 --- a/nc_password_client/nc_password_client.py +++ b/nc_password_client/nc_password_client.py @@ -53,65 +53,6 @@ DIOS_MIO = """ """ -class NextcloudErrorHandler: - '''Handle errors in Nextcloud''' - def __init__(self): - self.config = {} - self._init_log() - - def _init_log(self): - ''' Initialize log object ''' - self._log = logging.getLogger("nc_password_client") - self._log.setLevel(logging.DEBUG) - - sysloghandler = SysLogHandler() - sysloghandler.setLevel(logging.DEBUG) - self._log.addHandler(sysloghandler) - - streamhandler = logging.StreamHandler(sys.stdout) - streamhandler.setLevel( - logging.getLevelName(self.config.get("debug_level", 'INFO')) - ) - self._log.addHandler(streamhandler) - - if 'log_file' in self.config: - log_file = self.config['log_file'] - else: - home_folder = os.environ.get( - 'HOME', os.environ.get('USERPROFILE', '') - ) - log_folder = os.path.join(home_folder, "log") - log_file = os.path.join(log_folder, "nc_password_client.log") - - if not os.path.exists(os.path.dirname(log_file)): - os.mkdir(os.path.dirname(log_file)) - - filehandler = logging.handlers.RotatingFileHandler( - log_file, maxBytes=102400000 - ) - # create formatter - formatter = logging.Formatter( - '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' - ) - filehandler.setFormatter(formatter) - filehandler.setLevel(logging.DEBUG) - self._log.addHandler(filehandler) - return True - - def status_code_error(self, status): - '''Process status code error???''' - if status > 399: - self._log.error( - 'Nextcloud returned status code %s', - status - ) - sys.exit(1) - else: - self._log.debug( - 'Nextcloud returned status %s', - status - ) - def parameter_spects(spec_arguments): '''Specification of parameters???''' argument_spec = dict( @@ -143,7 +84,6 @@ class NextcloudHandler: '''Handle Nextcloud Password API''' def __init__(self, params): self._init_log(params) - self.exit = NextcloudErrorHandler() self.http = 'https' self.timeout = params.get('timeout', 3) self.ssl = True @@ -161,15 +101,18 @@ class NextcloudHandler: self.host = params.get('host') or os.environ.get('NEXTCLOUD_HOST') if self.host is None: - self.exit.status_code_error('Unable to continue. No Nextcloud Host is given.') + self._log.error('Unable to continue. No Nextcloud Host is given.') + sys.exit(1) self.user = params.get('user') or os.environ.get('NEXTCLOUD_USER') if self.user is None: - self.exit.status_code_error('Unable to continue. No Nextcloud User is given.') + self._log.error('Unable to continue. No Nextcloud User is given.') + sys.exit(2) self.token = params.get('api_token') or os.environ.get('NEXTCLOUD_TOKEN') if self.token is None: - self.exit.status_code_error('Unable to continue. No Nextcloud Token is given.') + self._log.error('Unable to continue. No Nextcloud Token is given.') + sys.exit(3) self.e2e_password = False if params.get('cse_password') or os.environ.get('NEXTCLOUD_CSE_PASSWORD'): @@ -270,9 +213,11 @@ class NextcloudHandler: if r.status_code == 200: return r elif r.status_code == 404: - self.exit.status_code_error(f'File {path} does not exist') + self._log.error(f'File {path} does not exist') + sys.exit(3) else: - self.exit.status_code_error(r.status_code) + self._log.error(r.status_code) + sys.exit(4) def propfind(self, path): '''Do a PROPFIND request''' @@ -338,9 +283,11 @@ class NextcloudHandler: elif r.status_code == 404: return {} - else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def put(self, path, src=None): '''Do a PUT request''' @@ -358,7 +305,10 @@ class NextcloudHandler: if r.status_code in [200, 201, 204]: return r, True else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def delete(self, path): '''Do a DELETE request''' @@ -372,7 +322,10 @@ class NextcloudHandler: elif r.status_code == 404: return r, False else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def talk(self, message, channel): '''Post in Talk/Chat''' @@ -394,7 +347,10 @@ class NextcloudHandler: if r.status_code == 201: return r, True else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def request_passwords_session(self): '''Request a Passwords API session''' @@ -402,7 +358,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def open_passwords_session(self, password_hash): '''Open a Passwords API session''' @@ -421,7 +380,10 @@ class NextcloudHandler: self.x_api_session = r.headers.get('X-API-SESSION') return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def close_passwords_session(self): '''Close Passwords API session''' @@ -429,7 +391,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def list_passwords(self): '''List all passwords''' @@ -437,7 +402,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def list_passwords_folders(self): '''List passwords folders''' @@ -445,7 +413,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", + r.status_code + ) def delete_passwords_folder(self, name): '''Delete a passwords folder''' @@ -473,7 +444,10 @@ class NextcloudHandler: if r.status_code == 201: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) else: self._log.error( "Fodler '{name}' not found." @@ -505,7 +479,10 @@ class NextcloudHandler: if r.status_code == 201: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) else: self._log.warning( "Password folder '%s' already exists", @@ -552,7 +529,10 @@ class NextcloudHandler: if r.status_code == 200: return [r.json().get('password')] else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) def exists_password(self, obj): '''Test if a password exist with the same name''' @@ -578,7 +558,10 @@ class NextcloudHandler: return r.json() else: self._log.error(r.json()) - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) else: self._log.warning( "Password with name '%s' already exists", @@ -598,7 +581,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) def update_password(self, post_obj): '''Update a password''' @@ -613,7 +599,10 @@ class NextcloudHandler: if r.status_code == 200: return r.json() else: - self.exit.status_code_error(r.status_code) + self._log.error( + "Nextcloud instance returned status code: %s", +r.status_code +) def is_same_key(self, key, dict1, dict2): '''Test if two dictionaries have the same key with the same value'''