Przeglądaj źródła

Adding scripts that create a default network setup file

Andrej 2 tygodni temu
rodzic
commit
d8248b091c
1 zmienionych plików z 80 dodań i 0 usunięć
  1. 80 0
      config.py

+ 80 - 0
config.py

@@ -0,0 +1,80 @@
+import labkeyInterface 
+import pathlib
+import os
+import zipfile
+import json
+
+def setConfig(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
+        certificateZip=None):
+    connectionConfig={}
+    if certificateZip!=None:
+        zpath=pathlib.Path(certificateZip)
+        user=zpath.stem
+        userDir=os.path.join(os.path.expanduser('~'),'.labkey',user)
+        if not os.path.isdir(userDir):
+            os.makedirs(userDir)
+        caName=None
+        with zipfile.ZipFile(certificateZip,'r') as zipObj:
+            zipObj.extract(user+'.crt',userDir)
+            zipObj.extract(user+'.key',userDir)
+            zipList=zipObj.namelist()
+            for f in zipList:
+                if f.find('CA')>-1:
+                    caName=f
+                    zipObj.extract(f,userDir)
+    
+        #setup SSL
+        sslSetup={}
+        sslSetup['user']=os.path.join(userDir,user+'.crt')
+        sslSetup['key']=os.path.join(userDir,user+'.key')
+        sslSetup['ca']=os.path.join(userDir,caName)
+        sslSetup['keyPwd']='notUsed'
+        connectionConfig['SSL']=sslSetup
+
+    connectionConfig["host"]=labkeyServer
+    connectionConfig["context"]="labkey"
+    labkeySetup={}
+    labkeySetup['user']=labkeyUser
+    labkeyPwd='guest'
+    if not labkeyUser=='guest':
+        labkeyPwd=getpass.getpass(prompt='Enter labkey password:')
+    labkeySetup['password']=labkeyPwd 
+    connectionConfig['labkey']=labkeySetup
+
+    orthancSetup={}
+    if connectionConfig['host'].find('merlin')>-1:
+        orthancSetup['server']='https://orthanc.fmf.uni-lj.si'
+    if connectionConfig['host'].find('onko-nix')>-1:
+        orthancSetup['server']='http://onko-nix.onko-i.si:8042'
+    orthancSetup['user']='ask'
+    orthancSetup['password']='askPassword'
+    connectionConfig['orthanc']=orthancSetup
+    net=labkeyInterface.labkeyInterface()
+    net.connectionConfig=connectionConfig
+    net.initRemote()
+    print(net.getUserId())
+    return net
+
+def testCertificate(certificateZip):
+    return setConfig('guest',certificateZip=certificateZip)
+
+def testConnection(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
+        certificateZip=None):
+    return setConfig(labkeyUser,labkeyServer,certificateZip)
+
+def getDefaultConfig(configFile):
+    #if configFile is None, this will set it to default and leave unchanged otherwise
+    if configFile==None:
+        return os.path.join(os.path.expanduser('~'),'.labkey','network.json')
+    return configFile
+
+def storeConfig(net,configFile=None):
+    #if configFile is None, this will set it to default and leave unchanged otherwise
+    with open(getDefaultConfig(configFile),'w') as f:
+        json.dump(net.connectionConfig,f,indent='\t')
+
+def getInterface(configFile=None):
+    net=labkeyInterface.labkeyInterface()
+    #if configFile is None, this will set it to default and leave unchanged otherwise
+    net.init(getDefaultConfig(configFile))
+    return net