Parcourir la source

Updating slicerNetwork to upload binary files

Andrej Studen il y a 6 ans
Parent
commit
149ce613ee

+ 41 - 5
labkeySlicerPythonExtension/fileIO.py

@@ -157,9 +157,18 @@ class fileIOWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget):
         copyToRemoteButton.connect('clicked(bool)',self.onCopyToRemoteButtonClicked)
         fileDialogFormLayout.addRow(copyToRemoteButton)
 
+        removeRemoteButton=qt.QPushButton("Remove remote")
+        removeRemoteButton.toolTip="Remove Remote"
+        removeRemoteButton.connect('clicked(bool)',self.onRemoveRemoteButtonClicked)
+        fileDialogFormLayout.addRow(removeRemoteButton)
+
+        mkdirRemoteButton=qt.QPushButton("Mkdir remote")
+        mkdirRemoteButton.toolTip="Mkdir Remote"
+        mkdirRemoteButton.connect('clicked(bool)',self.onMkdirRemoteButtonClicked)
+        fileDialogFormLayout.addRow(mkdirRemoteButton)
 
     def onListLocalButtonClicked(self):
-        self.localPath.setText(qt.QFileDialog.getOpenFileName())
+        self.localPath.setText(qt.QFileDialog.getOpenFileName(None,"Select local"))
 
     def onListRemoteButtonClicked(self):
         self.selectRemote.show()
@@ -188,11 +197,38 @@ class fileIOWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget):
         self.loadConfigButton.setText(os.path.basename(filename))
         self.network.initRemote()
 
+    def copyLocalToRemote(self,localPath,remotePath):
+        if os.path.isfile(localPath):
+            #end recursion
+            print "Copy {} to {}".format(localPath,remotePath)
+            self.network.copyFileToRemote(localPath,remotePath)
+            return
+
+        dirName=os.path.basename(os.path.normpath(localPath))
+        remotePath+='/'+dirName
+        if not self.network.isDir(remotePath):
+            self.network.mkdir(remotePath+'/')
+        for f in os.listdir(localPath):
+            #enter recursion
+            localfile=os.path.join(localPath,f)
+            self.copyLocalToRemote(localfile,remotePath)
+
     def onCopyToRemoteButtonClicked(self):
-            if os.path.isfile(self.localPath.text):
-                self.network.copyFileToRemote(self.localPath.text,self.remotePath.text)
-            else:
-                print "Not implemented for directories"
+        self.copyLocalToRemote(self.localPath.text,self.remotePath.text)
+
+    def onRemoveRemoteButtonClicked(self):
+        remotePath=self.remotePath.text
+        if self.network.isDir(remotePath):
+            resp=qt.QMessageBox.question(None,'Delete directory',
+                'Do you want to delete directory {}'.format(remotePath))
+            if resp == qt.QMessageBox.No:
+                return
+        self.network.rmdir(remotePath)
+
+    def onMkdirRemoteButtonClicked(self):
+        remotePath=self.remotePath.text
+        self.network.mkdir(remotePath)
+
 
 class fileIOLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
     def __init__(self,parent):

+ 2 - 2
labkeySlicerPythonExtension/slicerNetwork.py

@@ -249,7 +249,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
     def put(self,url,data):
 
         print "PUT: {0}".format(url)
-        r=MethodRequest(url,method="PUT")
+        r=MethodRequest(url.encode('utf-8'),method="PUT")
         #makes it a post
         r.add_data(data)
         r.add_header("content-type","application/octet-stream")
@@ -312,6 +312,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
         return dirs
 
     def isDir(self, remotePath):
+        #print "isDir: {}".format(remotePath)
         r=MethodRequest(remotePath,method="PROPFIND")
         PROPFIND=u"""<?xml version="1.0" encoding="utf-8"?>\n
             <a:propfind xmlns:a="DAV:">\n
@@ -326,7 +327,6 @@ class labkeyURIHandler(slicer.vtkURIHandler):
         base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
         r.add_header("Authorization", "Basic %s" % base64string)
         print "PROPFIND: {0}".format(remotePath)
-        dirs=[]
         try:
             f=self.opener.open(r)
         except: