Updated list
[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)[6] != lastDate:
64                                         lastDate        = time.localtime(lastItem.itemDate)[6]
65                                         workingDate     = PlanetDate(lastItem.itemDate)
66                                         self.dates.append(workingDate)
67                                 # append the item to the current date
68                                 workingDate.items.append(lastItem)      
69                         self.__tainted__        = False
70                 return self.dates
71                         
72
73 class XMLWriter:
74         def __init__(self, bloglist):
75                 self.blogs      = bloglist
76                 self.planet     = Planet(bloglist)
77                 self.items      = self.planet.sort()
78         
79         def write(self, doctype):
80                 # doctype should be something like XHTMLWriter.XHTMLWriter
81                 writer          = doctype(self.items)
82                 writer.parent   = self
83                 output          = writer.write()
84                 return output
85

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