--- cdli/cdli_files.py 2009/08/31 14:06:42 1.113 +++ cdli/cdli_files.py 2011/07/20 09:33:35 1.119 @@ -90,7 +90,7 @@ class CDLIFileObject(CatalogAware,extVer def getPNumber(self): """get the pnumber""" try: - txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:]) + txt=re.match("&[PpSs](\d*)\s*=([^\r\n]*)",self.getData()[0:]) except: txt=self.getData()[0:] @@ -104,7 +104,7 @@ class CDLIFileObject(CatalogAware,extVer def getDesignation(self): """get the designation out of the file""" try: - txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:]) + txt=re.match("&[PpSs](\d*)\s*=([^\r\n]*)",self.getData()[0:]) except: txt=self.getData()[0:] @@ -306,10 +306,15 @@ def manage_addCDLIFile(self,id,title,loc def checkUTF8(data): """check utf 8""" + if not isinstance(data, str): + logging.error("checkUTF8 data is not string! (%s)"%repr(data)) + try: - data.encode('utf-8') + data.decode('utf-8') + logging.debug("checkUTF8: ok!") return True except: + logging.debug("checkUTF8: false!") return False @@ -318,8 +323,8 @@ def checkFile(filename,data,folder): # first check the file name fn=filename.split(".") # no extension - if not fn[0][0]=="P": - return False,"P missing in the filename" + if not (fn[0][0]=="P" or fn[0][0]=="S"): + return False,"P/S missing in the filename" elif len(fn[0])!=7: return False,"P number has not the right length 6" elif not checkUTF8(data): @@ -335,7 +340,7 @@ def splitatf(fh,dir=None,ext=None): i=0 #ROC: why split \n first and then \r??? - if (type(fh) is StringType) or (type(fh) is UnicodeType): + if isinstance(fh, basestring): iter=fh.split("\n") else: iter=fh.readlines() @@ -369,7 +374,7 @@ def splitatf(fh,dir=None,ext=None): if dir: filename=os.path.join(dir,filename) nf=file(filename,"w") - logging.info("open %s"%filename) + logging.debug("open %s"%filename) if nf: nf.write(line.replace("\n","")+"\n") @@ -378,8 +383,9 @@ def splitatf(fh,dir=None,ext=None): except: pass - if not((type(fh) is StringType) or (type(fh) is UnicodeType)): + if not isinstance(fh, basestring): fh.close() + return ret,len(os.listdir(dir)) @@ -802,7 +808,8 @@ class CDLIRoot(Folder): return fileId x=self.v_files.get(fileId) logging.debug("obj: "+repr(x)) - if x==None: + if x is None: + logging.debug("fileId"+repr(fileId)) folder=fileId[0:3] f2=fileId[0:5] fObj = getattr(self.cdliRoot.cdli_main,folder); @@ -857,8 +864,17 @@ class CDLIRoot(Folder): self.v_files_lastVersion.update({obj.getId():obj.getLastVersion()}) self.v_file_ids.add(obj.getId()) - logging.debug("update:"+obj.getId()+"XXX"+repr(obj)) + #change everthing around to make it persistent... + tmp = self.v_files + self.v_files=tmp + tmp2=self.v_file_ids + self.v_file_ids=tmp2 + + self.CDLICache.cleanCache() #be sure that the cache is clean + logging.debug("update:"+obj.getId()+"XXX"+repr(obj)) + + def deleteFromBTree(self,objId): """delete an obj""" self.v_files.pop(objId) @@ -879,8 +895,9 @@ class CDLIRoot(Folder): - def searchText(self, query, index='graphemes'): - """searches query in the fulltext index and returns a list of file ids/P-numbers""" + def searchText(self, query, index='graphemes', resultFilter=None): + """searches query in the fulltext index and returns a list of file IDs/P-numbers + resultFilter is matched against the beginning of the file ID""" # see also: http://www.plope.com/Books/2_7Edition/SearchingZCatalog.stx#2-13 logging.debug("searchtext for '%s' in index %s"%(query,index)) #import Products.ZCTextIndex.QueryParser @@ -890,8 +907,11 @@ class CDLIRoot(Folder): idx = getattr(self, self.file_catalog) # do search resultset = idx.search(query_request=idxQuery,sort_index='textid') - # put only the P-Number in the result - results = [res.getId[:7] for res in resultset] + # put only the P-Number in the result + if resultFilter is None: + results = [res.getId[:7] for res in resultset] + else: + results = [res.getId[:7] for res in resultset if res.getId.startswith(resultFilter)] logging.debug("searchtext: found %d texts"%len(results)) return results @@ -991,7 +1011,7 @@ class CDLIRoot(Folder): if line.lstrip().startswith('#lem:'): continue # ignore p-num line - if line.startswith('&P'): + if line.startswith('&P') or line.startswith('&S'): continue # ignore version lines if line.startswith('#version'): @@ -1082,9 +1102,6 @@ class CDLIRoot(Folder): lv.author=user lv.versionComment="XXXXXXX" - - - gen def forceunlock(self,REQUEST=None,user=None,fid=None): "break all locks" if fid is not None: @@ -1227,7 +1244,39 @@ class CDLIRoot(Folder): return generateXMLReturn(stObj.returnValue) + + def uploadATFAsync(self,repeat=None,upload=None,basketId=0,RESPONSE=None): + """upload an atf file / basket file""" + #self._v_uploadATF.returnValue=None + + + idTmp=str(randint(0,1000000000)) + + if upload is None: + return "ERROR NO FILE!" + + tmpFile = File("/tmp/idTmp","w") + # sicher basket in file + for x in upload.read(): + tmpFile.write(x) + + tmpFile.close(); + uploadATFAsync(baskerId,idTmp,basketId,self.REQUEST['AUTHENTICATED_USER'],idTmp,serverport=self.REQUEST['SERVER_PORT']) + return idTMP + + def viewTicketAsync(self,ticketNr=1): + """viewticket""" + + tmp = pickle.load(file("/tmp/"+str(ticketNr)+".result")) + + pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheckAsync.zpt')).__of__(self) + + return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'], + basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId']) + + + def uploadATF(self,repeat=None,upload=None,basketId=0,RESPONSE=None): """upload an atf file / basket file""" #self._v_uploadATF.returnValue=None @@ -1299,6 +1348,7 @@ class CDLIRoot(Folder): return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'], basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId']) + def redoUpload(self,threadName): """redo the upload""" tmp=self.cdli_main.tmpStore2[threadName] @@ -1308,9 +1358,6 @@ class CDLIRoot(Folder): def uploadATFfinally(self,procedure='',comment="",basketname='',unlock=None,repeat=None,RESPONSE=None): """nowupload the files""" - - - threadName=repeat if not threadName or threadName=="": thread=uploadATFfinallyThread() @@ -1373,12 +1420,8 @@ class CDLIRoot(Folder): logging.error("uploadATFfinally - cannot update Object %s Error: %s %s"%(ob[1],sys.exc_info()[0],sys.exc_info()[1])) for x in stObj.returnValue['newPs']: - ob=self.CDLICatalog.search({'title':x}) - try: - self.cdliRoot.updateOrAddToFileBTree(ob[0].getObject()) - except: - - logging.error("uploadATFfinally - cannot update Object %s Error: %s %s"%(x,sys.exc_info()[0],sys.exc_info()[1])) + obj=self.getFileObject(x) #updates the object in the cache + logging.debug("Got:"+repr(obj)) if RESPONSE is not None: RESPONSE.redirect(self.absolute_url()) @@ -1398,7 +1441,11 @@ class CDLIRoot(Folder): obj=self.ZopeFind(root,obj_ids=[folder]) logging.debug("importFiles: folder=%s f2=%s obj=%s"%(folder,f2,obj)) if ext: - ext.result="

adding: %s

"%f+ext.result + + if type(ext.result) is types.FileType: + ext.result.write("

adding: %s

\n"%f) + else: + ext.result="

adding: %s

"%f+ext.result if not obj: # if not create it