nixModule.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import os
  2. import unittest
  3. from __main__ import vtk, qt, ctk, slicer
  4. from slicer.ScriptedLoadableModule import *
  5. #import slicerNetwork
  6. import json
  7. import zipfile
  8. import pathlib
  9. import sys
  10. import urllib3
  11. import string
  12. import chardet
  13. import base64
  14. #
  15. # labkeySlicerPythonExtension
  16. #
  17. def buildGITURL(project,path,server):
  18. pathURL='%2f'.join(path)
  19. projectURL='%2f'.join(project)
  20. return server+'/api/v4/projects/'+projectURL+'/repository/files/'+pathURL+'?ref=master'
  21. def getWrapper(name):
  22. project=['labkey','nixsuite']
  23. if name=='nixWrapper.py':
  24. path=['wrapper','nixWrapper.py']
  25. server='http://wiscigt.powertheword.com'
  26. remoteSourcesURL=buildGITURL(project,path,server)
  27. http = urllib3.PoolManager()
  28. r = http.request('GET', remoteSourcesURL)
  29. #returns a JSON
  30. encoding=chardet.detect(r.data)['encoding']
  31. jsonData=json.loads(r.data.decode(encoding))
  32. #we are interested in content, do looped way of decoding it
  33. b64_bytes=jsonData['content'].encode('ascii')
  34. m_bytes=base64.b64decode(b64_bytes)
  35. m=m_bytes.decode('ascii')
  36. #m is string coded file that needs to be written to file
  37. pathList=['.nix','software','src']
  38. pathList.extend(project)
  39. pathList.extend(path)
  40. localPath=os.path.join(os.path.expanduser('~'),*pathList)
  41. p=pathlib.Path(localPath)
  42. if not os.path.isdir(str(p.parent)):
  43. os.makedirs(str(p.parent))
  44. with open(localPath,'w') as f:
  45. f.write(m)
  46. return localPath
  47. class nixModule(ScriptedLoadableModule):
  48. """Uses ScriptedLoadableModule base class, available at:
  49. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  50. """
  51. def __init__(self, parent):
  52. ScriptedLoadableModule.__init__(self, parent)
  53. self.parent.title = "nixModule" # TODO make this more human readable by adding spaces
  54. self.parent.categories = ["NIX"]
  55. self.parent.dependencies = []
  56. self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)"
  57. self.parent.helpText = """
  58. Interface to NIX software
  59. """
  60. self.parent.acknowledgementText = """
  61. Developed within the medical physics research programme of the Slovenian research agency.
  62. """ # replace with organization, grant and thanks.
  63. #
  64. # labkeySlicerPythonExtensionWidget
  65. #
  66. class nixModuleWidget(ScriptedLoadableModuleWidget):
  67. """Uses ScriptedLoadableModuleWidget base class, available at:
  68. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  69. """
  70. def setup(self):
  71. ScriptedLoadableModuleWidget.setup(self)
  72. # Instantiate and connect widgets ...
  73. self.logic=nixModuleLogic(self)
  74. fhome=os.path.expanduser('~')
  75. fwrapper=getWrapper('nixWrapper.py')
  76. p=pathlib.Path(fwrapper)
  77. sys.path.append(str(p.parent))
  78. import nixWrapper
  79. self.remoteResources=nixWrapper.getResources()
  80. #self.network=slicerNetwork.labkeyURIHandler()
  81. #
  82. # Parameters Area
  83. #
  84. loadCollapsibleButton = ctk.ctkCollapsibleButton()
  85. loadCollapsibleButton.text = "Add module"
  86. self.layout.addWidget(loadCollapsibleButton)
  87. loadFormLayout = qt.QFormLayout(loadCollapsibleButton)
  88. self.libraryName=qt.QComboBox()
  89. self.libraryName.insertItems(0,[m for m in self.remoteResources])
  90. self.libraryName.currentIndexChanged.connect(self.onLibraryNameChanged)
  91. loadFormLayout.addRow("Library: ", self.libraryName)
  92. self.moduleName=qt.QComboBox()
  93. try:
  94. print('Library: '.format(self.libraryName.currentText))
  95. self.moduleName.insertItems(0,[m for m in self.remoteResources[self.libraryName.currentText]['modules']])
  96. except KeyError:
  97. pass
  98. self.moduleName.currentIndexChanged.connect(self.onModuleNameChanged)
  99. loadFormLayout.addRow("Module: ", self.moduleName)
  100. self.loadLibraryButton=qt.QPushButton("Load")
  101. self.loadLibraryButton.toolTip="Load library"
  102. self.loadLibraryButton.clicked.connect(self.onLoadLibraryButtonClicked)
  103. loadFormLayout.addRow("Load library:",self.loadLibraryButton)
  104. self.loadModuleButton=qt.QPushButton("Load")
  105. self.loadModuleButton.toolTip="Load library"
  106. self.loadModuleButton.clicked.connect(self.onLoadModuleButtonClicked)
  107. loadFormLayout.addRow("Load module:",self.loadModuleButton)
  108. def onLoadModuleButtonClicked(self):
  109. self.logic.loadModule(self.libraryName.currentText,self.moduleName.currentText)
  110. print('onLoadModuleButtonClicked')
  111. def onLoadLibraryButtonClicked(self):
  112. self.logic.loadLibrary(self.libraryName.currentText)
  113. print('onLoadLibraryButtonClicked')
  114. def onModuleNameChanged(self):
  115. pass
  116. def onLibraryNameChanged(self):
  117. self.moduleName.clear()
  118. self.moduleName.insertItem(0,'Select')
  119. self.moduleName.insertItems(1,self.remoteResources[self.libraryName.currentText]['modules'])
  120. # labkeySlicerPythonExtensionLogic
  121. #
  122. class nixModuleLogic(ScriptedLoadableModuleLogic):
  123. """This class should implement all the actual
  124. computation done by your module. The interface
  125. should be such that other python code can import
  126. this class and make use of the functionality without
  127. requiring an instance of the Widget.
  128. Uses ScriptedLoadableModuleLogic base class, available at:
  129. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  130. """
  131. def __init__(self,parent):
  132. ScriptedLoadableModuleLogic.__init__(self, parent)
  133. def loadModule(self,libName,modName):
  134. import nixWrapper
  135. nixWrapper.loadModule(slicer,qt,libName,modName)
  136. def loadLibrary(self,libName):
  137. import nixWrapper
  138. nixWrapper.loadLibrary(libName)
  139. class nixModuleTest(ScriptedLoadableModuleTest):
  140. """
  141. This is the test case for your scripted module.
  142. Uses ScriptedLoadableModuleTest base class, available at:
  143. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  144. """
  145. def setUp(self):
  146. """ Do whatever is needed to reset the state - typically a scene clear will be enough.
  147. """
  148. slicer.mrmlScene.Clear(0)
  149. def runTest(self):
  150. pass