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