annotate restService/redirector.py @ 11:fad73212354b

wsgi addedx
author dwinter
date Mon, 05 Nov 2012 09:29:21 +0100
parents 5f5447b3a082
children 0287aed01f2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
1 '''
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
2 redirects depending on a configuration file an index.meta purl to an viewer
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
3 Created on 01.11.2012
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
4
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
5 @author: dwinter
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
6 '''
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
7
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
8 import web
9
5f5447b3a082 repackaging
dwinter
parents: 5
diff changeset
9 import managePurls.manageIndexMetaPURLs as manageIndexMetaPURLs
3
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
10 import logging
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
11
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
12 class redirector:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
13
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
14 viewers={} # hash mit allen viewer name --> urls
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
15 purlHandler=None
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
16 def __init__(self):
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
17 #read config file for the viewers
11
fad73212354b wsgi addedx
dwinter
parents: 9
diff changeset
18 confFile= file("/usr/local/metadataServices/purlService/restService/viewer.config")
3
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
19
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
20 self.purlHandler = manageIndexMetaPURLs.IndexMetaPURLManager()
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
21
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
22 for line in confFile.readlines():
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
23 splitted=line.split(",")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
24
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
25 list=[]
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
26 if splitted[1]=="":
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
27 list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
28 else:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
29 list.append(splitted[1])
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
30
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
31 if len(splitted)>1: # url fur image viewer
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
32 if splitted[2]=="":
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
33 list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
34 else:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
35 list.append(splitted[2])
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
36 else:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
37 list.append(None) # null wenn keiner konfiguriert wird. TODO: handle this
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
38
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
39
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
40 self.viewers[splitted[0]]=list
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
41
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
42 def GET(self,path):
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
43
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
44 splitted=path.split("/")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
45 if len(splitted)!=2: #pfrad sollte zwei anteile habe "flavour/purl"
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
46 raise web.notfound("not found")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
47
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
48 purl = splitted[1]
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
49 flavour = splitted[0]
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
50
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
51 if flavour not in self.viewers.keys():
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
52 raise web.notfound("no viewer for %s"%flavour)
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
53
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
54 formats = self.viewers[flavour]
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
55
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
56 viewerWithIndexMetaFormatString = formats[0];
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
57 viewerWithImagePathFormatString = formats[1];
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
58
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
59
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
60 # checke ob es einen Image path gibt
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
61 path,validity = self.purlHandler.getImagePathValidity(purl)
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
62 if path is not None and path!="":
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
63 return self.handlePath(path,validity,viewerWithImagePathFormatString)
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
64
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
65
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
66 path,validity = self.purlHandler.getPathValidity(purl)
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
67
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
68 if path is not None and path !="":
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
69 return self.handlePath(path,validity,viewerWithIndexMetaFormatString)
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
70
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
71
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
72
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
73 #handle path
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
74 def handlePath(self,path,validity,viewerFormatString):
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
75
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
76 if viewerFormatString is None or viewerFormatString=="":
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
77 raise web.internalerror("no viewer configure for indexMeta for this flavour")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
78
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
79 if path is None:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
80 raise web.notfound("Cannnot find a URL to this path")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
81
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
82 if validity is manageIndexMetaPURLs.PERM_NON_VALID:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
83 raise web.notfound("PURL NON VALID ANYMORE!")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
84
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
85 if validity is manageIndexMetaPURLs.TEMP_NON_VALID:
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
86 return web.notfound("PURL currently not VALID try later!")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
87
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
88
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
89 viewerUrl = viewerFormatString%path
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
90
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
91 print viewerUrl
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
92 raise web.redirect(viewerUrl,"302 found")
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
93
caeede0c9464 update and redirector
dwinter
parents:
diff changeset
94 if __name__ == '__main__':
11
fad73212354b wsgi addedx
dwinter
parents: 9
diff changeset
95 pass