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