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