Initial Upload
[planet-ucc.git] / XMLWriter.py
1 #
2 # XMLWriter.py
3 #
4 # Generate arbitrary XML files
5 #
6 # (c) 2004, Davyd Madeley <[email protected]>
7 #
8
9 import time
10
11 class PlanetItem:
12         def __init__(self, blog, item):
13                 self.itemTitle  = item.itemTitle
14                 self.itemURL    = item.itemURL
15                 self.itemDate   = item.itemDate
16                 self.blogTitle  = blog.blogTitle
17                 self.blogURL    = blog.blogURL
18                 self.imageURL   = blog.imageURL
19                 self.imageLink  = blog.imageLink
20                 self.contents   = item.contents
21
22 class PlanetDate:
23         def __init__(self, date):
24                 self.planetDate = date
25                 self.items      = []
26
27 class Planet:
28         def __init__(self, bloglist):
29                 self.__bloglist__       = bloglist
30                 self.__tainted__        = True
31                 self.dates              = []
32                 
33         def append(self, blog):
34                 self.__bloglist__.append(blog)
35                 self.__tainted__        = True
36         
37         def __getNext__(self, bloglist):
38                 "Returns a PlanetItem reaped from a bloglist"
39                 latestTime              = 0
40                 holdingBlog             = None
41                 for blog in bloglist:
42                         if len(blog.items) > 0 and blog.items[0].itemDate > latestTime:
43                                 latestTime      = blog.items[0].itemDate
44                                 holdingBlog     = blog
45                 if holdingBlog == None:
46                         return None
47                 item    = holdingBlog.items.pop(0)
48                 return PlanetItem(holdingBlog, item)
49         
50         def sort(self):
51                 if self.__tainted__:
52                         # we need to sort the blogs into a single news feed
53                         # copy the bloglist to a working symbol
54                         bloglist        = self.__bloglist__ + []
55                         lastDate        = -1
56                         workingDate     = None
57                         self.dates      = []
58                         while True:
59                                 lastItem        = self.__getNext__(bloglist)
60                                 if lastItem == None:
61                                         break
62                                 # this checks to see if it's a new day
63                                 if time.localtime(lastItem.itemDate) != lastDate:
64                                         workingDate     = PlanetDate(lastItem.itemDate)
65                                         self.dates.append(workingDate)
66                                 # append the item to the current date
67                                 workingDate.items.append(lastItem)      
68                         self.__tainted__        = False
69                 return self.dates
70                         
71
72 class XMLWriter:
73         def __init__(self, doctype, bloglist):
74                 self.planet     = Planet(bloglist)
75                 self.items      = self.planet.sort()
76                 # doctype should be something like XMLWriter.XHTMLWriter
77                 self.writer     = doctype(self.items)
78         
79         def write(self):
80                 output          = self.writer.write()
81                 return output
82
83 class XHTMLWriter:
84         def __init__(self, planet):
85                 self.planet     = planet
86                 self.maxitems   = 100
87         
88         def __write_item__(self, item):
89                 output  =       ''
90                 output  +=      '<div id="item">\n'
91                 output  +=      '<h2>%s: %s</h2>\n' % (item.blogTitle, item.itemTitle)
92                 output  +=      '<p class="time">\n'
93                 output  +=      '(%s)\n' % time.strftime('%A %B %d, %Y %H:%M %Z', time.localtime(item.itemDate))
94                 output  +=      '</p>\n'
95                 output  +=      '<p class="body">\n'
96                 output  +=      item.contents
97                 output  +=      '\n</p>\n'
98                 return output
99         
100         def write(self):
101                 itemcount       = 0
102                 output  =       ''
103                 output  +=      '<?xml version="1.0" encoding="UTF-8"?>\n'
104                 output  +=      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n'
105                 output  +=      '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >\n'
106                 output  +=      '<head>\n'
107                 output  +=      '<title>Planet UCC</title>\n'
108                 # XXX: we'll want a style sheet in here
109                 output  +=      '</head>\n'
110                 output  +=      '<body>\n'
111                 # XXX: we want some stuff in here, I'm sure
112                 for date in self.planet:
113                         output  += '<h1>%s</h1>\n' % time.strftime('%A %B %d, %Y', time.localtime(date.planetDate))
114                         for item in date.items:
115                                 output  += self.__write_item__(item)
116                                 # see how many items we've written
117                                 itemcount += 1
118                                 if itemcount >= self.maxitems:
119                                         break
120                         # again, check to see if we've written the maximum number of items
121                         if itemcount >= self.maxitems:
122                                 break
123                 # XXX: we want further stuff here
124                 output  +=      '</body>'
125                 output  +=      '</html>'
126                 return output

UCC git Repository :: git.ucc.asn.au