Browse Source

Adding slicer module

Andrej 3 years ago
parent
commit
6250dba6fa
1 changed files with 181 additions and 0 deletions
  1. 181 0
      slicerModule/nixModule.py

+ 181 - 0
slicerModule/nixModule.py

@@ -0,0 +1,181 @@
+import os
+import unittest
+from __main__ import vtk, qt, ctk, slicer
+from slicer.ScriptedLoadableModule import *
+#import slicerNetwork
+import json
+import zipfile
+import pathlib
+import sys
+import urllib3
+import string
+import chardet
+import base64
+#
+# labkeySlicerPythonExtension
+#
+
+def buildGITURL(project,path,server):
+    pathURL='%2f'.join(path)
+    projectURL='%2f'.join(project)
+    return server+'/api/v4/projects/'+projectURL+'/repository/files/'+pathURL+'?ref=master'
+
+def getWrapper(name):
+    project=['labkey','nixsuite']
+    if name=='nixWrapper.py':
+        path=['wrapper','nixWrapper.py']
+    server='http://wiscigt.powertheword.com'
+    remoteSourcesURL=buildGITURL(project,path,server)
+    http = urllib3.PoolManager()
+    r = http.request('GET', remoteSourcesURL)
+    #returns a JSON
+    encoding=chardet.detect(r.data)['encoding']
+    jsonData=json.loads(r.data.decode(encoding))
+
+    #we are interested in content, do looped way of decoding it
+    b64_bytes=jsonData['content'].encode('ascii')
+    m_bytes=base64.b64decode(b64_bytes)
+    m=m_bytes.decode('ascii')
+    #m is string coded file that needs to be written to file
+    pathList=['.nix','software','src']
+    pathList.extend(project)
+    pathList.extend(path)
+    localPath=os.path.join(os.path.expanduser('~'),*pathList)
+    p=pathlib.Path(localPath)
+    if not os.path.isdir(str(p.parent)):
+        os.makedirs(str(p.parent))
+    
+    with open(localPath,'w') as f:
+        f.write(m)
+    return localPath
+
+class nixModule(ScriptedLoadableModule):
+  """Uses ScriptedLoadableModule base class, available at:
+  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
+  """
+
+  def __init__(self, parent):
+    ScriptedLoadableModule.__init__(self, parent)
+    self.parent.title = "nixModule" # TODO make this more human readable by adding spaces
+    self.parent.categories = ["NIX"]
+    self.parent.dependencies = []
+    self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)"
+    self.parent.helpText = """
+    Interface to NIX software
+    """
+    self.parent.acknowledgementText = """
+    Developed within the medical physics research programme of the Slovenian research agency.
+    """ # replace with organization, grant and thanks.
+
+#
+# labkeySlicerPythonExtensionWidget
+#
+
+class nixModuleWidget(ScriptedLoadableModuleWidget):
+  """Uses ScriptedLoadableModuleWidget base class, available at:
+  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
+  """
+
+  def setup(self):
+    ScriptedLoadableModuleWidget.setup(self)
+    # Instantiate and connect widgets ...
+    self.logic=nixModuleLogic(self)
+
+    fhome=os.path.expanduser('~')
+
+    fwrapper=getWrapper('nixWrapper.py')
+    p=pathlib.Path(fwrapper)
+    sys.path.append(str(p.parent))
+    import nixWrapper
+
+    self.remoteResources=nixWrapper.getResources()
+    
+    #self.network=slicerNetwork.labkeyURIHandler()
+
+    #
+    # Parameters Area
+    #
+    loadCollapsibleButton = ctk.ctkCollapsibleButton()
+    loadCollapsibleButton.text = "Add module"
+    self.layout.addWidget(loadCollapsibleButton)
+
+    loadFormLayout = qt.QFormLayout(loadCollapsibleButton)
+
+    self.libraryName=qt.QComboBox()
+    self.libraryName.insertItems(0,[m for m in self.remoteResources])
+    self.libraryName.currentIndexChanged.connect(self.onLibraryNameChanged)
+    loadFormLayout.addRow("Library: ", self.libraryName)
+
+    self.moduleName=qt.QComboBox()
+    try:
+        print('Library: '.format(self.libraryName.currentText))
+        self.moduleName.insertItems(0,[m for m in self.remoteResources[self.libraryName.currentText]['modules']])
+    except KeyError:
+        pass
+    self.moduleName.currentIndexChanged.connect(self.onModuleNameChanged)
+    loadFormLayout.addRow("Module: ", self.moduleName)
+    
+    self.loadLibraryButton=qt.QPushButton("Load")
+    self.loadLibraryButton.toolTip="Load library"
+    self.loadLibraryButton.clicked.connect(self.onLoadLibraryButtonClicked)
+    loadFormLayout.addRow("Load library:",self.loadLibraryButton)
+
+    self.loadModuleButton=qt.QPushButton("Load")
+    self.loadModuleButton.toolTip="Load library"
+    self.loadModuleButton.clicked.connect(self.onLoadModuleButtonClicked)
+    loadFormLayout.addRow("Load module:",self.loadModuleButton)
+
+  def onLoadModuleButtonClicked(self):
+      self.logic.loadModule(self.libraryName.currentText,self.moduleName.currentText)
+      print('onLoadModuleButtonClicked')
+
+  def onLoadLibraryButtonClicked(self):
+      self.logic.loadLibrary(self.libraryName.currentText)
+      print('onLoadLibraryButtonClicked')
+
+  def onModuleNameChanged(self):
+      pass
+
+  def onLibraryNameChanged(self):
+      self.moduleName.clear()
+      self.moduleName.insertItem(0,'Select')
+      self.moduleName.insertItems(1,self.remoteResources[self.libraryName.currentText]['modules'])
+      
+
+# labkeySlicerPythonExtensionLogic
+#
+
+class nixModuleLogic(ScriptedLoadableModuleLogic):
+  """This class should implement all the actual
+  computation done by your module.  The interface
+  should be such that other python code can import
+  this class and make use of the functionality without
+  requiring an instance of the Widget.
+  Uses ScriptedLoadableModuleLogic base class, available at:
+  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
+  """
+  def __init__(self,parent):
+      ScriptedLoadableModuleLogic.__init__(self, parent)
+
+  def loadModule(self,libName,modName):
+      import nixWrapper
+      nixWrapper.loadModule(slicer,qt,libName,modName)
+
+  def loadLibrary(self,libName):
+      import nixWrapper
+      nixWrapper.loadLibrary(libName)
+
+class nixModuleTest(ScriptedLoadableModuleTest):
+  """
+  This is the test case for your scripted module.
+  Uses ScriptedLoadableModuleTest base class, available at:
+  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
+  """
+
+  def setUp(self):
+    """ Do whatever is needed to reset the state - typically a scene clear will be enough.
+    """
+    slicer.mrmlScene.Clear(0)
+
+  def runTest(self):
+      pass