Mercurial > hg > purlService
annotate managePurls/manageIndexMetaPURLs.py @ 37:6a0873a913c5
register updated die dri, wenn sie nicht gleich der gespeicherten dri ist, auch wenn update nicht gleich true ist.
author | dwinter |
---|---|
date | Mon, 03 Mar 2014 18:07:08 +0100 |
parents | be8640c08d99 |
children | 6d0d7f1c11f2 |
rev | line source |
---|---|
0 | 1 ''' |
2 Created on 31.10.2012 | |
3 | |
4 @author: dwinter | |
5 ''' | |
6 | |
7 import web | |
8 import random | |
9 | |
10 ALREADY_EXISTS=0 | |
11 NEW_PURL=1 | |
12 ERROR=-1 | |
3 | 13 UPDATED=2 |
14 | |
0 | 15 PURL_PREFIX="MPIWG:" |
2 | 16 |
17 VALID=1 | |
18 TEMP_NON_VALID=0 | |
19 PERM_NON_VALID=-1 | |
28
246049db5466
getImagePath askes now the md server for the image path
dwinter
parents:
22
diff
changeset
|
20 NO_METADATA_FOUND=-2 |
246049db5466
getImagePath askes now the md server for the image path
dwinter
parents:
22
diff
changeset
|
21 NO_IMAGE_PATH_FOUND=-3 |
0 | 22 |
23 class IndexMetaPURLManager: | |
24 | |
25 # register a new path to htmk | |
26 # should start with /mpiwg/online/ oder direct mit /permanent/ oder /archive/ | |
27 # return tuple: status, result | |
28 | |
29 purlDB=None | |
30 # DB has fields: | |
31 # purl purl | |
32 # path url or path to indexMeta | |
3 | 33 # imagePath dezidierter Pfad auf images. |
0 | 34 # is_Index_meta bolean |
35 # created_by | |
36 # created_at | |
37 # last_change | |
38 # validity 1 if it is valid, 0 if temporary invalid, -1 if permanetly invalid | |
39 # server_url base_url of server | |
3 | 40 |
0 | 41 |
42 def __init__(self): | |
22 | 43 self.purlDB = web.database(dbn="postgres", db="purlDB",user="purlUSER",password="p*lWa55eR", host="tuxserve03") |
44 #self.purlDB = web.database(dbn="postgres", db="purlDB",user="purlUSER",password="3333") | |
0 | 45 |
8 | 46 |
0 | 47 |
48 def getPath(self,purl): | |
49 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) | |
50 if urls is None or len(urls)==0: | |
51 return None | |
52 else: | |
53 return urls[0]['path'] | |
2 | 54 |
3 | 55 def getImagePath(self,purl): |
56 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) | |
57 if urls is None or len(urls)==0: | |
58 return None | |
59 else: | |
60 return urls[0]['image_path'] | |
61 | |
2 | 62 def isIndexMeta(self,purl): |
63 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) | |
64 if urls is None or len(urls)==0: | |
65 return False | |
66 else: | |
67 return urls[0]['is_index_meta'] | |
0 | 68 |
3 | 69 def getImagePathValidity(self,purl): |
70 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) | |
71 if urls is None or len(urls)==0: | |
72 return None,-1 | |
73 else: | |
74 res = urls[0] | |
75 return res['image_path'],res['validity'] | |
76 | |
2 | 77 |
78 def getPathValidity(self,purl): | |
79 urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) | |
80 if urls is None or len(urls)==0: | |
81 return None,-1 | |
82 else: | |
83 res = urls[0] | |
84 return res['path'],res['validity'] | |
85 | |
0 | 86 #get purl attached tp a path or URL, return None if none. |
87 | |
88 #checke if purl exist | |
89 def exists(self,purl): | |
90 if self.getPath(purl)==None: | |
91 return False | |
92 return True | |
93 | |
94 | |
95 def getPurl(self,path): | |
1 | 96 #urls = self.purlDB.select('"purls"',where="path=%s"%web.sqlquote(path.replace("'")) |
97 urls = self.purlDB.query("select * from purls where path=$path",vars={'path':path}) | |
8 | 98 |
0 | 99 if urls is None or len(urls)==0: |
100 return None | |
101 else: | |
102 return urls[0]['purl'] | |
103 | |
104 | |
8 | 105 |
106 def search(self,query): | |
107 purls = self.purlDB.query("select purl from purls where path like $path",vars={'path':query}) | |
108 if purls is None or len(purls)==0: | |
109 return None | |
110 else: | |
111 return purls | |
112 | |
113 | |
0 | 114 def generatePurl(self): |
115 | |
116 | |
117 driEncode={ 0:'0', | |
118 1:'1', | |
119 2:'2', | |
120 3:'3', | |
121 4:'4', | |
122 5:'5', | |
123 6:'6', | |
124 7:'7', | |
125 8:'8', | |
126 9:'9', | |
127 10:'A', | |
128 11:'B', | |
129 12:'C', | |
130 13:'D', | |
131 14:'E', | |
132 15:'F', | |
133 16:'G', | |
134 17:'H', | |
135 18:'K', | |
136 19:'M', | |
137 20:'N', | |
138 21:'P', | |
139 22:'Q', | |
140 23:'R', | |
141 24:'S', | |
142 25:'T', | |
143 26:'U', | |
144 27:'V', | |
145 28:'W', | |
146 29:'X', | |
147 30:'Y', | |
148 31:'Z' | |
149 } | |
150 | |
151 random.seed() | |
152 x=[] | |
153 for i in range(7): | |
154 x.append(random.randint(0,31)) | |
155 | |
156 sm=0 | |
157 for i in range(7): | |
158 sm+=(i+1)*x[i] | |
159 | |
160 c=sm % 31 | |
161 nid="" | |
162 for i in range(7): | |
163 nid+=driEncode[x[i]] | |
164 nid+=driEncode[c] | |
165 return PURL_PREFIX+nid | |
166 | |
167 | |
168 | |
169 | |
170 #generate purl and add it to the database | |
3 | 171 def createPurl(self,path,isIndexMeta,imagePath="",server_url="",user=""): |
0 | 172 |
173 purl = self.generatePurl() | |
174 | |
175 #erzeuge eine neue purl | |
176 while self.exists(purl): | |
36 | 177 purl = self.generatePurl() |
0 | 178 |
179 | |
3 | 180 seq= self.purlDB.insert('purls',path=path,purl=purl,is_index_meta=isIndexMeta, image_path=imagePath, |
0 | 181 server_url=server_url,validity=1,created_by=user,created_at=web.SQLLiteral("NOW()")) |
182 | |
183 | |
184 return purl | |
185 | |
186 #register a new path | |
3 | 187 |
188 | |
36 | 189 def updatePurl(self,purl,isIndexMeta,path="",imagePath="",server_url="",user="",isDri=False): |
3 | 190 |
191 update= self.purlDB.update('purls',where="purl = '%s'"%web.sqlparam(purl),path=path,is_index_meta=isIndexMeta, image_path=imagePath, | |
36 | 192 server_url=server_url,validity=1,last_change_by=user,last_change_at=web.SQLLiteral("NOW()"),isdri=isDri) |
193 | |
194 | |
195 return update | |
196 | |
197 | |
198 def updatePurlForPath(self,purl,isIndexMeta,path="",imagePath="",server_url="",user=""): | |
199 """schreibt zu einem Pfad die entsprechende purl""" | |
200 | |
201 | |
202 update= self.purlDB.update('purls',where="path = '%s'"%web.sqlparam(path),purl=purl,is_index_meta=isIndexMeta, image_path=imagePath, | |
3 | 203 server_url=server_url,validity=1,last_change_by=user,last_change_at=web.SQLLiteral("NOW()")) |
204 | |
205 | |
206 return update | |
207 | |
36 | 208 def register(self,path=None,isIndexMeta=False,imagePath="",server_url="",user="",driIndexMeta=None,update=False): |
0 | 209 |
210 | |
211 #teste ob es zu dem Pfad schon eine Purl gibt | |
19 | 212 |
36 | 213 if path: # wenn ein pfad definiert ist teste ob es schon eine purl in der Datenbank gibt. |
214 purl = self.getPurl(path) | |
215 | |
19 | 216 else: |
217 purl =None | |
36 | 218 |
37
6a0873a913c5
register updated die dri, wenn sie nicht gleich der gespeicherten dri ist, auch wenn update nicht gleich true ist.
dwinter
parents:
36
diff
changeset
|
219 # wenn update compare this with the purl which is stored in the index.meta file. |
6a0873a913c5
register updated die dri, wenn sie nicht gleich der gespeicherten dri ist, auch wenn update nicht gleich true ist.
dwinter
parents:
36
diff
changeset
|
220 if (driIndexMeta is not None) & (purl!=driIndexMeta): |
36 | 221 #wenn nicht gleich, dann trage die driIndexMeta in die purl db! |
222 purl=driIndexMeta | |
37
6a0873a913c5
register updated die dri, wenn sie nicht gleich der gespeicherten dri ist, auch wenn update nicht gleich true ist.
dwinter
parents:
36
diff
changeset
|
223 update=True |
36 | 224 |
225 | |
0 | 226 if purl!=None: |
3 | 227 |
228 if update: | |
36 | 229 up= self.updatePurl(driIndexMeta, isIndexMeta, path, imagePath, server_url, user,isDri=True) |
230 if up>0: | |
231 return UPDATED,purl | |
232 else: | |
233 return ERROR,None | |
234 | |
0 | 235 return ALREADY_EXISTS,purl |
36 | 236 |
0 | 237 |
238 #wenn nicht dann neue erzeugen | |
239 else: | |
3 | 240 purl = self.createPurl(path,isIndexMeta,imagePath=imagePath,user=user,server_url=server_url) |
0 | 241 if purl!=None: |
242 return NEW_PURL,purl | |
243 | |
244 else: | |
245 return ERROR,None | |
246 | |
247 | |
30 | 248 def getExistingRandom(self,number): |
249 """gibt zufaellig existierende purls zurueck""" | |
250 | |
251 qst = "select count(*) from purls" | |
252 max = self.purlDB.query(qst)[0].count | |
253 | |
254 random.seed() | |
255 | |
256 ret=set() | |
257 | |
258 while len(ret)<number: | |
259 zuf = random.randrange(max-1) | |
260 | |
261 qst="select purl from purls OFFSET %s LIMIT 1"%zuf | |
262 purl=self.purlDB.query(qst)[0].purl | |
263 ret.add(purl) | |
264 | |
265 return [x for x in ret] | |
266 | |
267 def getLastEntries(self,number): | |
268 | |
269 qst ="select purl from purls order by created_at limit %s"%number | |
270 purls=self.purlDB.query(qst) | |
271 ret=[] | |
272 for purl in purls: | |
273 ret.append(purl.purl) | |
274 | |
275 return ret | |
276 | |
0 | 277 if __name__ == '__main__': |
278 | |
279 im = IndexMetaPURLManager() | |
280 print im.register("/tmp3/index.meta", True, "", "dwinter") | |
4 | 281 pass |