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