config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import labkeyInterface
  2. import pathlib
  3. import os
  4. import zipfile
  5. import json
  6. def setConfig(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  7. certificateZip=None):
  8. connectionConfig={}
  9. if certificateZip!=None:
  10. zpath=pathlib.Path(certificateZip)
  11. user=zpath.stem
  12. userDir=os.path.join(os.path.expanduser('~'),'.labkey',user)
  13. if not os.path.isdir(userDir):
  14. os.makedirs(userDir)
  15. caName=None
  16. with zipfile.ZipFile(certificateZip,'r') as zipObj:
  17. zipObj.extract(user+'.crt',userDir)
  18. zipObj.extract(user+'.key',userDir)
  19. zipList=zipObj.namelist()
  20. for f in zipList:
  21. if f.find('CA')>-1:
  22. caName=f
  23. zipObj.extract(f,userDir)
  24. #setup SSL
  25. sslSetup={}
  26. sslSetup['user']=os.path.join(userDir,user+'.crt')
  27. sslSetup['key']=os.path.join(userDir,user+'.key')
  28. sslSetup['ca']=os.path.join(userDir,caName)
  29. sslSetup['keyPwd']='notUsed'
  30. connectionConfig['SSL']=sslSetup
  31. connectionConfig["host"]=labkeyServer
  32. connectionConfig["context"]="labkey"
  33. labkeySetup={}
  34. labkeySetup['user']=labkeyUser
  35. labkeyPwd='guest'
  36. if not labkeyUser=='guest':
  37. labkeyPwd=getpass.getpass(prompt='Enter labkey password:')
  38. labkeySetup['password']=labkeyPwd
  39. connectionConfig['labkey']=labkeySetup
  40. orthancSetup={}
  41. if connectionConfig['host'].find('merlin')>-1:
  42. orthancSetup['server']='https://orthanc.fmf.uni-lj.si'
  43. if connectionConfig['host'].find('onko-nix')>-1:
  44. orthancSetup['server']='http://onko-nix.onko-i.si:8042'
  45. orthancSetup['user']='ask'
  46. orthancSetup['password']='askPassword'
  47. connectionConfig['orthanc']=orthancSetup
  48. net=labkeyInterface.labkeyInterface()
  49. net.connectionConfig=connectionConfig
  50. net.initRemote()
  51. print(net.getUserId())
  52. return net
  53. def testCertificate(certificateZip):
  54. return setConfig('guest',certificateZip=certificateZip)
  55. def testConnection(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  56. certificateZip=None):
  57. return setConfig(labkeyUser,labkeyServer,certificateZip)
  58. def getDefaultConfig(configFile):
  59. #if configFile is None, this will set it to default and leave unchanged otherwise
  60. if configFile==None:
  61. return os.path.join(os.path.expanduser('~'),'.labkey','network.json')
  62. return configFile
  63. def storeConfig(net,configFile=None):
  64. #if configFile is None, this will set it to default and leave unchanged otherwise
  65. with open(getDefaultConfig(configFile),'w') as f:
  66. json.dump(net.connectionConfig,f,indent='\t')
  67. def getInterface(configFile=None):
  68. net=labkeyInterface.labkeyInterface()
  69. #if configFile is None, this will set it to default and leave unchanged otherwise
  70. net.init(getDefaultConfig(configFile))
  71. return net