Annotation of OSA_system2/OSAS_browser.py, revision 1.2

1.1       dwinter     1: """ Classes for displaying, browsing and organizing the archive
                      2: """
                      3: 
                      4: 
                      5: import OSAS_helpers
                      6: from AccessControl import ClassSecurityInfo
                      7: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
                      8: from OFS.Folder import Folder
                      9: from OFS.SimpleItem import SimpleItem
                     10: from Globals import InitializeClass,package_home
                     11: import zLOG
                     12: import os
                     13: import os.path
                     14: import stat
1.2     ! dwinter    15: import xml.dom.minidom
1.1       dwinter    16: 
                     17: class OSAS_storeOnline(SimpleItem):
                     18:     """Webfrontend für das Storagesystem
                     19:     liefert Browserumgebung 
                     20:     """
                     21:     meta_type="OSAS_StoreOnline__neu"
                     22:     
                     23:     security=ClassSecurityInfo()
                     24: 
                     25:     _v_fileSystem={} #chache fuer filesystem
                     26:     
                     27:     def __init__(self,id):
                     28:         """initialize a new instance"""
                     29:         self.id = id
                     30: 
                     31:     
                     32: 
                     33:     security.declareProtected('View','index_html')
                     34:     def index_html(self):
1.2     ! dwinter    35:         """main view either standard template zpt/storeOnline_index_html.zpt or storeOnline_index.html in tree"""
1.1       dwinter    36:         if hasattr(self,'storeOnline_index.html'):
                     37:             return getattr(self,'storeOnline_index.html')()
                     38:         else:
                     39:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','storeOnline_index_html.zpt')).__of__(self)
                     40:             return pt()
                     41: 
                     42: 
1.2     ! dwinter    43:     def findIndexMeta(self,path=""):
        !            44:         """finde index_meta fuer diesen eventuell virtuellen Pfad
        !            45:         @param path: optional, default ist "", Pfad auf das Object relativ zum rootFolderName
        !            46:         @return: None falls kein index.meta existiert sonst Pfad auf das index.meta
        !            47:         """
        !            48:         realPath=os.path.normpath(os.path.join(self.rootFolderName,path))
        !            49:         #suche index.meta
        !            50:         while (not os.path.exists(os.path.join(realPath,'index.meta'))) and (not ((realPath=="") or (realPath=="/"))):
        !            51:             realPath=os.path.split(realPath)[0]
        !            52:         if realPath=='' or realPath=='/':
        !            53:             return None
        !            54:         else:
        !            55:             return os.path.join(realPath,'index.meta')
        !            56: 
        !            57:     def findEntryInIndexMeta(self,path):
        !            58:         """fragm xml zum path
        !            59:         @param path: Pfad auf das Object relativ zum rootFolderName
        !            60:         @return: den Teil von Index.meta der Informationen zu path enthaelt.
        !            61:         """
        !            62: 
        !            63:         indexMeta=self.findIndexMeta(path)
        !            64:         
        !            65:         realPath=os.path.split(indexMeta)[0]
        !            66:         path=os.path.normpath(os.path.join(self.rootFolderName,path))
        !            67:         
        !            68:         dom=xml.dom.minidom.parse(indexMeta)
        !            69: 
        !            70:         #ist path ein directory?
        !            71:         dirs=dom.getElementsByTagName('dir')
        !            72:         for dir in dirs:
        !            73:             pathes=dir.getElementsByTagName('path')
        !            74:             if pathes:
        !            75:                 pathX=OSAS_helpers.getText(pathes[0].childNodes)
        !            76:             else:
        !            77:                 pathX=""
        !            78:             names=dir.getElementsByTagName('name')
        !            79:             if names:
        !            80:                 name=OSAS_helpers.getText(names[0].childNodes)
        !            81:             else:
        !            82:                 name=""
        !            83: 
        !            84:             checkpath=os.path.normpath(os.path.join(realPath,pathX,name))
        !            85:             if checkpath==path:
        !            86:                 
        !            87:                 return dir.toxml()
        !            88: 
        !            89:         #ist path ein file?
        !            90:         files=dom.getElementsByTagName('file')
        !            91:         for dir in dirs:
        !            92:             pathes=dir.getElementsByTagName('path')
        !            93:             if pathes:
        !            94:                 pathX=OSAS_helpers.getText(pathes[0].childNodes)
        !            95:             else:
        !            96:                 pathX=""
        !            97:             names=dir.getElementsByTagName('name')
        !            98:             if names:
        !            99:                 name=OSAS_helpers.getText(names[0].childNodes)
        !           100:             else:
        !           101:                 name=""
        !           102: 
        !           103:             checkpath=os.path.normpath(os.path.join(realPath,pathX,name))
        !           104:             if checkpath==path:
        !           105:                 
        !           106:                 return dir.toxml()
        !           107: 
        !           108:         
        !           109:         return None
        !           110: 
        !           111:     def getSubDirsFromIndexMeta(self,path):
        !           112:         
        !           113:         """Gebe alle path untergeordenten Objekte aus
        !           114:         @param path: optional, default ist "", Pfad auf das Object relativ zum rootFolderName
        !           115:         @return: Directory [pfad auf das Objekt]->(fileType,''), fileType ist hierbei OSAS_dir_archive falls Object ein directory und OSAS_file_archive falls das Object ein File ist,der zweite Eintrag des Tupels ist zur Zeit immer '', spaeter wird hier die Beschreibung gemaess Metadaten stehen, wie bei readObjectsFromPath.
        !           116:         """
        !           117:         ret={}
        !           118:         indexMeta=self.findIndexMeta(path)
        !           119:         if not indexMeta:
        !           120:             return ret
        !           121:         realPath=os.path.split(indexMeta)[0]
        !           122:         path=path.replace(realPath,"")
        !           123:         if path and (path[0]==os.sep):
        !           124:             path=path[1:]
        !           125: 
        !           126:             
        !           127:         dom=xml.dom.minidom.parse(indexMeta)
        !           128: 
        !           129:         dirs=[]
        !           130:         dirs=dom.getElementsByTagName('dir')+dom.getElementsByTagName('file')
        !           131:     
        !           132:         for dir in dirs:
        !           133:             pathes=dir.getElementsByTagName('path')
        !           134:             if pathes:
        !           135:                 pathX=OSAS_helpers.getText(pathes[0].childNodes)
        !           136:             else:
        !           137:                 pathX=""
        !           138:             names=dir.getElementsByTagName('name')
        !           139:             if names:
        !           140:                 name=OSAS_helpers.getText(names[0].childNodes)
        !           141:             else:
        !           142:                 name=""
        !           143: 
        !           144:         
        !           145:             if pathX==path:
        !           146:                 if dir.tagName=="dir":
        !           147:                     fileType="OSAS_dir_archive"
        !           148:                 else:
        !           149:                     fileType="OSAS_file_archive"
        !           150: 
        !           151:                 object=os.path.join(realPath,pathX,name)
        !           152:                 ret[object]=(fileType,'')
        !           153: 
        !           154: 
        !           155:         return ret
        !           156: 
        !           157:         
        !           158:     def getMetaInfoFromIndexMeta(self,path):
        !           159:         """metadaten zu path als html aus dem index.meta file zu path
        !           160:         @param path: optional, default ist "", Pfad auf das Object relativ zum rootFolderName
        !           161:         @return: metadata als html
        !           162:         """
        !           163:         xmlInfos=self.findEntryInIndexMeta(path)
        !           164:         return OSAS_helpers.getMetaInfoFromXML(path,xmlInfos)
        !           165:                 
