--- ECHO_content/VLPExtension.py 2008/10/08 08:35:20 1.78 +++ ECHO_content/VLPExtension.py 2012/01/03 13:02:31 1.95 @@ -17,6 +17,8 @@ from types import * from Globals import package_home import transaction import Acquisition +import re +import logging from Products.ECHO_content.ECHO_collection import * from vlp_xmlhelpers import * @@ -163,7 +165,7 @@ def manage_addSendMailForm(self, id, toA class VLP_collectionFolder(Folder): - """klasse fŸr folder innheralb der collection""" + """klasse fuer folder innerhalb der collection""" meta_type="VLP_collectionFolder" class VLP_essay(Folder): @@ -308,7 +310,9 @@ class VLP_essay(Folder): #txt=pages[int(pagenum)-1][1].data.decode('utf-8') txt=pages[int(pagenum)-1][1].data #print txt.encode('utf-8') - return self.xml2html(makeXML(txt),quote="no") + tmp =makeXML(txt) + logging.debug(txt) + return self.xml2html(tmp,quote="no") else: return self.xml2html(makeXML(pages[int(pagenum)-1][1]()),quote="no") @@ -486,9 +490,12 @@ def manage_addVLP_subCollection(self,id, from zope.publisher.interfaces import IPublishTraverse from zope.interface import implements from zope.publisher.interfaces import NotFound -from zope.app import zapi +#from zope.app import zapi from zope.component import queryMultiAdapter -from ZPublisher.BaseRequest import DefaultPublishTraverse +try: + from ZPublisher.BaseRequest import DefaultPublishTraverse +except: + pass class VLP_collection(ECHO_collection,Acquisition.Explicit): """VLP spezifische Erweiterung der Collection""" @@ -496,6 +503,23 @@ class VLP_collection(ECHO_collection,Acq implements(IPublishTraverse) + + + def foxridgePath2vlp(self,url,mk,RESPONSE=None): + """convertiert einen pfad zu einer url in vlp""" + litRe = re.match(r".*lit([0-9]*)",url) + logging.debug("foxridgePath2vlp URL:"+repr(url)) + lit = "lit"+litRe.group(1) + splitted=url.split("/") + path=splitted[-1].split(".")[0] + newUrl=lit+"?p=%s&mk=%s"%(path,mk) + + if RESPONSE: + RESPONSE.redirect(newUrl) + return + + return newUrl + def findObjFromLitName(self,fileName): if not fileName[0:3]=="lit": logging.error("getOrCreateFolder wrong filename: %s"%fileName) @@ -520,20 +544,24 @@ class VLP_collection(ECHO_collection,Acq actual_url=request['ACTUAL_URL'] lastActual=actual_url.split("/")[-1] - + logging.debug("pub_trav:"+actual_url) if name.startswith("lit"): #umleitung wenn lit aufgerufen wirk # umleitung auf den eigentlichen folder nur wenn direkt der Folder litXXX oder dessen index_html methode aufgerufen wird - if lastActual.startswith("lit") or (lastActual=="index_html"): + if lastActual.startswith("lit") or (lastActual=="index_html") or (lastActual=="index_meta") or (lastActual=="getTitle") or (lastActual=="getMetaDataLink") or (lastActual=="hasTranscription") or (lastActual=="transcription"): #obj=self. ZopeFind(self,obj_ids=[name],search_sub=1) ob=self.findObjFromLitName(name) if not ob: return "LIt not found" else: - logging.error("request:"+repr(ob)) + logging.debug("request:"+repr(ob)) request.response.setStatus(200) + #if (lastActual=="index_meta"): + # logging.debug("index_meta requested") + # return ob.index_meta return ob + else: ## andern falls transversiere zum parent. obj = self.aq_parent return obj @@ -553,7 +581,15 @@ class VLP_collection(ECHO_collection,Acq {'label':'Update Library','action':'updateCollection'}, {'label':'Update Metadata','action':'updateCollectionMD'}, ) - + def getAllRessources(self): + """getallressources""" + res=self.ZopeFind(self,obj_metatypes=['VLP_resource'],search_sub=1) + ret=[] + for r in res: + ret.append([r[0],r[1].absolute_url()]) + + return ret + def generateSubCollections(self,errorsTXT="",forceUpdate=False,RESPONSE=None): """erzeuge subcollectionen""" logging.debug("generateSubCollections") @@ -565,23 +601,25 @@ class VLP_collection(ECHO_collection,Acq for found in founds: try: litid = str(found.reference) - foundCol=self.ZopeFind(self,obj_ids=[litid]) + foundCol=self.findObjFromLitName(litid) + #foundCol=self.ZopeFind(self,obj_ids=[litid]) if foundCol: - col = foundCol[0][1] - logging.debug("generateSubCollections: subcollection %s exists (%s)"%(col.getId(),found.reference)) - if (col.title != found.titlerefdisplay) or (col.label != found.titlerefdisplay): + col = foundCol + logging.debug("generateSubCollections: subcollection %s exists (%s)"%(repr(col),found.reference)) + logging.debug(repr([col.title,found.titlerefdisplay,col.label,found.titlerefdisplay])) + if (unicodify(col.title) != unicodify(found.titlerefdisplay)) or (unicodify(col.label) != unicodify(found.titlerefdisplay)): # subcollection seems to have changed logging.debug("generateSubCollections: subcollection has changed, recreating!") - self.manage_delObjects([foundCol[0][0]]) - manage_addVLP_subCollection(self,litid,found.titlerefdisplay,found.titlerefdisplay) + col.aq_parent.manage_delObjects([col.getId()]) + manage_addVLP_subCollection(self.getOrCreateFolderForFile(litid),litid,found.titlerefdisplay,found.titlerefdisplay) else: logging.debug("generateSubCollections: creating new subcollection %s"%found.reference) - manage_addVLP_subCollection(self,litid,found.titlerefdisplay,found.titlerefdisplay) + manage_addVLP_subCollection(self.getOrCreateFolderForFile(litid),litid,found.titlerefdisplay,found.titlerefdisplay) #teste ob es Images auf dem Server gibt mit gleichem Namen (frontmatter) if os.path.exists(os.path.join(self.vlp_basis,litid)): logging.debug("generateSubCollections: found frontmatter in %s"%litid) - obj=getattr(self,litid) + obj=self.findObjFromLitName(litid) if not self.ZopeFind(obj,obj_ids=[litid]): metalink=self.REQUEST['URL1']+"/"+litid+"/"+litid+"/index_meta" newObj=VLP_resource(litid,'',metalink,litid,litid,litid,'generated','book','','','','','','') @@ -592,17 +630,18 @@ class VLP_collection(ECHO_collection,Acq if RESPONSE is not None: self.REQUEST.RESPONSE.write("

%s

\n"%litid) - logging.debug("

%s

\n"%litid) + logging.debug("

%s

\n"%litid) except: error=sys.exc_info()[0:2] logging.error("generateSubCollections: ERROR in %s"%litid) if RESPONSE is not None: + RESPONSE.write("

(ERROR updateII(%s): %s %s TXT: %s)\n

"%(litid,error[0],error[1],sys.exc_info()[2])) RESPONSE.write("

(ERROR (%s): %s %s)

\n"%(litid,error[0],error[1])) - #logging.error("generateSubCollections: %s %s"%error) - #self.REQUEST.RESPONSE.write("(ERROR (%s): %s %s)\n"%(litid,repr(error[0]),repr(error[1]))) + logging.error("generateSubCollections: %s %s"%error) + self.REQUEST.RESPONSE.write("(ERROR (%s): %s %s)\n"%(litid,repr(error[0]),repr(error[1]))) errorsTXT+="

ERROR: No subcollection of %s (ERROR: %s %s)

\n"%(litid,error[0],error[1]) - #errorsTXT+="

No subcollection of %s "%litid+"(ERROR: %s %s)

"%error + errorsTXT+="

No subcollection of %s "%litid+"(ERROR: %s %s)

"%error return errorsTXT @@ -624,8 +663,11 @@ class VLP_collection(ECHO_collection,Acq logging.error("getOrCreateFolder wrong filename: %s"%fileName) return None - - restName="%08d"%int(fileName[3:]) + try: + restName="%08d"%int(fileName[3:]) + except: + logging.error("getOrCreateFolder wrong filename: %s"%fileName) + return None fs=(restName[0:2],restName[0:4],restName[0:6]) @@ -640,8 +682,9 @@ class VLP_collection(ECHO_collection,Acq return current - def updateCollection(self,forceUpdate=False,RESPONSE=None): - """liest verzeichnisse aus dem pfad und legt sie dann als objekte in den ordner""" + def changeOrAddResources(self,forceUpdate=False,RESPONSE=None): + """changes ord updates all resources found in the file system""" + errorsTXT="

Errors

" if RESPONSE is not None: RESPONSE.setHeader('Content-Type','text/html') @@ -698,7 +741,7 @@ class VLP_collection(ECHO_collection,Acq except: error=sys.exc_info()[0:2] if RESPONSE is not None: - RESPONSE.write("(ERROR (%s): %s %s) "%(fileName,error[0],error[1])) + RESPONSE.write("(ERROR Update(%s): %s %s %s) "%(fileName,error[0],error[1],sys.exc_info()[2])) errorsTXT+="

ERROR creating index for %s "%fileName+"(ERROR: %s %s)

\n"%error # update pageSizeSum for subcollections @@ -717,10 +760,16 @@ class VLP_collection(ECHO_collection,Acq error=sys.exc_info()[0:2] logging.error("updateCollection: ERROR in %s (%s %s)"%(fileName,error[0],error[1])) if RESPONSE is not None: - RESPONSE.write("

(ERROR (%s): %s %s)

\n"%(fileName,error[0],error[1])) + RESPONSE.write("(ERROR updateII(%s): %s %s %s) "%(fileName,error[0],error[1],sys.exc_info()[2])) errorsTXT+="

File not created:%s (ERROR: %s %s)

\n"%(fileName,error[0],error[1]) - - # update subcollections + return errorsTXT + + def updateCollection(self,forceUpdate=True,RESPONSE=None): + """liest verzeichnisse aus dem pfad und legt sie dann als objekte in den ordner""" + + # update subcollections + errorsTXT="" + errorsTXT+=self.changeOrAddResources(forceUpdate, RESPONSE); errorsTXT+=self.generateSubCollections(forceUpdate=forceUpdate,RESPONSE=RESPONSE) errorsTXT+="\n" if RESPONSE is not None: @@ -782,9 +831,14 @@ class VLP_resource(ECHO_resource,Cacheab meta_type="VLP_resource" + def _p_resolveConflict(self,oldstate,savedstate,newstate): + logging.debug("updateCollection: Have to resolve conflict!") + return newstate + #vlp_basis="/mpiwg/online/permanent/vlp" + referencetypes=[] manage_options=ECHO_resource.manage_options+Cacheable.manage_options+( @@ -794,17 +848,70 @@ class VLP_resource(ECHO_resource,Cacheab {'label':'Generate Library Template','action':'generateLibraryTemplateHTML'}, ) + def getMetaDataLink(self): + """link auf die metatdaten""" + return self.absolute_url()+"/index_meta" + + def getTitle(self): + """Title der Ressource""" + title= self.ZSQLSimpleSearch("""select title from vl_literature where reference= \'%s\' """ %self.getId())[0].title + logging.debug(title) + return title + + + def getImagePath(self): """Pfad zu den Images""" + path=None if os.path.isdir(os.path.join(self.vlp_basis,self.resourceID,'pageimg')): - return os.path.join(self.vlp_basis,self.resourceID,'pageimg') + path= os.path.join(self.vlp_basis,self.resourceID,'pageimg') elif os.path.isdir(os.path.join(self.vlp_basis,self.resourceID,'pages')): - return os.path.join(self.vlp_basis,self.resourceID,'pages') - else: - return "no images " - + path = os.path.join(self.vlp_basis,self.resourceID,'pages') + # workaround in some cases there seem to be no pictures in pages + + if os.path.isdir(os.path.join(self.vlp_basis,self.resourceID,'pagesHi')): + + + pagesHi = os.listdir(os.path.join(self.vlp_basis,self.resourceID,'pagesHi')) + if path is None: + return os.path.join(self.vlp_basis,self.resourceID,'pagesHi') + + pages = os.listdir(path) + + if len (pagesHi) > len(pages): + + countHi=0 + for p in pagesHi: + dat,ext = os.path.splitext(p) + if ext.lower() in [".tiff",".tif",".jpg"]: + countHi+=1 + count=0 + for p in pages: + dat,ext = os.path.splitext(p) + if ext.lower() in [".tiff",".tif",".jpg"]: + count+=1 + + if countHi > count: + path=os.path.join(self.vlp_basis,self.resourceID,'pagesHi') + + logging.error("pages < pagesHi: %s"%self.resourceID) + if path is None: + + + if len (os.listdir(os.path.join(self.vlp_basis,self.resourceID,'pagesHi'))) > len(os.listdir(path)): + + + + path=os.path.join(self.vlp_basis,self.resourceID,'pagesHi') + logging.error("pages < pagesHi: %s"%self.resourceID) + else: + + logging.error("no images: %s"%self.resourceID) + return None + + return path def transcription(self): @@ -865,7 +972,7 @@ class VLP_resource(ECHO_resource,Cacheab ret+="" re2=ret[0:] - re3=re2.decode('latin-1').encode('utf-8') + re3=self.unicodify(re2) #re3=re2 ft=self.ZopeFind(self,obj_metatypes=['ECHO_fullText']) @@ -875,7 +982,7 @@ class VLP_resource(ECHO_resource,Cacheab else: - self._setObject('fulltext',ECHO_fullText(id,'')) + self._setObject('fulltext',ECHO_fullText('fulltext','')) try: getattr(self,'fulltext').pt_edit(re3.encode('utf-8','ignore'),'text/xml') @@ -886,6 +993,16 @@ class VLP_resource(ECHO_resource,Cacheab else: return "no: %s"%self.getId() + def hasTranscription(self): + """transcription""" + + if not hasattr(self,'fulltext'): + self.copyTranscriptionFromDB() + if not hasattr(self,'fulltext'): #still not + return False + + return True + def resetMetaLink(self,all="no",RESPONSE=None): """resets metalink to standard resp. in case of server change it sets to the new server url""" if all=="yes": @@ -954,21 +1071,31 @@ class VLP_resource(ECHO_resource,Cacheab RESPONSE.redirect('manage_main') - def index_meta(self): + def index_meta(self,RESPONSE=None): """index_meta""" - + pt=PageTemplateFile(os.path.join(package_home(globals()),'vlp','index_meta.zpt')).__of__(self) - pt.content_type="text/html" - return pt() - + pt.content_type="text/xml" + x= pt.pt_render() + #logging.debug("index_meta 4:"+x) + if RESPONSE: + RESPONSE.setHeader("Content-Type","text/xml") + return x def file2page(self,p): """converts filename to pagenumber""" - dom=xml.dom.minidom.parse(ECHO_helpers.urlopen(getattr(self,'index.xml').absolute_url())) + + #dom=xml.dom.minidom.parse(ECHO_helpers.urlopen(getattr(self,'index.xml').absolute_url())) + index=getattr(self,'index.xml') + + txt=index.pt_render() + dom=xml.dom.minidom.parseString(txt) for page in dom.getElementsByTagName('page'): # pageummer mit lo oder hi sind aequivalent if (page.getAttribute('file')==p) or ("%slo"%page.getAttribute('file')==p) or ("%shi"%page.getAttribute('file')==p): - return page.getAttribute('nr') + nr=page.getAttribute('nr') + + return nr return 0 @@ -1021,13 +1148,14 @@ class VLP_resource(ECHO_resource,Cacheab ret="%s: %s"%(str[0],name) return ret - try: - ret=os.listdir(os.path.join(self.vlp_basis,self.resourceID,'pageimg')) - self.imagePath='pageimg' - except: - ret=os.listdir(os.path.join(self.vlp_basis,self.resourceID,'pages')) - self.imagePath='pages' + self.imagePathFull=self.getImagePath(); + self.imagePath=self.imagePathFull.split("/")[-1] + if (self.imagePath is None): + return None + temp=[] + ret = os.listdir(self.imagePathFull) + ret.sort() for x in ret: if (not (x[0] in ('.',':'))) and (not x[0:4] in excludeNames): @@ -1146,7 +1274,10 @@ class VLP_resource(ECHO_resource,Cacheab zt=getattr(self,'index.xml') if not forceUpdate: # check if directory is newer (self.imagePath sould be set by now) - imgdir = os.path.join(self.vlp_basis, self.resourceID, self.imagePath) + path = self.getImagePath() + if (path is None): + return "CREATE INDEX FILE NOT POSSIBLE" + imgdir = os.path.join(path) dirtime = os.path.getmtime(imgdir) zttime = zt.bobobase_modification_time() logging.debug("createindexfile: dir %s of %s index of %s"%(imgdir,dirtime,zttime))