浏览代码

Initial import

NIX User 5 年之前
父节点
当前提交
df904e183a
共有 3 个文件被更改,包括 172 次插入0 次删除
  1. 58 0
      orthancDatabaseBrowser.py
  2. 18 0
      orthancFileBrowser.py
  3. 96 0
      orthancInterface.py

+ 58 - 0
orthancDatabaseBrowser.py

@@ -0,0 +1,58 @@
+import orthancInterface
+import json
+import chardet
+
+def extractJSON(data):
+    encoding=chardet.detect(data)["encoding"]
+    return json.loads(data.decode(encoding))
+
+
+class orthancDB:
+    def __init__(self,net):
+        self.net=net
+
+    def getPatients(self):
+        url=self.net.getCoreURL()
+        url+='/patients'
+        response=self.net.get(url)
+        return extractJSON(response.data)
+
+    def getPatientData(self, orthancId):
+        return self.getData('patients',orthancId)
+
+    def getStudyData(self, studyOrthancId):
+        return self.getData('studies',studyOrthancId)
+
+    def getSeriesData(self, seriesOrthancId):
+        return self.getData('series',seriesOrthancId)
+
+    def getData(self, level, orthancId):
+        url=self.net.getCoreURL()
+        url+='/'+level+'/'+orthancId
+        response=self.net.get(url)
+        return extractJSON(response.data)
+
+
+    def selectRows(self, qfilter):
+        obj={}
+        obj["Level"]="Instance"
+        obj["Query"]=qfilter
+        jsonData=json.dumps(obj)
+
+        url=self.net.getCoreURL()
+        url+='/tools/find'
+        response=self.net.post(url,'json',jsonData)
+        encoding=chardet.detect(response.data)["encoding"]
+        return json.loads(response.data.decode(encoding))
+
+
+    def upload(self, f) :
+        url=self.net.getCoreURL()
+        url+='/instances'
+        fobj=open (f, 'rb')
+        response=self.net.post(url,'octet-stream',fobj.read())
+        encoding=chardet.detect(response.data)["encoding"]
+        return json.loads(response.data.decode(encoding))
+
+
+

+ 18 - 0
orthancFileBrowser.py

@@ -0,0 +1,18 @@
+import urllib3
+import shutil
+
+
+class orthancFileBrowser:
+
+    def __init__(self,net):
+        self.net=net
+
+    def getZip(self,retrieveLevel, dataId, path):
+        url=self.net.getCoreURL()
+        url+="/"+retrieveLevel+"/"+dataId+"/archive";
+        response=self.net.get(url,binary=True);
+        with open(path,'wb') as out_file:
+            shutil.copyfileobj(response,out_file)
+        response.release_conn()
+        #response.data is a byte array -> is the same as file
+

+ 96 - 0
orthancInterface.py

@@ -0,0 +1,96 @@
+import urllib3
+#requests doesnt work as it lacks key password checking
+import json
+
+
+class orthancInterface:
+
+    def init(self,fname):
+        #fname is a file with configuration
+        try:
+            f=open(fname)
+        except OSError as e:
+            print("Confgiuration error: OS error({0}): {1}").format(e.errno, e.strerror)
+            raise
+
+        self.connectionConfig=json.load(f)
+        
+        self.http=urllib3.PoolManager()
+
+        if 'SSL' in self.connectionConfig:
+            self.http = urllib3.PoolManager(\
+                cert_file=self.connectionConfig['SSL']['user'],\
+                cert_reqs='CERT_REQUIRED',\
+                key_file=self.connectionConfig['SSL']['key'],\
+                ca_certs=self.connectionConfig['SSL']['ca'])
+
+    #password=self.connectionConfig['SSL']['keyPwd'],\ doesnt work until 1.25
+
+    def getBasicAuth(self):
+        user=self.connectionConfig['orthanc']['user']
+        pwd=self.connectionConfig['orthanc']['password']
+        return user+":"+pwd
+        
+        
+    def getCoreURL(self):
+        return self.connectionConfig['orthanc']['server']
+
+    
+    def get(self,url,binary=False):
+
+        debug=False
+        if debug:
+            print("GET: {0}").format(url)
+            print("as {0}").format(user)
+        headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
+        try:
+            if not binary:
+                return self.http.request('GET',url,headers=headers)
+            else:
+                return self.http.request('GET',url,headers=headers,preload_content=False)
+        #f contains json as a return value
+        except urllib3.exceptions.HTTPError as e:
+            print(e)
+
+    def post(self,url,dataType,data):
+
+        #dataType is typically json or octet-stream
+        debug=False
+        headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
+        headers['Content-Type']="application/"+dataType#for files
+        #add csrf;also sets self.cookie
+        #headers["X-LABKEY-CSRF"]=self.getCSRF()
+        #doesn't seem like orthanc is big on security
+        #headers["Cookie"]=self.cookie
+
+        try:
+            return self.http.request('POST',url,headers=headers,body=data)
+        #f contains json as a return value
+        except urllib3.exceptions.HTTPError as e:
+            print(e)
+
+    def put(self,url,data):
+
+        debug=False
+
+        if debug:
+            print("PUT: {}").format(url)
+
+        headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
+        headers["Content-Type"]="application/octet-stream"
+        #add csrf
+        #headers["X-LABKEY-CSRF"]=self.getCSRF()
+        #headers["Cookie"]=self.cookie
+
+        try:
+            return self.http.request('PUT',url,headers=headers,body=data)
+        #f contains json as a return value
+        except urllib3.exceptions.HTTPError as e:
+            print(e)
+      
+
+            
+
+
+
+