Mercurial > hg > purlService
view managePurls/manageIndexMetaPURLs.py @ 12:0287aed01f2b
config and minor bugs
author | dwinter |
---|---|
date | Tue, 06 Nov 2012 08:41:18 +0100 |
parents | fad73212354b |
children | a2bcca6539fd |
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 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="localhost") 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.gneratePurl() 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=""): 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()")) return update def register(self,path,isIndexMeta,imagePath="",server_url="",user="",update=False): #teste ob es zu dem Pfad schon eine Purl gibt purl = self.getPurl(path) if purl!=None: if update: up= self.updatePurl(purl, isIndexMeta, path, imagePath, server_url, user) 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 if __name__ == '__main__': im = IndexMetaPURLManager() print im.register("/tmp3/index.meta", True, "", "dwinter") pass