1.1       dwinter   166:    
                    167: 
                    168:     def readObjectsFromPath(self,path=""):
                    169:         """Liest files aus dem path und speichert im cache _v_filesystem.
                    170:          - to do: files aus metadaten
                    171: 
                    172:         @param path : path relativ zum root folder des Storagesystems
1.2     ! dwinter   173:         @return: directory der Form [pfad zum Objekt] -> (fileType,metadatum als String)
1.1       dwinter   174:         """
                    175:                        
                    176:         realPath=os.path.normpath(os.path.join(self.rootFolderName,path))
                    177:         metaData=self.testmd #test
                    178:         
                    179:         if realPath.find(self.rootFolderName) <0: #versuch auf Pfad unterhalb des Rootfolder zuzugreifen
                    180:             return {}
                    181:             
                    182:         
                    183:         if not os.path.exists(realPath):
1.2     ! dwinter   184:             #return None,"(ERROR) path %s does not exist."%path
        !           185:             return None
        !           186:         
1.1       dwinter   187:         stats=os.stat(realPath)
                    188: 
                    189:         # teste ob schon im cache
                    190:         #if self._v_fileSystem.has_key(realPath) and (self._v_fileSystem[realPath][0]==stats[stat.ST_MTIME]):
                    191:             
                    192:         #    return self._v_fileSystem[realPath][1]
                    193: 
                    194:         dir=os.listdir(realPath)
                    195:         ret={}
                    196:         for filename in dir:
                    197:             object=os.path.join(realPath,filename)
                    198:             fileType=OSAS_helpers.checkOSASFileType(object)
                    199:             
                    200:             if fileType:
1.2     ! dwinter   201:                 
1.1       dwinter   202:                 ret[object]=(fileType,metaData.getDisplayFieldsAsStr(object))
                    203:             
                    204:         self._v_fileSystem[realPath]=(stats[stat.ST_MTIME],ret) # speicher im chache
                    205:         
                    206:         return ret
                    207: 
                    208:     def giveHandlers(self,path,type):
1.2     ! dwinter   209:         """teste ob fuer diesen Typ, viewer definiert sind und gibt einen entsprechenden Link zurueck, der das Object mit diesem Handler ausfuehrt.
        !           210:         @param path: Pfad auf das Objekt
        !           211:         @param type: Typ des Objektes
        !           212:         @return: (string) html-Fragment, link der das Objekt mit diesem Handler anzeigt.
        !           213:         """
