nixModule.py 6.7 KB

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