bug fixes
[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.blogName   = blog.blogName
18                 self.blogURL    = blog.blogURL
19                 self.imageURL   = blog.imageURL
20                 self.imageLink  = blog.imageLink
21                 self.contents   = item.contents
22
23 class PlanetDate:
24         def __init__(self, date):
25                 self.planetDate = date
26                 self.items      = []
27
28 class Planet:
29         def __init__(self, bloglist):
30                 self.__bloglist__       = bloglist
31                 self.__tainted__        = True
32                 self.dates              = []
33                 
34         def append(self, blog):
35                 self.__bloglist__.append(blog)
36                 self.__tainted__        = True
37         
38         def __getNext__(self, bloglist):
39                 "Returns a PlanetItem reaped from a bloglist"
40                 latestTime              = 0
41                 holdingBlog             = None
42                 for blog in bloglist:
43                         if len(blog.items) > 0 and blog.items[0].itemDate > latestTime:
44                                 latestTime      = blog.items[0].itemDate
45                                 holdingBlog     = blog
46                 if holdingBlog == None:
47                         return None
48                 item    = holdingBlog.items.pop(0)
49                 return PlanetItem(holdingBlog, item)
50         
51         def sort(self):
52                 if self.__tainted__:
53                         # we need to sort the blogs into a single news feed
54                         # copy the bloglist to a working symbol
55                         bloglist        = self.__bloglist__ + []
56                         lastDate        = -1
57                         workingDate     = None
58                         self.dates      = []
59                         while True:
60                                 lastItem        = self.__getNext__(bloglist)
61                                 if lastItem == None:
62                                         break
63                                 # this checks to see if it's a new day
64                                 if time.localtime(lastItem.itemDate)[6] != lastDate:
65                                         lastDate        = time.localtime(lastItem.itemDate)[6]
66                                         workingDate     = PlanetDate(lastItem.itemDate)
67                                         self.dates.append(workingDate)
68                                 # append the item to the current date
69                                 workingDate.items.append(lastItem)      
70                         self.__tainted__        = False
71                 return self.dates
72                         
73
74 class XMLWriter:
75         def __init__(self, bloglist):
76                 self.blogs      = bloglist
77                 self.planet     = Planet(bloglist)
78                 self.items      = self.planet.sort()
79         
80         def write(self, doctype):
81                 # doctype should be something like XHTMLWriter.XHTMLWriter
82                 writer          = doctype(self.items)
83                 writer.parent   = self
84                 output          = writer.write()
85                 return output
86

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