From 27216500312070a52b2a471d46d18c869a594063 Mon Sep 17 00:00:00 2001 From: Gerald Kogler Date: Fri, 8 Nov 2024 13:07:37 +0100 Subject: [PATCH] mkdir for subsubdirectories --- layertree2json.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/layertree2json.py b/layertree2json.py index 65d7075..1994fc5 100644 --- a/layertree2json.py +++ b/layertree2json.py @@ -224,6 +224,17 @@ def inputsFtpOk(self, host=None, user=None, password=None): return False else: return True + + + def mk_each_dir(self, sftp, inRemoteDir): + currentDir = '/' # Slash '/' is hardcoded because ftp always uses slash + for dirElement in inRemoteDir.split('/'): + if dirElement: + currentDir += dirElement + '/' + try: + sftp.mkdir(currentDir) + except: + pass # fail silently if remote directory already exists def connectToFtp(self, localFilePath=False, uploadPath=False, uploadFile=False, host=None, user=None, password=None): @@ -244,10 +255,13 @@ def connectToFtp(self, localFilePath=False, uploadPath=False, uploadFile=False, if localFilePath and uploadPath and uploadFile: # Test if remote_path exists and create of not try: - ftp_client.chdir(uploadPath) + ftp_client.chdir(uploadPath) except IOError: - ftp_client.mkdir(uploadPath) - + try: + self.mk_each_dir(ftp_client, uploadPath) + except IOError: + pass + #print(localFilePath, "->", uploadPath + uploadFile) ftp_client.put(localFilePath, uploadPath + uploadFile) @@ -256,9 +270,10 @@ def connectToFtp(self, localFilePath=False, uploadPath=False, uploadFile=False, self.iface.messageBar().pushMessage("Success", "FTP connection ESTABLISHED to host " + host + " without uploading file", level=Qgis.Success, duration=3) ftp_client.close() + ssh_client.close() except: self.iface.messageBar().pushMessage("Warning", "FTP connection FAILED to host " + host, level=Qgis.Warning, duration=3) - + def replaceSpecialChar(self, text): chars = "!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~¬·"