From 74338be3e141c38d894c33f3f6a7e92f1584c8fa Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Fri, 24 Jan 2025 11:15:40 +0200 Subject: [PATCH] Handle error when no passwords --- nc_password_client/nc_password_client.py | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/nc_password_client/nc_password_client.py b/nc_password_client/nc_password_client.py index 377dcb7..57f3fe9 100755 --- a/nc_password_client/nc_password_client.py +++ b/nc_password_client/nc_password_client.py @@ -112,6 +112,7 @@ class NextcloudHandler: self._log = params['logger'] else: self._init_log(params) + self.params = params self.http = 'https' self.timeout = params.get('timeout', 3) self.ssl = True @@ -391,6 +392,10 @@ class NextcloudHandler: def debug(self, obj, unsafe=False): '''Show debug information''' + obj['connection'] = { + "host": self.params['host'], + "user": self.params['user'] + } self._log.debug( self._output(obj, unsafe=unsafe) ) @@ -444,7 +449,7 @@ class NextcloudHandler: def get(self, path): '''Do a GET request''' - self.debug({ "action": "get", "message": f"Requesting {path}" }) + self.debug({ "action": "get", "message": f"Requesting '{self.http}://{self.host}/{path}'" }) try: r = self.session.get( f'{self.http}://{self.host}/{path}', @@ -784,6 +789,13 @@ class NextcloudHandler: return folder return False + def _is_uuid(self, string): + '''Test if a string is a UUID''' + match = re.match(r'^[0-9a-f\-]*$', string) + if match: + return True + return False + def create_passwords_folder(self, name): '''Create passwords folder''' if not self.exists_passwords_folder(name): @@ -903,7 +915,10 @@ class NextcloudHandler: } ) if new_obj.get('folder', '') != '': - folder_id = self.get_folder_id(new_obj['folder']) + if self._is_uuid(new_obj['folder']): + folder_id = new_obj['folder'] + else: + folder_id = self.get_folder_id(new_obj['folder']) if not folder_id: created_folder = self.create_passwords_folder(new_obj['folder']) if created_folder: @@ -1636,8 +1651,12 @@ class NcPasswordClient: "field_replacements": self.field_replacements, } self.destination_nc = NextcloudHandler(params) - for password in self.nc.list_passwords(): - self.destination_nc.create_password(password, update=True) + all_passwords = self.nc.list_passwords() + if all_passwords: + for password in all_passwords: + self.destination_nc.create_password(password, update=True) + else: + return False return True def _init_log(self):