annotate managePurls/manageIndexMetaPURLs.py @ 9:5f5447b3a082

repackaging
author dwinter
date Fri, 02 Nov 2012 15:37:17 +0100
parents manageIndexMetaPURLs.py@733d43b30a82
children fad73212354b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dwinter
parents:
diff changeset
1 '''
dwinter
parents:
diff changeset
2 Created on 31.10.2012
dwinter
parents:
diff changeset
3
dwinter
parents:
diff changeset
4 @author: dwinter
dwinter
parents:
diff changeset
5 '''
dwinter
parents:
diff changeset
6
dwinter
parents:
diff changeset
7 import web
dwinter
parents:
diff changeset
8 import random
dwinter
parents:
diff changeset
9
dwinter
parents:
diff changeset
10 ALREADY_EXISTS=0
dwinter
parents:
diff changeset
11 NEW_PURL=1
dwinter
parents:
diff changeset
12 ERROR=-1
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
13 UPDATED=2
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
14
0
dwinter
parents:
diff changeset
15 PURL_PREFIX="MPIWG:"
2
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
16
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
17 VALID=1
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
18 TEMP_NON_VALID=0
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
19 PERM_NON_VALID=-1
0
dwinter
parents:
diff changeset
20
dwinter
parents:
diff changeset
21 class IndexMetaPURLManager:
dwinter
parents:
diff changeset
22
dwinter
parents:
diff changeset
23 # register a new path to htmk
dwinter
parents:
diff changeset
24 # should start with /mpiwg/online/ oder direct mit /permanent/ oder /archive/
dwinter
parents:
diff changeset
25 # return tuple: status, result
dwinter
parents:
diff changeset
26
dwinter
parents:
diff changeset
27 purlDB=None
dwinter
parents:
diff changeset
28 # DB has fields:
dwinter
parents:
diff changeset
29 # purl purl
dwinter
parents:
diff changeset
30 # path url or path to indexMeta
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
31 # imagePath dezidierter Pfad auf images.
0
dwinter
parents:
diff changeset
32 # is_Index_meta bolean
dwinter
parents:
diff changeset
33 # created_by
dwinter
parents:
diff changeset
34 # created_at
dwinter
parents:
diff changeset
35 # last_change
dwinter
parents:
diff changeset
36 # validity 1 if it is valid, 0 if temporary invalid, -1 if permanetly invalid
dwinter
parents:
diff changeset
37 # server_url base_url of server
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
38
0
dwinter
parents:
diff changeset
39
dwinter
parents:
diff changeset
40 def __init__(self):
8
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
41 self.purlDB = web.database(dbn="postgres", db="purlDB",user="purlUSER",password="3333", host="localhost")
0
dwinter
parents:
diff changeset
42
8
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
43
0
dwinter
parents:
diff changeset
44
dwinter
parents:
diff changeset
45 def getPath(self,purl):
dwinter
parents:
diff changeset
46 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl)
dwinter
parents:
diff changeset
47 if urls is None or len(urls)==0:
dwinter
parents:
diff changeset
48 return None
dwinter
parents:
diff changeset
49 else:
dwinter
parents:
diff changeset
50 return urls[0]['path']
2
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
51
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
52 def getImagePath(self,purl):
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
53 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl)
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
54 if urls is None or len(urls)==0:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
55 return None
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
56 else:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
57 return urls[0]['image_path']
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
58
2
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
59 def isIndexMeta(self,purl):
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
60 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl)
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
61 if urls is None or len(urls)==0:
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
62 return False
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
63 else:
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
64 return urls[0]['is_index_meta']
0
dwinter
parents:
diff changeset
65
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
66 def getImagePathValidity(self,purl):
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
67 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl)
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
68 if urls is None or len(urls)==0:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
69 return None,-1
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
70 else:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
71 res = urls[0]
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
72 return res['image_path'],res['validity']
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
73
2
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
74
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
75 def getPathValidity(self,purl):
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
76 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl)
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
77 if urls is None or len(urls)==0:
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
78 return None,-1
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
79 else:
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
80 res = urls[0]
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
81 return res['path'],res['validity']
fb2a3b4542a4 restservice
dwinter
parents: 1
diff changeset
82
0
dwinter
parents:
diff changeset
83 #get purl attached tp a path or URL, return None if none.
dwinter
parents:
diff changeset
84
dwinter
parents:
diff changeset
85 #checke if purl exist
dwinter
parents:
diff changeset
86 def exists(self,purl):
dwinter
parents:
diff changeset
87 if self.getPath(purl)==None:
dwinter
parents:
diff changeset
88 return False
dwinter
parents:
diff changeset
89 return True
dwinter
parents:
diff changeset
90
dwinter
parents:
diff changeset
91
dwinter
parents:
diff changeset
92 def getPurl(self,path):
1
fef9ad4020ee sql change
dwinter
parents: 0
diff changeset
93 #urls = self.purlDB.select('"purls"',where="path=%s"%web.sqlquote(path.replace("'"))
fef9ad4020ee sql change
dwinter
parents: 0
diff changeset
94 urls = self.purlDB.query("select * from purls where path=$path",vars={'path':path})
8
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
95
0
dwinter
parents:
diff changeset
96 if urls is None or len(urls)==0:
dwinter
parents:
diff changeset
97 return None
dwinter
parents:
diff changeset
98 else:
dwinter
parents:
diff changeset
99 return urls[0]['purl']
dwinter
parents:
diff changeset
100
dwinter
parents:
diff changeset
101
8
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
102
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
103 def search(self,query):
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
104 purls = self.purlDB.query("select purl from purls where path like $path",vars={'path':query})
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
105 if purls is None or len(purls)==0:
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
106 return None
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
107 else:
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
108 return purls
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
109
733d43b30a82 connection handling changed
dwinter
parents: 7
diff changeset
110
0
dwinter
parents:
diff changeset
111 def generatePurl(self):
dwinter
parents:
diff changeset
112
dwinter
parents:
diff changeset
113
dwinter
parents:
diff changeset
114 driEncode={ 0:'0',
dwinter
parents:
diff changeset
115 1:'1',
dwinter
parents:
diff changeset
116 2:'2',
dwinter
parents:
diff changeset
117 3:'3',
dwinter
parents:
diff changeset
118 4:'4',
dwinter
parents:
diff changeset
119 5:'5',
dwinter
parents:
diff changeset
120 6:'6',
dwinter
parents:
diff changeset
121 7:'7',
dwinter
parents:
diff changeset
122 8:'8',
dwinter
parents:
diff changeset
123 9:'9',
dwinter
parents:
diff changeset
124 10:'A',
dwinter
parents:
diff changeset
125 11:'B',
dwinter
parents:
diff changeset
126 12:'C',
dwinter
parents:
diff changeset
127 13:'D',
dwinter
parents:
diff changeset
128 14:'E',
dwinter
parents:
diff changeset
129 15:'F',
dwinter
parents:
diff changeset
130 16:'G',
dwinter
parents:
diff changeset
131 17:'H',
dwinter
parents:
diff changeset
132 18:'K',
dwinter
parents:
diff changeset
133 19:'M',
dwinter
parents:
diff changeset
134 20:'N',
dwinter
parents:
diff changeset
135 21:'P',
dwinter
parents:
diff changeset
136 22:'Q',
dwinter
parents:
diff changeset
137 23:'R',
dwinter
parents:
diff changeset
138 24:'S',
dwinter
parents:
diff changeset
139 25:'T',
dwinter
parents:
diff changeset
140 26:'U',
dwinter
parents:
diff changeset
141 27:'V',
dwinter
parents:
diff changeset
142 28:'W',
dwinter
parents:
diff changeset
143 29:'X',
dwinter
parents:
diff changeset
144 30:'Y',
dwinter
parents:
diff changeset
145 31:'Z'
dwinter
parents:
diff changeset
146 }
dwinter
parents:
diff changeset
147
dwinter
parents:
diff changeset
148 random.seed()
dwinter
parents:
diff changeset
149 x=[]
dwinter
parents:
diff changeset
150 for i in range(7):
dwinter
parents:
diff changeset
151 x.append(random.randint(0,31))
dwinter
parents:
diff changeset
152
dwinter
parents:
diff changeset
153 sm=0
dwinter
parents:
diff changeset
154 for i in range(7):
dwinter
parents:
diff changeset
155 sm+=(i+1)*x[i]
dwinter
parents:
diff changeset
156
dwinter
parents:
diff changeset
157 c=sm % 31
dwinter
parents:
diff changeset
158 nid=""
dwinter
parents:
diff changeset
159 for i in range(7):
dwinter
parents:
diff changeset
160 nid+=driEncode[x[i]]
dwinter
parents:
diff changeset
161 nid+=driEncode[c]
dwinter
parents:
diff changeset
162 return PURL_PREFIX+nid
dwinter
parents:
diff changeset
163
dwinter
parents:
diff changeset
164
dwinter
parents:
diff changeset
165
dwinter
parents:
diff changeset
166
dwinter
parents:
diff changeset
167 #generate purl and add it to the database
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
168 def createPurl(self,path,isIndexMeta,imagePath="",server_url="",user=""):
0
dwinter
parents:
diff changeset
169
dwinter
parents:
diff changeset
170 purl = self.generatePurl()
dwinter
parents:
diff changeset
171
dwinter
parents:
diff changeset
172 #erzeuge eine neue purl
dwinter
parents:
diff changeset
173 while self.exists(purl):
dwinter
parents:
diff changeset
174 purl = self.gneratePurl()
dwinter
parents:
diff changeset
175
dwinter
parents:
diff changeset
176
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
177 seq= self.purlDB.insert('purls',path=path,purl=purl,is_index_meta=isIndexMeta, image_path=imagePath,
0
dwinter
parents:
diff changeset
178 server_url=server_url,validity=1,created_by=user,created_at=web.SQLLiteral("NOW()"))
dwinter
parents:
diff changeset
179
dwinter
parents:
diff changeset
180
dwinter
parents:
diff changeset
181 return purl
dwinter
parents:
diff changeset
182
dwinter
parents:
diff changeset
183 #register a new path
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
184
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
185
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
186 def updatePurl(self,purl,isIndexMeta,path="",imagePath="",server_url="",user=""):
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
187
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
188 update= self.purlDB.update('purls',where="purl = '%s'"%web.sqlparam(purl),path=path,is_index_meta=isIndexMeta, image_path=imagePath,
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
189 server_url=server_url,validity=1,last_change_by=user,last_change_at=web.SQLLiteral("NOW()"))
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
190
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
191
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
192 return update
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
193
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
194 def register(self,path,isIndexMeta,imagePath="",server_url="",user="",update=False):
0
dwinter
parents:
diff changeset
195
dwinter
parents:
diff changeset
196
dwinter
parents:
diff changeset
197 #teste ob es zu dem Pfad schon eine Purl gibt
dwinter
parents:
diff changeset
198 purl = self.getPurl(path)
dwinter
parents:
diff changeset
199 if purl!=None:
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
200
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
201 if update:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
202 up= self.updatePurl(purl, isIndexMeta, path, imagePath, server_url, user)
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
203 if up>0:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
204 return UPDATED,purl
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
205 else:
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
206 return ERROR,None
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
207
0
dwinter
parents:
diff changeset
208 return ALREADY_EXISTS,purl
dwinter
parents:
diff changeset
209
dwinter
parents:
diff changeset
210
dwinter
parents:
diff changeset
211 #wenn nicht dann neue erzeugen
dwinter
parents:
diff changeset
212 else:
3
caeede0c9464 update and redirector
dwinter
parents: 2
diff changeset
213 purl = self.createPurl(path,isIndexMeta,imagePath=imagePath,user=user,server_url=server_url)
0
dwinter
parents:
diff changeset
214 if purl!=None:
dwinter
parents:
diff changeset
215 return NEW_PURL,purl
dwinter
parents:
diff changeset
216
dwinter
parents:
diff changeset
217 else:
dwinter
parents:
diff changeset
218 return ERROR,None
dwinter
parents:
diff changeset
219
dwinter
parents:
diff changeset
220
dwinter
parents:
diff changeset
221
dwinter
parents:
diff changeset
222
dwinter
parents:
diff changeset
223 if __name__ == '__main__':
dwinter
parents:
diff changeset
224
dwinter
parents:
diff changeset
225 im = IndexMetaPURLManager()
dwinter
parents:
diff changeset
226 print im.register("/tmp3/index.meta", True, "", "dwinter")
4
107f13ca333b try except added
dwinter
parents: 3
diff changeset
227 pass