Mercurial > hg > purlService
view managePurls/manageIndexMetaPURLs.py @ 40:671dd1e4bd09 default tip
minor bug
author | dwinter |
---|---|
date | Wed, 05 Mar 2014 10:20:54 +0100 |
parents | 6d0d7f1c11f2 |
children |
line wrap: on
line source
''' Created on 31.10.2012 @author: dwinter ''' import web import random ALREADY_EXISTS=0 NEW_PURL=1 ERROR=-1 UPDATED=2 PURL_PREFIX="MPIWG:" VALID=1 TEMP_NON_VALID=0 PERM_NON_VALID=-1 NO_METADATA_FOUND=-2 NO_IMAGE_PATH_FOUND=-3 class IndexMetaPURLManager: # register a new path to htmk # should start with /mpiwg/online/ oder direct mit /permanent/ oder /archive/ # return tuple: status, result purlDB=None # DB has fields: # purl purl # path url or path to indexMeta # imagePath dezidierter Pfad auf images. # is_Index_meta bolean # created_by # created_at # last_change # validity 1 if it is valid, 0 if temporary invalid, -1 if permanetly invalid # server_url base_url of server def __init__(self): self.purlDB = web.database(dbn="postgres", db="purlDB",user="purlUSER",password="p*lWa55eR", host="tuxserve03") #self.purlDB = web.database(dbn="postgres", db="purlDB",user="purlUSER",password="3333") def getPath(self,purl): urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) if urls is None or len(urls)==0: return None else: return urls[0]['path'] def getImagePath(self,purl): urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) if urls is None or len(urls)==0: return None else: return urls[0]['image_path'] def isIndexMeta(self,purl): urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) if urls is None or len(urls)==0: return False else: return urls[0]['is_index_meta'] def getImagePathValidity(self,purl): urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) if urls is None or len(urls)==0: return None,-1 else: res = urls[0] return res['image_path'],res['validity'] def getPathValidity(self,purl): urls = self.purlDB.select('"purls"' ,where="purl='%s'"%purl) if urls is None or len(urls)==0: return None,-1 else: res = urls[0] return res['path'],res['validity'] #get purl attached tp a path or URL, return None if none. #checke if purl exist def exists(self,purl): if self.getPath(purl)==None: return False return True def getPurl(self,path): #urls = self.purlDB.select('"purls"',where="path=%s"%web.sqlquote(path.replace("'")) urls = self.purlDB.query("select * from purls where path=$path",vars={'path':path}) if urls is None or len(urls)==0: return None else: return urls[0]['purl'] def search(self,query): purls = self.purlDB.query("select purl from purls where path like $path",vars={'path':query}) if purls is None or len(purls)==0: return None else: return purls def generatePurl(self): driEncode={ 0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F', 16:'G', 17:'H', 18:'K', 19:'M', 20:'N', 21:'P', 22:'Q', 23:'R', 24:'S', 25:'T', 26:'U', 27:'V', 28:'W', 29:'X', 30:'Y', 31:'Z' } random.seed() x=[] for i in range(7): x.append(random.randint(0,31)) sm=0 for i in range(7): sm+=(i+1)*x[i] c=sm % 31 nid="" for i in range(7): nid+=driEncode[x[i]] nid+=driEncode[c] return PURL_PREFIX+nid #generate purl and add it to the database def createPurl(self,path,isIndexMeta,imagePath="",server_url="",user=""): purl = self.generatePurl() #erzeuge eine neue purl while self.exists(purl): purl = self.generatePurl() seq= self.purlDB.insert('purls',path=path,purl=purl,is_index_meta=isIndexMeta, image_path=imagePath, server_url=server_url,validity=1,created_by=user,created_at=web.SQLLiteral("NOW()")) return purl #register a new path def updatePurl(self,purl,isIndexMeta,path="",imagePath="",server_url="",user="",isDri=False): update= self.purlDB.update('purls',where="purl = '%s'"%web.sqlparam(purl),path=path,is_index_meta=isIndexMeta, image_path=imagePath, server_url=server_url,validity=1,last_change_by=user,last_change_at=web.SQLLiteral("NOW()"),isdri=isDri) return update def updatePurlForPath(self,purl,isIndexMeta,path="",imagePath="",server_url="",user=""): """schreibt zu einem Pfad die entsprechende purl""" update= self.purlDB.update('purls',where="path = '%s'"%web.sqlparam(path),purl=purl,is_index_meta=isIndexMeta, image_path=imagePath, server_url=server_url,validity=1,last_change_by=user,last_change_at=web.SQLLiteral("NOW()")) return update def register(self,path=None,isIndexMeta=False,imagePath="",server_url="",user="",driIndexMeta=None,update=False): #teste ob es zu dem Pfad schon eine Purl gibt if path: # wenn ein pfad definiert ist teste ob es schon eine purl in der Datenbank gibt. purl = self.getPurl(path) else: purl =None # wenn update compare this with the purl which is stored in the index.meta file. if (driIndexMeta is not None) & (purl!=driIndexMeta): #wenn nicht gleich, dann trage die driIndexMeta in die purl db! purl=driIndexMeta update=True if purl!=None: if update: up= self.updatePurl(driIndexMeta, isIndexMeta, path, imagePath, server_url, user,isDri=True) if up>0: return UPDATED,purl else: return ERROR,None return ALREADY_EXISTS,purl #wenn nicht dann neue erzeugen else: purl = self.createPurl(path,isIndexMeta,imagePath=imagePath,user=user,server_url=server_url) if purl!=None: return NEW_PURL,purl else: return ERROR,None def getExistingRandom(self,number): """gibt zufaellig existierende purls zurueck""" qst = "select count(*) from purls" max = self.purlDB.query(qst)[0].count random.seed() ret=set() while len(ret)<number: zuf = random.randrange(max-1) qst="select purl from purls OFFSET %s LIMIT 1"%zuf purl=self.purlDB.query(qst)[0].purl ret.add(purl) return [x for x in ret] def getLastEntries(self,number): qst ="select purl from purls order by created_at limit %s"%number purls=self.purlDB.query(qst) ret=[] for purl in purls: ret.append(purl.purl) return ret def getAllPurls(self,limit=None,offset=None,isDri=True): results = self.purlDB.where('purls',what="purl,path",limit=limit,offset=offset,isdri=isDri) ret = [] for result in results: ret.append({'path':result.path,'dri':result.purl}) return ret if __name__ == '__main__': im = IndexMetaPURLManager() #print im.register("/tmp3/index.meta", True, "", "dwinter") print im.getAllPurls() pass