Annotation of lise/lise.py, revision 1.1.1.1
1.1 wischi 1: import xml.parsers.expat
2: import re
3: import OFS.Image
4: from types import *
5: from OFS.Image import Image
6: from OFS.Folder import Folder
7: from OFS.SimpleItem import SimpleItem
8: from OFS.Image import manage_addFile
9: from OFS.Image import File
10: from OFS.DTMLDocument import addDTMLDocument
11: from Globals import DTMLFile, MessageDialog, package_home
12: from Products.ExternalMethod.ExternalMethod import manage_addExternalMethod
13: from Products.PageTemplates.ZopePageTemplate import manage_addPageTemplate
14: import os
15: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
16: from Products.PageTemplates.PageTemplate import PageTemplate
17: import string
18: import urllib
19: import xml.dom.minidom
20:
21:
22: ### G L O B A L S ###
23: allobjects = []
24: pagearray = []
25: objindex = 0
26: currobj = 0
27: currchapter = 0
28: tagstr = ""
29: counter = 1 # counts pages/pictures
30: counterid = 1 # counts IDs
31:
32: onpage = 0
33: onpagename = 0
34: onpageindex = 0
35: onpageurl = 0
36: onchapter = 0
37: onchaptername = 0
38: onstart = 0
39: onstartname = 0
40: onstartindex = 0
41: onstarturl = 0
42: onend = 0
43: onendname = 0
44: onendindex = 0
45: onendurl = 0
46: gbaseurl = ''
47: gpictpath = ''
48: errstr = ''
49:
50:
51:
52: class lise(Folder):
53: "The class lise"
54:
55: meta_type = 'lise'
56:
57: manage_options=(
58: {'label': 'Contents', 'action': 'manage_main'},
59: {'label': 'Welcome', 'action': 'index_html'},
60: {'label': 'Schedule', 'action': 'schedule_html'},
61: {'label': 'Edit Schedule', 'action': 'manage_editScheduleForm'},
62: )
63:
64: def __init__(self, id, title):
65: "Initialize a new instance of lise class"
66: self.id = id
67: self.title = title
68: self.freetext = 'hoho'
69: self.schedule = 'schedule'
70: self.xml = 'xml'
71: self.xmlpath = 'xmlpath'
72: self.xmlfilename = 'xmlfilename'
73: self.link = ''
74:
75: def manage_editSchedule(self, title, schedule, RESPONSE=None):
76: "Change attributes of a lise instance"
77: self.title = title
78: self.schedule = schedule
79: if RESPONSE is not None:
80: return MessageDialog(
81: title='Edited',
82: message='<strong>%s</strong> has been edited.'%self.id,
83: action='./schedule_html'
84: )
85:
86: schedule_html = DTMLFile('dtml/schedule_html', globals())
87: manage_editScheduleForm = DTMLFile('dtml/manage_editScheduleForm', globals())
88:
89:
90: # ZOPE interface
91: manage_addliseForm = DTMLFile('dtml/manage_addliseForm', globals())
92:
93:
94: def manage_addlise(self, id, title, pictpath, baseurl, file, RESPONSE = None):
95: "add a lise instance in a folder."
96: global allobjects
97: global objindex
98: global currchapter
99: global currobj
100: global tagstr
101: global onpage
102: global onpagename
103: global onpageindex
104: global onpageurl
105: global onchapter
106: global onchaptername
107: global onstart
108: global onstartname
109: global onstartindex
110: global onstarturl
111: global onend
112: global onendname
113: global onendindex
114: global onendurl
115: global pagearray
116: global errstr
117: global counterid
118: global gbaseurl
119: global gpictpath
120:
121: allobjects = []
122: pagearray = []
123: filenames = []
124: fnames = []
125: id = str(id)
126: title = str(title)
127: if IsStr(gbaseurl):
128: gbaseurl = str(baseurl)
129: gpictpath = str(pictpath)
130: #### lšschen nicht vergessen ###
131: gpictpath = '/barker'
132: thelen = len(gpictpath)
133: if thelen > 0:
134: fnames = os.listdir(gpictpath)
135: thelen = len(filenames)
136: j = 0
137: for i in fnames:
138: if i[0] <> '.':
139: normalize_whitespace(i)
140: filenames.append('file://' + gpictpath + '/' + i)
141:
142: newObj = lise(id, title) # create new lise object
143: self._setObject(id, newObj) # put it into zope
144: if RESPONSE is not None:
145: RESPONSE.redirect('manage_main')
146: link = self.absolute_url() + '/' + id # path of new lise object
147: object = getattr(self, id) # get the new lise object
148: RESPONSE.redirect(link) # set path to new lise object
149:
150: errid = 'standard_error_message'
151: error_html = DTMLFile('dtml/standard_error_message',globals())
152: addDTMLDocument(object, errid, '', error_html)
153: errobj = getattr(object, errid) # reference this object
154:
155: lisescript = manage_addExternalMethod(object, 'liseScript','liseScript','lise.liseScript','liseScript')
156:
157: id2 = id + '.xml'
158: manage_addFile(object, id2, file, title, 'text/xml') # create new xml file
159: object2 = getattr(object, id2) # reference this object
160: link2 = self.absolute_url() + '/' + id + '/' + id + '.xml' # path of xml file
161:
162: objarr = []
163: retval = ''
164: thelen = 42
165: newObj.xmlpath = str(thelen)
166:
167: p = xml.parsers.expat.ParserCreate()
168: taghandler = TagHandler()
169: p.StartElementHandler = taghandler.expat_start_element
170: p.EndElementHandler = taghandler.expat_endElement
171: p.CharacterDataHandler = taghandler.characters
172: p.Parse(str(object2.data))
173:
174: for i in allobjects:
175: if hasattr(i, 'typestr'):
176: if i.typestr == 'chapter':
177: i.parseobjects()
178:
179: counterid = 1
180: for i in allobjects:
181: if hasattr(i, 'typestr'):
182: if i.typestr == 'chapter':
183: i.CreateChapter()
184: if i.typestr == 'page':
185: i.CreatePage()
186:
187: pacount = 0
188: for i in pagearray:
189: if i.type == 'C':
190: i.picturelink = 'this_is the_no_fucking_picture_error'
191: i.pictindex = 0
192: for j in range(pacount, len(pagearray)):
193: if pagearray[j].type == 'P':
194: if len(pagearray[j].picturelink) > 0:
195: i.picturelink = pagearray[j].picturelink
196: break
197: if IsInt(pagearray[j].pictindex):
198: i.pictindex = pagearray[j].pictindex
199: i.picturelink = ''
200: break
201: pacount = pacount + 1
202:
203: mainid = 'main'
204: main_html = PageTemplateFile('dtml/main.zpt',globals()).__of__(self)
205: manage_addPageTemplate(object, mainid, '', main_html)
206: # addDTMLDocument(object, mainid, '', main_html)
207: mainobj = getattr(object, mainid) # reference this object
208: setattr(mainobj, 'pagelist', pagearray)
209: setattr(mainobj, 'filenames', filenames)
210: if IsStr(pagearray[0].picturelink):
211: setattr(mainobj, 'currpict', pagearray[0].picturelink)
212: else:
213: setattr(mainobj, 'currpict', filenames[int(pagearray[0].id)])
214:
215: # sdmid = 'sdm'
216: # sdm = SessionDataManager(sdmid)
217: # object._setObject(sdmid, sdm) # put it into lise folder
218:
219: if(len(errstr) > 0):
220: newObj.freetext = self.absolute_url() + '/' + id + '/standard_error_message'
221: errobj.error_message = errstr + '<br><br><h3>by babes</h3>'
222: RESPONSE.redirect(self.absolute_url() + '/' + id + '/standard_error_message')
223: else:
224: RESPONSE.redirect(self.absolute_url() + '/' + id + '/main?theid=1')
225:
226:
227:
228: ########################################################################################
229: class Error_DTML(SimpleItem):
230: "the ERROR message"
231: meta_type = 'error_message'
232:
233: def __init__(self, id):
234: self.id = id
235:
236:
237: ########################################################################################
238: class TagHandler:
239: def __init__(self):
240: self.tags={}
241: self.name=""
242: self.index = ""
243: self.url = ""
244: self.startname = ""
245: self.startindex = 0
246: self.starturl = ""
247: self.endname = ""
248: self.endindex = 0
249: self.endurl = ""
250: self.chapterindex = 0
251: self.currentchapter = 0
252:
253:
254: def expat_start_element(self, name, attrs):
255: global allobjects
256: global objindex
257: global currchapter
258: global currobj
259: global tagstr
260: global onpage
261: global onpagename
262: global onpageindex
263: global onpageurl
264: global onchapter
265: global onchaptername
266: global onstart
267: global onstartname
268: global onstartindex
269: global onstarturl
270: global onend
271: global onendname
272: global onendindex
273: global onendurl
274: global errstr
275:
276: if name == 'page':
277: onstart = False
278: onpage = True
279: currobj = PageObject()
280: if onchapter:
281: thelen = len(currchapter.chapterobjects)
282: if thelen > 0:
283: currobj.prevref = currchapter.chapterobjects[thelen - 1]
284: currchapter.chapterobjects[thelen - 1].nextref = currobj
285: currchapter.chapterobjects.append(currobj)
286: currobj.chapterindex = currchapter.chapterindex
287: else:
288: thelen = len(allobjects)
289: if thelen > 0:
290: currobj.prevref = allobjects[thelen - 1]
291: allobjects[thelen - 1].nextref = currobj
292: allobjects.append(currobj)
293:
294: if name == 'chapter':
295: onstart = False
296: onend = False
297: currobj = ChapterObject(objindex)
298: objindex += 1
299: if onchapter:
300: currobj.upref = currchapter
301: thelen = len(currchapter.chapterobjects)
302: if thelen > 0:
303: currobj.prevref = currchapter.chapterobjects[thelen - 1]
304: currchapter.chapterobjects[thelen - 1].nextref = currobj
305: currchapter.chapterobjects.append(currobj)
306: else:
307: allobjects.append(currobj)
308: onchapter = True
309: currchapter = currobj
310:
311: if name == 'name':
312: tagstr = ""
313: if onpage:
314: onpagename = True
315: else:
316: if onchapter:
317: if (not onstart) and (not onend):
318: onchaptername = True
319: else:
320: if onstart:
321: onstartname = True
322: if onend:
323: onendname = True
324: else:
325: errstr = errstr + 'ERROR in XML file: \"<name> tag outside <page> or <chapter>\"<br>'
326:
327: if name == 'index':
328: tagstr = ""
329: if onpage:
330: onpageindex = True
331: else:
332: if onchapter:
333: if onstart:
334: if not onend:
335: onstartindex = True
336: else:
337: onendindex = True
338: else:
339: errstr = errstr + 'ERROR in XML file: \"<index> tag inside chapter but outside <start> or <end>\"<br>'
340: else:
341: errstr = errstr + 'ERROR in XML file: \"<index> tag outside <page> or <chapter>\"<br>'
342:
343: if name == 'url':
344: tagstr = ""
345: if onpage:
346: onpageurl = True
347: else:
348: if onchapter:
349: if onstart:
350: if not onend:
351: onstarturl = True
352: else:
353: onendurl = True
354: else:
355: errstr = errstr + 'ERROR in XML file: \"<url> tag inside chapter but outside <start> or <end>\"<br>'
356: else:
357: errstr = errstr + 'ERROR in XML file: \"<url> tag outside <page> or <chapter>\"<br>'
358:
359: if name == 'start':
360: if onchapter:
361: onstart = True
362: currobj = StartEndObject()
363: thelen = len(currchapter.chapterobjects)
364: if thelen > 0:
365: currobj.prevref = currchapter.chapterobjects[thelen - 1]
366: currchapter.chapterobjects[thelen - 1].nextref = currobj
367: currchapter.chapterobjects.append(currobj)
368: else:
369: errstr = errstr + 'ERROR in XML file: \"<start> tag outside <chapter>\"<br>'
370:
371: if name == 'end':
372: if onchapter:
373: if onstart:
374: onend = True
375: else:
376: errstr = errstr + 'ERROR in XML file: \"<end> tag without <start>\"<br>'
377: else:
378: errstr = errstr + 'ERROR in XML file: \"<end> tag outside <chapter>\"<br>'
379:
380:
381: def characters(self, ch):
382: global tagstr
383: tagstr += ch
384:
385:
386: def expat_endElement(self, name):
387: global allobjects
388: global objindex
389: global currchapter
390: global currobj
391: global tagstr
392: global onpage
393: global onpagename
394: global onpageindex
395: global onpageurl
396: global onchapter
397: global onchaptername
398: global onstart
399: global onstartname
400: global onstartindex
401: global onstarturl
402: global onend
403: global onendname
404: global onendindex
405: global onendurl
406: global errstr
407:
408: # errstr = errstr + 'end: ' + name + ':<br>'
409: if name == 'page':
410: onpage = False
411:
412: if name == 'chapter':
413: objindex -= 1
414: if objindex <= 0:
415: onchapter = False
416: currchapter = 0
417: else:
418: currchapter = currchapter.upref
419:
420: if name == 'name':
421: tagstr = normalize_whitespace(tagstr)
422: if onpagename:
423: onpagename = False
424: currobj.name = tagstr
425: if onchaptername:
426: if currobj.typestr == 'chapter':
427: currobj.name = tagstr
428: onchaptername = False
429: if onstartname:
430: if currobj.typestr == 'start':
431: currobj.startname = tagstr
432: onstartname = False
433: if onendname:
434: if currobj.typestr == 'start':
435: currobj.endname = tagstr
436: onendname = False
437:
438: if name == 'index':
439: tagstr = normalize_whitespace(tagstr)
440: if onpageindex:
441: if IsInt(tagstr):
442: currobj.index = int(tagstr) - 1
443: else:
444: errstr = errstr + 'ERRRRROR: cannot extract number from page index.<br>'
445: onpageindex = False
446:
447: if onstartindex:
448: if IsInt(tagstr):
449: currobj.startindex = int(tagstr) - 1
450: else:
451: errstr = errstr + 'ERRRRROR: cannot extract startnumber from startindex.<br>'
452: onstartindex = False
453:
454: if onendindex:
455: if IsInt(tagstr):
456: currobj.endindex = int(tagstr) - 1
457: else:
458: errstr = errstr + 'ERRRRROR: cannot extract endnumber from endindex.<br>'
459: onendindex = False
460:
461: if name == 'url':
462: tagstr = normalize_whitespace(tagstr)
463: if onpageurl:
464: currobj.url = tagstr
465: onpageurl = False
466: if onstarturl:
467: currobj.starturl = tagstr
468: onstarturl = False
469: if onendurl:
470: currobj.endurl = tagstr
471: onendurl = False
472:
473: if name == 'end':
474: onstart = False
475: onend = False
476:
477:
478: ########################################################################################
479: def normalize_whitespace(text):
480: "Remove redundant whitespace from a string"
481: return ' '.join(text.split())
482:
483:
484: ########################################################################################
485: def IsInt(str):
486: """ Is the given string an Integer? """
487: try: int(str)
488: except ValueError:
489: return 0
490: else:
491: return 1
492:
493: ########################################################################################
494: def IsStr(str):
495: """ Is the given string really a string? """
496: try: str + ''
497: except: return 0
498: else: return 1
499:
500: ########################################################################################
501: class PageObject:
502: def __init__(self):
503: self.name = ""
504: self.index = ""
505: self.pictindex = 0
506: self.url = ""
507: self.isselected = False
508: self.typestr = "page"
509: self.chapterindex = 0
510: self.prevref = 0
511: self.nextref = 0
512: self.chapref = 0
513:
514: def CreatePage(self):
515: global counter
516: global counterid
517: global gbaseurl
518: global gpicpath
519: global pagearray
520: global errstr
521:
522: pageelement = PageElement()
523: pageelement.id = str(counterid)
524: pageelement.type = "P"
525: pageelement.name = self.name
526: if IsStr(self.url):
527: if len(self.url) > 0:
528: pageelement.picturelink = gbaseurl + '/' + self.url
529:
530: pageelement.level = self.chapterindex
531: if IsInt(self.index):
532: if int(self.index) > 0:
533: pageelement.pictindex = int(self.index) - 1
534: else:
535: pageelement.pictindex = '9999'
536: else:
537: pageelement.pictindex = '9999'
538:
539: if pageelement.level == 0:
540: pageelement.open = 1
541: else:
542: pageelement.open = 0
543:
544: pagearray.append(pageelement)
545: counter = counter + 1
546: counterid = counterid + 1
547:
548:
549: ########################################################################################
550: class ChapterObject:
551: def __init__(self, chapterindex):
552: self.name = ""
553: self.typestr = "chapter"
554: self.chapterindex = chapterindex
555: self.isselected = False
556: self.isopen = False
557: self.prevref = 0
558: self.nextref = 0
559: self.upref = 0
560: self.chapterobjects = []
561: self.pages_subchapters = []
562:
563: def parseobjects(self):
564: thelen = len(self.chapterobjects)
565: if hasattr(self, 'chapterobjects'):
566: count = 0
567: for i in self.chapterobjects:
568: count += 1
569: if hasattr(self, 'typestr'):
570: if i.typestr == 'page':
571: self.pages_subchapters.append(i)
572: if i.typestr == 'chapter':
573: i.parseobjects()
574: if i.typestr == 'start':
575: i.startindex = int(i.startindex)
576: if i.startindex > 0:
577: i.endindex = int(i.endindex)
578: if i.endindex > 0:
579: if i.endindex < i.startindex:
580: errstr = errstr + 'ERROR in XML file: \"<start> index bigger then <end> index.\"<br>'
581: else:
582: startfound = False
583: for j in range(count, len(self.chapterobjects)):
584: if self.chapterobjects[j].typestr == 'start':
585: startfound = True
586: i.endindex = self.chapterobjects[j].startindex - 1
587: break
588: if self.chapterobjects[j].typestr == 'page':
589: i.endindex = self.chapterobjects[j].index - 1
590: startfound = True
591: break
592:
593: if not startfound:
594: errstr = errstr + 'ERROR in XML file: \"<start> tag in chapter has no end-tag.\"<br>'
595: # print ' startname: ', i.startname
596: # print 'startindex: ', i.startindex
597: # print ' endurl: ', i.starturl
598: # print ' endname: ', i.endname
599: # print ' endindex: ', i.endindex
600: # print ' endurl: ', i.endurl, '\n'
601:
602: def CreateChapter(self):
603: global counter
604: global counterid
605: global baseurl
606: global pagearray
607: global errstr
608:
609: thelen = len(self.chapterobjects)
610: chapelement = PageElement()
611: chapelement.name = self.name
612: chapelement.id = counterid
613: chapelement.type = "C"
614: chapelement.level = self.chapterindex
615: chapelement.open = 0
616: chapelement.picturelink = ''
617: chapelement.pictindex = 9999;
618: pagearray.append(chapelement)
619: counterid = counterid + 1
620: if hasattr(self, 'chapterobjects'):
621: count = 0
622: for i in self.chapterobjects:
623: count += 1
624: if hasattr(self, 'typestr'):
625: if i.typestr == 'page':
626: i.CreatePage()
627: if i.typestr == 'chapter':
628: i.CreateChapter()
629: if i.typestr == 'start':
630: namenumber = counter
631: nameprev = ""
632: urlstart = 0
633: urlend = 0
634: if i.startname != '':
635: t = re.search(r'\d*$', i.startname)
636: if not IsInt(t.group(0)):
637: t = re.sub('(\..*$)', '', s)
638: t = re.search(r'\d*$', t)
639: if IsInt(t.group(0)):
640: namenumber = int(t.group(0))
641: nameprev = re.sub(r'\d*$', '', i.startname)
642: else:
643: errstr = errstr + 'ERRRRROR: cannot extract startnumber.<br>'
644: else:
645: namenumber = int(t.group(0))
646: nameprev = re.sub(r'\d*$', '', i.startname)
647:
648: if ( (i.startindex > 0) and (i.endindex > 0) and (i.endindex > i.startindex) ):
649: currcount = 0
650: for j in range(i.startindex, i.endindex + 1):
651: currnumber = namenumber + currcount
652: pageelement = PageElement()
653: pageelement.name = nameprev + str(currnumber)
654: pageelement.level = self.chapterindex + 1
655: pageelement.id = counterid
656: pageelement.type = "P"
657: pageelement.open = 0
658: pageelement.pictindex = j
659: pagearray.append(pageelement)
660: currlen = len(pagearray)
661: if pagearray[currlen - 2].type == 'C':
662: pagearray[currlen - 2].pictindex = j
663: counter = counter + 1
664: counterid = counterid + 1
665: currcount = currcount + 1
666: else:
667: t = re.search(r'\d*$', i.starturl)
668: if not IsInt(t.group(0)):
669: t = re.sub('(\..*$)', '', s)
670: t = re.search(r'\d*$', t)
671: if IsInt(t.group(0)):
672: urlstart = int(t.group(0))
673: else:
674: errstr = errstr + 'ERRRRROR: cannot extract number from starturl.<br>'
675: else:
676: urlstart = int(t.group(0))
677: t = re.search(r'\d*$', i.endurl)
678: if not IsInt(t.group(0)):
679: t = re.sub('(\..*$)', '', s)
680: t = re.search(r'\d*$', t)
681: if IsInt(t.group(0)):
682: urlend = int(t.group(0))
683: else:
684: errstr = errstr + 'ERRRRROR: cannot extract from endnumber.<br>'
685: else:
686: urlend = int(t.group(0))
687: if ( (urlstart > 0) and (urlend > 0) and (urlend > urlstart) ):
688: for j in range(urlstart, urlend):
689: pageelement = PageElement()
690: currnumber = namenumber + j
691: pageelement.name = nameprev + str(currnumber)
692: pageelement.level = self.chapterindex + 1
693: pageelement.id = counterid
694: pageelement.type = "P"
695: pageelement.open = 0
696: pageelement.pictindex = j
697: pagearray.append(pageelement)
698:
699: counter = counter + 1
700: counterid = counterid + 1
701:
702:
703: ########################################################################################
704: class StartEndObject:
705: def __init__(self):
706: self.startname = ""
707: self.startindex = 0
708: self.starturl = ""
709: self.endname = ""
710: self.endindex = 0
711: self.endurl = ""
712: self.typestr = "start"
713: self.prevref = 0
714: self.nextref = 0
715: self.chapref = 0
716:
717:
718: ########################################################################################
719: class PageElement:
720: def __init__(self):
721: self.id = ""
722: self.type = ""
723: self.name = ""
724: self.picturelink = ""
725: self.pictindex = 0
726: self.level = 0
727: self.open = 0
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>