123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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(server,project,path,branch='master'):
- #copy of buildGITURL from nixWrapper.py
- if server.find('wiscigt')>-1:
- projectURL='%2f'.join(project)
- pathURL='%2f'.join(path)
- return server+'/api/v4/projects/'+projectURL+'/repository/files/'+pathURL+'?ref='+branch
- if server.find('fmf.uni-lj.si')>-1:
- projectURL='/'.join(project)#studen/nixSuite
- pathURL='/'.join(path)
- return '/'.join([server,projectURL,'raw',branch,pathURL])
- def getWrapper(name):
- project=['labkey','nixsuite']
- if name=='nixWrapper.py':
- path=['wrapper','nixWrapper.py']
- server='http://wiscigt.powertheword.com'
- #update to new git server layout
- server='https://git0.fmf.uni-lj.si'
- project=['studen','nixSuite']
-
- remoteSourcesURL=buildGITURL(server,project,path)
- http = urllib3.PoolManager()
- r = http.request('GET', remoteSourcesURL)
- print(r.data)
-
- #gogs returns raw data
- if server.find('fmf.uni-lj.si')>-1:
- m_bytes=r.data
- if server.find('wiscigt')>-1:
- #returns a JSON
- encoding=chardet.detect(r.data)['encoding']
- jsonData=json.loads(r.data.decode(encoding))
- content=jsonData['content']
- #we are interested in content, do looped way of decoding it
- b64_bytes=content.encode('ascii')
- m_bytes=base64.b64decode(b64_bytes)
-
- #we are interested in content, do looped way of decoding it
- 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
|