Преглед на файлове

Additional functionality to fileIO, further standardization of slicerNetwork

Eager Beaver преди 5 години
родител
ревизия
711f64b45d
променени са 2 файла, в които са добавени 41 реда и са изтрити 4 реда
  1. 37 1
      labkeySlicerPythonExtension/fileIO.py
  2. 4 3
      labkeySlicerPythonExtension/slicerNetwork.py

+ 37 - 1
labkeySlicerPythonExtension/fileIO.py

@@ -4,7 +4,7 @@ import qt
 import ctk
 import json
 import os
-
+import shutil
 
 class fileIO(slicer.ScriptedLoadableModule.ScriptedLoadableModule):
     def __init__(self,parent):
@@ -157,6 +157,11 @@ class fileIOWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget):
         copyToRemoteButton.connect('clicked(bool)',self.onCopyToRemoteButtonClicked)
         fileDialogFormLayout.addRow(copyToRemoteButton)
 
+        copyToLocalButton=qt.QPushButton("Remote -> Local")
+        copyToLocalButton.toolTip="Remote -> Local"
+        copyToLocalButton.connect('clicked(bool)',self.onCopyToLocalButtonClicked)
+        fileDialogFormLayout.addRow(copyToLocalButton)
+
         removeRemoteButton=qt.QPushButton("Remove remote")
         removeRemoteButton.toolTip="Remove Remote"
         removeRemoteButton.connect('clicked(bool)',self.onRemoveRemoteButtonClicked)
@@ -213,9 +218,40 @@ class fileIOWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget):
             localfile=os.path.join(localPath,f)
             self.copyLocalToRemote(localfile,remotePath)
 
+    def copyRemoteToLocal(self,localPath,remotePath):
+        ok,files=self.network.listRemoteDir(remotePath)
+        if not ok:
+            print 'Troubles getting remote dir content for {}'.format(remotePath)
+            return
+        #remove trailing slash
+        for f in files:
+            if f[-1]=='/':
+                f=f[:-1]
+
+            bf=f[f.rfind('/')+1:]
+            dirUrl=self.network.GetLabkeyWebdavUrl()+"/"+f
+            if self.network.isDir(dirUrl):
+                lp=os.path.join(localPath,bf)
+                if not os.path.isdir(lp):
+                    os.mkdir(lp)
+                    print 'Creating {}'.format(lp)
+                print 'Copying directory {} to {}'.format(f,lp)
+                self.copyRemoteToLocal(lp,f)
+            else:
+                rf=self.network.readFile(f)
+                fout=os.path.join(localPath,bf)
+                print 'Copying file {} to {}'.format(f,fout)
+                with open (fout, 'w') as fd:
+                    rf.seek (0)
+                    shutil.copyfileobj (rf, fd)
+                fd.close()
+
     def onCopyToRemoteButtonClicked(self):
         self.copyLocalToRemote(self.localPath.text,self.remotePath.text)
 
+    def onCopyToLocalButtonClicked(self):
+        self.copyRemoteToLocal(self.localPath.text,self.remotePath.text)
+
     def onRemoveRemoteButtonClicked(self):
         remotePath=self.remotePath.text
         if self.network.isDir(remotePath):

+ 4 - 3
labkeySlicerPythonExtension/slicerNetwork.py

@@ -126,7 +126,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
         if debug:
             print "labkeyURIHandler::StageFileRead({0},{1})".format(source,dest)
         labkeyPath=re.sub('labkey://','',source)
-        remote=self.readFile(self.hostname,labkeyPath)
+        remote=self.readFile(labkeyPath)
         #make all necessary directories
         path=os.path.dirname(dest)
         if not os.path.isdir(path):
@@ -406,8 +406,9 @@ class labkeyURIHandler(slicer.vtkURIHandler):
             flist1.append(d)
         return flist1
 
-    def readFile(self, serverUrl, path):
-        dirUrl=serverUrl+"/labkey/_webdav"+"/"+path
+    def readFile(self, path):
+        dirUrl=self.GetLabkeyWebdavUrl()+"/"+path
+        print 'Reading {}'.format(dirUrl)
         f=self.get(dirUrl)
         return StringIO.StringIO(f.read())