diff restService/redirector.py @ 5:3ebe37d81071

addDri added reorganisation of the packafes
author dwinter
date Fri, 02 Nov 2012 09:01:35 +0100
parents redirector.py@caeede0c9464
children 5f5447b3a082
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/restService/redirector.py	Fri Nov 02 09:01:35 2012 +0100
@@ -0,0 +1,95 @@
+'''
+redirects depending on a configuration file an index.meta purl to an viewer
+Created on 01.11.2012
+
+@author: dwinter
+'''
+
+import web
+import manageIndexMetaPURLs
+import logging
+
+class redirector:
+    
+    viewers={} # hash mit allen viewer name --> urls
+    purlHandler=None
+    def __init__(self):
+        #read config file for the viewers
+        confFile= file("viewer.config")
+        
+        self.purlHandler = manageIndexMetaPURLs.IndexMetaPURLManager()
+        
+        for line in confFile.readlines():
+            splitted=line.split(",")
+            
+            list=[]
+            if splitted[1]=="":
+                list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
+            else:
+                list.append(splitted[1])
+                
+            if len(splitted)>1: # url fur image viewer
+                if splitted[2]=="":
+                    list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
+                else:
+                    list.append(splitted[2])
+            else:
+                list.append(None) # null wenn keiner konfiguriert wird. TODO: handle this
+            
+            
+            self.viewers[splitted[0]]=list
+                
+    def GET(self,path):
+        
+        splitted=path.split("/")
+        if len(splitted)!=2: #pfrad sollte zwei anteile habe "flavour/purl"
+            raise web.notfound("not found")
+        
+        purl = splitted[1] 
+        flavour = splitted[0]
+        
+        if flavour not in self.viewers.keys():
+            raise web.notfound("no viewer for %s"%flavour)
+        
+        formats = self.viewers[flavour]
+        
+        viewerWithIndexMetaFormatString =  formats[0];
+        viewerWithImagePathFormatString = formats[1];
+        
+      
+        # checke ob es einen Image path gibt
+        path,validity = self.purlHandler.getImagePathValidity(purl)
+        if path is not None and path!="":
+            return self.handlePath(path,validity,viewerWithImagePathFormatString)
+        
+        
+        path,validity = self.purlHandler.getPathValidity(purl)
+        
+        if path is not None and path !="":
+            return self.handlePath(path,validity,viewerWithIndexMetaFormatString)
+        
+    
+    
+    #handle path 
+    def handlePath(self,path,validity,viewerFormatString):
+        
+        if viewerFormatString is None or viewerFormatString=="": 
+            raise web.internalerror("no viewer configure for indexMeta for this flavour")
+        
+        if path is None:
+            raise web.notfound("Cannnot find a URL to this path")
+        
+        if validity is manageIndexMetaPURLs.PERM_NON_VALID:
+            raise web.notfound("PURL NON VALID ANYMORE!")
+         
+        if validity is manageIndexMetaPURLs.TEMP_NON_VALID:
+            return web.notfound("PURL currently not VALID try later!")
+        
+    
+        viewerUrl = viewerFormatString%path
+        
+        print viewerUrl
+        raise web.redirect(viewerUrl,"302 found")
+        
+if __name__ == '__main__':
+    pass
\ No newline at end of file