1.1       dwinter   214:         ret=[]
                    215:         
                    216:         for viewer in self.ZopeFind(self.aq_parent,obj_metatypes=['OSAS_ViewerObject__neu'],search_sub=1):
                    217:             if type in viewer[1].objectTypes:
                    218:                 path=path.replace(getattr(viewer[1],'ignorePath',''),'')
                    219:                 url=viewer[1].prefix%path
                    220:                 text=viewer[1].title
                    221:                 string="""<a target="_blank" href="%s">%s</a>"""%(url,text)
                    222:                 ret.append(string)
                    223:         return ret
                    224:                       
                    225:         
                    226:     def generateTree(self,path=""):
1.2     ! dwinter   227:         """erzeuge liest die Objekte aus die im Pfad gespeichert sind
        !           228:         @param path: optional mit default='', Pfad relativ zu rootFolderName
        !           229:         @return: List von Tripeln, (link_html,array of handlers,metainformationen) hierbei ist
        !           230:         - (string) link_html ein html-Fragement, falls das Objekt vom Typ OSAS_dir ist, ist dies ein Link auf dieses Verzeichnis, sonst der Dateiname
        !           231:         - (string) handler sind die Ergebnisse von giveHandlers fuer dieses Objekt
        !           232:         - (string) metainformationen die Metainformationen zum Objekt als Ergebnis von readObjectsFromPath
        !           233:         """
1.1       dwinter   234:         objects=self.readObjectsFromPath(path)
1.2     ! dwinter   235:         if not objects:
        !           236:             objects={}
        !           237:         im=self.getSubDirsFromIndexMeta(path)
        !           238:         for key in im.keys():
        !           239:             #virtuelle pfade hinzufuegen
        !           240:             
        !           241:             if not objects.has_key(key):
        !           242:                 objects[key]=im[key]
        !           243:                 
        !           244:         
1.1       dwinter   245:         def sortLow(x,y):
                    246:             return cmp(x.lower(),y.lower())
                    247:         
                    248:         ret=[]
                    249:         
                    250:         objectSorted=objects.keys()
                    251:         objectSorted.sort(sortLow)
                    252:         for object in objectSorted:
                    253:             handler=self.giveHandlers(object,objects[object][0])
                    254:             if objects[object][0]=="OSAS_dir":
                    255:                 
                    256:                 string="""<a href="?path=%s">%s</a>"""%(object,os.path.split(object)[1])
                    257:                 
                    258:                 ret.append((string,handler,objects[object][1]))
1.2     ! dwinter   259:             elif objects[object][0]=="OSAS_dir_archive":
        !           260:                 string="""<a href="?path=%s">%s (A)</a>"""%(object,os.path.split(object)[1])
        !           261:                 
        !           262:                 ret.append((string,handler,objects[object][1]))
1.1       dwinter   263:             else:
                    264:                 ret.append((os.path.split(object)[1],handler,objects[object][1]))
                    265: 
                    266:                      
                    267:         return ret
                    268: 
                    269: 
                    270:     def path_to_link(self,pathTmp=""):
                    271:         """generates navigation bar for viewfiles"""
                    272: 
                    273:         path=os.path.normpath(os.path.join(self.rootFolderName,pathTmp))
                    274:         
                    275:         URL=self.absolute_url()
                    276:         string=""
                    277:         
                    278:         tmppath=os.path.dirname(path)
                    279:         i=0
                    280:         pathes=[[path, os.path.basename(path)]]
                    281: 
                    282:         while not (len(tmppath)==1):
                    283: 
                    284:               i=i+1
                    285:               if i>20: break
                    286: 
                    287:               pathes.append([tmppath, os.path.basename(tmppath)])
                    288:               tmppath=os.path.dirname(tmppath)
                    289: 
                    290:         while i>=0:
                    291:             if pathes[i][0].find(self.rootFolderName) <0: #versuch auf Pfad unterhalb des Rootfolder zuzugreifen
                    292:                 string=string+"<a>"+pathes[i][1]+"</a>/"
                    293:             else:
                    294:                 string=string+"<a href="+URL+"?path="+pathes[i][0]+">"+pathes[i][1]+"</a>/"
                    295:             
                    296:             i=i-1
                    297:         return string
                    298: 
                    299:     def getMetaFile(self,path):
                    300:         """Lese Metafile ein"""
1.2     ! dwinter   301:         return OSAS_helpers.getMetaFile(self,path)
1.1       dwinter   302: 
                    303: InitializeClass(OSAS_storeOnline)
                    304:    
                    305: def manage_addOSAS_storeOnlineForm(self):
                    306:     """interface for adding the OSAS_root"""
                    307:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addStoreOnline.zpt')).__of__(self)
                    308:     return pt()
                    309: 
                    310: def manage_addOSAS_storeOnline(self,id,RESPONSE=None):
                    311:     """add the OSAS_root"""
                    312:     newObj=OSAS_storeOnline(id)
                    313:     self._setObject(id,newObj)
                    314:     if RESPONSE is not None:
                    315:         RESPONSE.redirect('manage_main')
                    316: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>