|
@@ -10,6 +10,7 @@ import shutil
|
|
import distutils
|
|
import distutils
|
|
import os
|
|
import os
|
|
import base64
|
|
import base64
|
|
|
|
+import json
|
|
|
|
|
|
#see https://gist.github.com/logic/2715756, allow requests to do Put
|
|
#see https://gist.github.com/logic/2715756, allow requests to do Put
|
|
class MethodRequest(urllib2.Request):
|
|
class MethodRequest(urllib2.Request):
|
|
@@ -27,9 +28,8 @@ class MethodRequest(urllib2.Request):
|
|
return urllib2.Request.get_method(self, *args, **kwargs)
|
|
return urllib2.Request.get_method(self, *args, **kwargs)
|
|
|
|
|
|
class slicerNetwork:
|
|
class slicerNetwork:
|
|
- def __init__(self,parent):
|
|
|
|
- parent.title = "slicerNetwork"
|
|
|
|
- self.parent=parent
|
|
|
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
|
+ pass
|
|
|
|
|
|
class labkeyURIHandler(slicer.vtkURIHandler):
|
|
class labkeyURIHandler(slicer.vtkURIHandler):
|
|
def __init__(self):
|
|
def __init__(self):
|
|
@@ -65,7 +65,10 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
return self.localCacheDirectory
|
|
return self.localCacheDirectory
|
|
|
|
|
|
def GetLabkeyUrl(self):
|
|
def GetLabkeyUrl(self):
|
|
- return self.hostname+"/labkey/_webdav"
|
|
|
|
|
|
+ return self.hostname+"/labkey"
|
|
|
|
+
|
|
|
|
+ def GetLabkeyWebdavUrl(self):
|
|
|
|
+ return self.GetLabkeyUrl()+"/_webdav"
|
|
|
|
|
|
def GetLocalPath(self,source):
|
|
def GetLocalPath(self,source):
|
|
debug=False
|
|
debug=False
|
|
@@ -77,7 +80,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
return os.path.join(self.localCacheDirectory,relativePath)
|
|
return os.path.join(self.localCacheDirectory,relativePath)
|
|
|
|
|
|
def GetRemotePath(self,source):
|
|
def GetRemotePath(self,source):
|
|
- return self.GetLabkeyUrl()+"/"+GetLabkeyPathFromLocalPath(source)
|
|
|
|
|
|
+ return self.GetLabkeyWebdavUrl()+"/"+GetLabkeyPathFromLocalPath(source)
|
|
|
|
|
|
def GetLabkeyPathFromLocalPath(self,f):
|
|
def GetLabkeyPathFromLocalPath(self,f):
|
|
#report it with URL separator, forward-slash
|
|
#report it with URL separator, forward-slash
|
|
@@ -95,7 +98,8 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
def GetLabkeyPathFromRemotePath(self,f):
|
|
def GetLabkeyPathFromRemotePath(self,f):
|
|
#used to query labkey stuff, so URL separator is used
|
|
#used to query labkey stuff, so URL separator is used
|
|
f=re.sub('labkey://','',f)
|
|
f=re.sub('labkey://','',f)
|
|
- f=re.sub(self.GetLabkeyUrl(),'',f)
|
|
|
|
|
|
+ f=re.sub(self.GetLabkeyWebdavUrl(),'',f)
|
|
|
|
+
|
|
if f[0]=='/':
|
|
if f[0]=='/':
|
|
f=f[1:len(f)]
|
|
f=f[1:len(f)]
|
|
return f;
|
|
return f;
|
|
@@ -115,11 +119,9 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
remote=self.readFile(self.hostname,labkeyPath)
|
|
remote=self.readFile(self.hostname,labkeyPath)
|
|
#make all necessary directories
|
|
#make all necessary directories
|
|
path=os.path.dirname(dest)
|
|
path=os.path.dirname(dest)
|
|
- try:
|
|
|
|
|
|
+ if not os.path.isdir(path):
|
|
os.makedirs(path)
|
|
os.makedirs(path)
|
|
- except:
|
|
|
|
- if not os.path.isdir(path):
|
|
|
|
- raise
|
|
|
|
|
|
+
|
|
local=open(dest,'wb')
|
|
local=open(dest,'wb')
|
|
#make sure we are at the begining of the file
|
|
#make sure we are at the begining of the file
|
|
|
|
|
|
@@ -164,6 +166,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
return self.loadNodeFromFile(filename,filetype, properties,returnNode)
|
|
return self.loadNodeFromFile(filename,filetype, properties,returnNode)
|
|
#add others if needed
|
|
#add others if needed
|
|
|
|
|
|
|
|
+ ## setup & initialization routines
|
|
|
|
|
|
def configureSSL(self,cert,key,pwd,cacert):
|
|
def configureSSL(self,cert,key,pwd,cacert):
|
|
#do this first
|
|
#do this first
|
|
@@ -201,11 +204,33 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
cookie_handler=urllib2.HTTPCookieProcessor(cj)
|
|
cookie_handler=urllib2.HTTPCookieProcessor(cj)
|
|
self.opener=urllib2.build_opener(https_handler,cookie_handler)
|
|
self.opener=urllib2.build_opener(https_handler,cookie_handler)
|
|
|
|
|
|
|
|
+ def parseConfig(self,fname):
|
|
|
|
+ try:
|
|
|
|
+ f=open(fname)
|
|
|
|
+ except OSError as e:
|
|
|
|
+ print "Confgiuration error: OS error({0}): {1}".format(e.errno, e.strerror)
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ dt=json.load(f)
|
|
|
|
+ if dt.has_key('SSL'):
|
|
|
|
+ self.configureSSL(
|
|
|
|
+ dt['SSL']['user'],
|
|
|
|
+ dt['SSL']['key'],
|
|
|
|
+ dt['SSL']['keyPwd'],
|
|
|
|
+ dt['SSL']['ca']
|
|
|
|
+ )
|
|
|
|
+ self.hostname=dt['host']
|
|
|
|
+ self.auth_name=dt['labkey']['user']
|
|
|
|
+ self.auth_pass=dt['labkey']['password']
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
#cj in opener contains the cookies
|
|
#cj in opener contains the cookies
|
|
|
|
|
|
def get(self,url):
|
|
def get(self,url):
|
|
|
|
|
|
print "GET: {0}".format(url)
|
|
print "GET: {0}".format(url)
|
|
|
|
+ print "as {0}".format(self.auth_name)
|
|
r=urllib2.Request(url)
|
|
r=urllib2.Request(url)
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
@@ -217,15 +242,18 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
r=urllib2.Request(url)
|
|
r=urllib2.Request(url)
|
|
#makes it a post
|
|
#makes it a post
|
|
r.add_data(data)
|
|
r.add_data(data)
|
|
- r.add_header("content-type","application/json")
|
|
|
|
|
|
+ r.add_header("Content-Type","application/json")
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
-
|
|
|
|
- f=self.opener.open(r)
|
|
|
|
|
|
+ print "{}: {}".format(r.get_method(),r.get_full_url())
|
|
|
|
+ print "data: {}".format(r.get_data())
|
|
|
|
+ print "Content-Type: {}".format(r.get_header('Content-Type'))
|
|
|
|
+ return self.opener.open(r)
|
|
#f contains json as a return value
|
|
#f contains json as a return value
|
|
|
|
|
|
def put(self,url,data):
|
|
def put(self,url,data):
|
|
|
|
|
|
|
|
+ print "PUT: {0}".format(url)
|
|
r=MethodRequest(url,method="PUT")
|
|
r=MethodRequest(url,method="PUT")
|
|
#makes it a post
|
|
#makes it a post
|
|
r.add_data(data)
|
|
r.add_data(data)
|
|
@@ -233,7 +261,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
base64string = base64.b64encode('%s:%s' % (self.auth_name, self.auth_pass))
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
r.add_header("Authorization", "Basic %s" % base64string)
|
|
|
|
|
|
- f=self.opener.open(r)
|
|
|
|
|
|
+ return self.opener.open(r)
|
|
#f contains json as a return value
|
|
#f contains json as a return value
|
|
|
|
|
|
def remoteDirExists(self,url):
|
|
def remoteDirExists(self,url):
|
|
@@ -261,7 +289,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
if s1<0:
|
|
if s1<0:
|
|
break
|
|
break
|
|
path=labkeyPath[0:s1]
|
|
path=labkeyPath[0:s1]
|
|
- remotePath=self.GetLabkeyUrl()+'/'+path
|
|
|
|
|
|
+ remotePath=self.GetLabkeyWebdavUrl()+'/'+path
|
|
dirExists=self.remoteDirExists(remotePath)
|
|
dirExists=self.remoteDirExists(remotePath)
|
|
if not dirExists:
|
|
if not dirExists:
|
|
if not self.mkdir(remotePath):
|
|
if not self.mkdir(remotePath):
|
|
@@ -284,7 +312,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
|
|
|
|
def listDir(self,dir):
|
|
def listDir(self,dir):
|
|
print "Listing for {0}".format(dir)
|
|
print "Listing for {0}".format(dir)
|
|
- dirUrl=self.GetLabkeyUrl()+"/"+dir
|
|
|
|
|
|
+ dirUrl=self.GetLabkeyWebdavUrl()+"/"+dir
|
|
status,dirs=self.listRemoteDir(dirUrl)
|
|
status,dirs=self.listRemoteDir(dirUrl)
|
|
return dirs
|
|
return dirs
|
|
|
|
|
|
@@ -342,7 +370,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
print errorCode.format(localPath)
|
|
print errorCode.format(localPath)
|
|
return False
|
|
return False
|
|
labkeyDir=labkeyPath[0:labkeyPath.rfind('/')]
|
|
labkeyDir=labkeyPath[0:labkeyPath.rfind('/')]
|
|
- remoteDir=self.GetLabkeyUrl()+'/'+labkeyDir
|
|
|
|
|
|
+ remoteDir=self.GetLabkeyWebdavUrl()+'/'+labkeyDir
|
|
if not self.remoteDirExists(remoteDir):
|
|
if not self.remoteDirExists(remoteDir):
|
|
if not self.mkdirs(remoteDir):
|
|
if not self.mkdirs(remoteDir):
|
|
errorCode="UploadFile: Could not create directory {}"
|
|
errorCode="UploadFile: Could not create directory {}"
|
|
@@ -352,7 +380,7 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
#make an URL request
|
|
#make an URL request
|
|
with open(localPath, 'r') as f:
|
|
with open(localPath, 'r') as f:
|
|
data=f.read()
|
|
data=f.read()
|
|
- remotePath=self.GetLabkeyUrl()+'/'+labkeyPath
|
|
|
|
|
|
+ remotePath=self.GetLabkeyWebdavUrl()+'/'+labkeyPath
|
|
self.put(remotePath,data)
|
|
self.put(remotePath,data)
|
|
|
|
|
|
def loadDir(self, path):
|
|
def loadDir(self, path):
|
|
@@ -367,3 +395,18 @@ class labkeyURIHandler(slicer.vtkURIHandler):
|
|
#fails if there is a subdirectory; go recursively
|
|
#fails if there is a subdirectory; go recursively
|
|
self.readDir(f)
|
|
self.readDir(f)
|
|
return fdir
|
|
return fdir
|
|
|
|
+
|
|
|
|
+ def loadDataset(self,project,dataset):
|
|
|
|
+ url=self.GetLabkeyUrl()+'/'+project
|
|
|
|
+ url+='/query-selectRows.api?schemaName=study&query.queryName='+dataset
|
|
|
|
+ return json.load(self.get(url))
|
|
|
|
+
|
|
|
|
+ def modifyDataset(self,method,project,dataset,rows):
|
|
|
|
+ #method can be insert or update
|
|
|
|
+ data={}
|
|
|
|
+ data['schemaName']='study'
|
|
|
|
+ data['queryName']=dataset
|
|
|
|
+ data['rows']=rows
|
|
|
|
+ url=self.GetLabkeyUrl()+'/'+project
|
|
|
|
+ url+='/query-'+method+'Rows.api?'
|
|
|
|
+ return self.post(url,json.dumps(data))
|