|
@@ -2,12 +2,13 @@ import os
|
|
|
import unittest
|
|
|
from __main__ import vtk, qt, ctk, slicer
|
|
|
from slicer.ScriptedLoadableModule import *
|
|
|
-import slicerNetwork
|
|
|
-import loadDicom
|
|
|
+#import slicerNetwork
|
|
|
import json
|
|
|
import zipfile
|
|
|
import pathlib
|
|
|
-
|
|
|
+import sys
|
|
|
+import urllib3
|
|
|
+import string
|
|
|
#
|
|
|
# labkeySlicerPythonExtension
|
|
|
#
|
|
@@ -43,7 +44,38 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
ScriptedLoadableModuleWidget.setup(self)
|
|
|
# Instantiate and connect widgets ...
|
|
|
self.logic=labkeyBrowserLogic(self)
|
|
|
- self.network=slicerNetwork.labkeyURIHandler()
|
|
|
+
|
|
|
+ fhome=os.path.expanduser('~')
|
|
|
+ fsetup=os.path.join(fhome,'.labkey','setup.json')
|
|
|
+ try:
|
|
|
+ with open(fsetup) as f:
|
|
|
+ self.setup=json.load(f)
|
|
|
+ except FileNotFoundError:
|
|
|
+ self.setup={}
|
|
|
+
|
|
|
+ try:
|
|
|
+ pt=self.setup['paths']
|
|
|
+ except KeyError:
|
|
|
+ self.setup['paths']={}
|
|
|
+
|
|
|
+ try:
|
|
|
+ sys.path.append(self.setup['paths']['labkeyInterface'])
|
|
|
+ except KeyError:
|
|
|
+ self.setup['paths']['labkeyInterface']=loadLibrary('labkeyInterface')
|
|
|
+ with open(fsetup,'w') as f:
|
|
|
+ json.dump(self.setup,f,indent='\t')
|
|
|
+
|
|
|
+ import labkeyInterface
|
|
|
+ import labkeyDatabaseBrowser
|
|
|
+ import labkeyFileBrowser
|
|
|
+
|
|
|
+ #self.network=slicerNetwork.labkeyURIHandler()
|
|
|
+ self.network=labkeyInterface.labkeyInterface()
|
|
|
+ fconfig=os.path.join(fhome,'.labkey','network.json')
|
|
|
+ self.network.init(fconfig)
|
|
|
+ self.db=labkeyDatabaseBrowser.labkeyDB(self.network)
|
|
|
+ self.fb=labkeyFileBrowser.labkeyFileBrowser(self.network)
|
|
|
+
|
|
|
#
|
|
|
# Parameters Area
|
|
|
#
|
|
@@ -134,7 +166,8 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
self.fileList=qt.QListWidget()
|
|
|
self.fileList.toolTip="Select remote file"
|
|
|
self.fileList.itemDoubleClicked.connect(self.onFileListDoubleClicked)
|
|
|
- self.currentRemoteDir=''
|
|
|
+ #remote dir is a list
|
|
|
+ self.currentRemoteDir=[]
|
|
|
|
|
|
#add dummy entry
|
|
|
items=('.','..')
|
|
@@ -152,7 +185,8 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
self.fileTypeSelector=qt.QComboBox()
|
|
|
self.fileTypeSelector.toolTip="Select file type"
|
|
|
|
|
|
- items=self.network.fileTypesAvailable()
|
|
|
+ items=('VolumeFile','SegmentationFile','TransformFile')
|
|
|
+
|
|
|
self.populateFileTypeSelector(items)
|
|
|
|
|
|
fileDialogFormLayout.addRow("File type :",self.fileTypeSelector)
|
|
@@ -264,7 +298,24 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
def onLoadConfigButtonClicked(self):
|
|
|
filename=qt.QFileDialog.getOpenFileName(None,'Open configuration file (JSON)',
|
|
|
self.configDir, '*.json')
|
|
|
- self.network.parseConfig(filename,self)
|
|
|
+
|
|
|
+
|
|
|
+ self.network.init(filename)
|
|
|
+
|
|
|
+ cfg=self.network.connectionConfig
|
|
|
+
|
|
|
+ if 'SSL' in cfg:
|
|
|
+ sslSetup=cfg['SSL']
|
|
|
+ self.privateKeyButton.setText(sslSetup['key'])
|
|
|
+ self.userCertButton.setText(sslSetup['user'])
|
|
|
+ self.caCertButton.setText(sslSetup['ca'])
|
|
|
+
|
|
|
+ self.serverURL.setText(cfg['host'])
|
|
|
+ if 'labkey' in cfg:
|
|
|
+ labkey=cfg['labkey']
|
|
|
+ self.authName.setText(labkey['user'])
|
|
|
+ self.authPass.setText(labkey['password'])
|
|
|
+
|
|
|
#self.serverURL.setText(self.network.hostname)
|
|
|
#self.authName.setText(self.network.auth_name)
|
|
|
#self.authPass.setText(self.network.auth_pass)
|
|
@@ -392,24 +443,24 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
iText=item.text()
|
|
|
print("Selected items: {0} ".format(iText))
|
|
|
|
|
|
-
|
|
|
+
|
|
|
#this is hard -> compose path string from currentRemoteDir and selection
|
|
|
if item.text().find('..')==0:
|
|
|
#one up
|
|
|
- idx=self.currentRemoteDir.rfind('/')
|
|
|
- if idx<0:
|
|
|
- self.currentRemoteDir=''
|
|
|
- else:
|
|
|
- self.currentRemoteDir=self.currentRemoteDir[:idx]
|
|
|
+ try:
|
|
|
+ self.currentRemoteDir.pop()
|
|
|
+ except IndexError:
|
|
|
+ pass
|
|
|
elif item.text().find('.')==0:
|
|
|
pass
|
|
|
else:
|
|
|
- if len(self.currentRemoteDir)>0:
|
|
|
- self.currentRemoteDir+='/'
|
|
|
- self.currentRemoteDir+=item.text()
|
|
|
- print("Listing {0}".format(self.currentRemoteDir))
|
|
|
- flist=self.network.toRelativePath(
|
|
|
- self.network.listRelativeDir(self.currentRemoteDir))
|
|
|
+ self.currentRemoteDir.append(item.text())
|
|
|
+ print("Listing {}".format(self.currentRemoteDir))
|
|
|
+ remoteDirString=self.fb.GetRootUrl()
|
|
|
+ if len(self.currentRemoteDir):
|
|
|
+ remoteDirString+='/'+'/'.join(self.currentRemoteDir)
|
|
|
+ ok,lst=self.fb.listRemoteDir(remoteDirString)
|
|
|
+ flist=[self.fb.baseDir(f) for f in lst]
|
|
|
print("Got")
|
|
|
print(flist)
|
|
|
flist.insert(0,'..')
|
|
@@ -419,20 +470,32 @@ class labkeyBrowserWidget(ScriptedLoadableModuleWidget):
|
|
|
|
|
|
def onLoadFileButtonClicked(self):
|
|
|
properties={}
|
|
|
- localPath=self.network.DownloadFileToCache(self.selectedFile.text)
|
|
|
+
|
|
|
+ #local path
|
|
|
+ tempDir=os.path.join(os.path.expanduser('~'),'temp')
|
|
|
+ if not os.path.isdir(tempDir):
|
|
|
+ os.mkdir(tempDir)
|
|
|
+ localPath=os.path.join(tempDir,self.currentRemoteDir[-1])
|
|
|
+
|
|
|
+ print(localPath)
|
|
|
+
|
|
|
+ #remote path
|
|
|
+ remotePath=self.fb.GetRootUrl()+'/'+'/'.join(self.currentRemoteDir)
|
|
|
+ print(remotePath)
|
|
|
+
|
|
|
+ #copy over
|
|
|
+ self.fb.readFileToFile(remotePath,localPath)
|
|
|
+
|
|
|
+ #do slicer magic
|
|
|
slicer.util.loadNodeFromFile(localPath,self.fileTypeSelector.currentText,
|
|
|
properties,returnNode=False)
|
|
|
- if not self.keepCachedFileCheckBox.isChecked():
|
|
|
- os.remove(localPath)
|
|
|
+
|
|
|
+ #if not self.keepCachedFileCheckBox.isChecked():
|
|
|
+ os.remove(localPath)
|
|
|
|
|
|
def onLoadDirButtonClicked(self):
|
|
|
- #dir=self.network.loadDir(self.selectedFile.text)
|
|
|
- dir=self.selectedFile.text
|
|
|
- try:
|
|
|
- self.loadDicomLogic.load(self.network,dir)
|
|
|
- except:
|
|
|
- self.loadDicomLogic=loadDicom.loadDicomLogic(self)
|
|
|
- self.loadDicomLogic.load(self.network,dir)
|
|
|
+
|
|
|
+ print('Not implemented')
|
|
|
|
|
|
# labkeySlicerPythonExtensionLogic
|
|
|
#
|
|
@@ -578,3 +641,45 @@ def getHomeDir():
|
|
|
except:
|
|
|
fhome=os.environ['HOMEDRIVE']+os.environ['HOMEPATH']
|
|
|
return fhome
|
|
|
+
|
|
|
+
|
|
|
+def loadLibrary(name):
|
|
|
+ #load library from git, store it at a default location and return path to the stored location
|
|
|
+ remoteSources={
|
|
|
+ "labkeyInterface":\
|
|
|
+ "http://wiscigt.powertheword.com/labkey/labkeyinterface/-/archive/master/labkeyinterface-master.zip"
|
|
|
+ }
|
|
|
+ #two steps:
|
|
|
+ #1 Download
|
|
|
+
|
|
|
+ tempDir=os.path.join(os.path.expanduser('~'),'temp')
|
|
|
+ if not os.path.isdir(tempDir):
|
|
|
+ os.mkdir(tempDir)
|
|
|
+ tempFile=os.path.join(tempDir,name+'.zip')
|
|
|
+
|
|
|
+ http = urllib3.PoolManager()
|
|
|
+ r = http.request('GET', remoteSources[name], preload_content=False)
|
|
|
+ chunk_size=65536
|
|
|
+ with open(tempFile, 'wb') as out:
|
|
|
+ while True:
|
|
|
+ data = r.read(chunk_size)
|
|
|
+ if not data:
|
|
|
+ break
|
|
|
+ out.write(data)
|
|
|
+
|
|
|
+ r.release_conn()
|
|
|
+
|
|
|
+ #2 Unzip
|
|
|
+ installDir=os.path.join(os.path.expanduser('~'),'.labkey','software','src')
|
|
|
+ if not os.path.isdir(installDir):
|
|
|
+ os.makedirs(installDir)
|
|
|
+
|
|
|
+ with zipfile.ZipFile(tempFile,'r') as zip_ref:
|
|
|
+ zip_ref.extractall(installDir)
|
|
|
+ #cleanup
|
|
|
+ os.remove(tempFile)
|
|
|
+
|
|
|
+ zipName=name.lower()+'-master'
|
|
|
+ os.rename(os.path.join(installDir,zipName),os.path.join(installDir,name))
|
|
|
+
|
|
|
+ return os.path.join(installDir,name)
|