#!/usr/bin/python # # RSS writer Roundup reactor # __author__ = "Mark Paschal " __copyright__ = "Copyright 2003 Mark Paschal" __version__ = "1.1" __changes__ = """ 1.1 29 Aug 2003 Produces valid pubDates. Produces pubDates and authors for change notes. Consolidates a message and change note into one item. Uses TRACKER_NAME in filename to produce one feed per tracker. Keeps to MAX_ITEMS limit more efficiently. """ __license__ = 'MIT' # # Copyright 2003 Mark Paschal # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # FILENAME = "/home/markpasc/public_html/%s.xml" MAX_ITEMS = 10 RSS20_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S %Z' def newRss(title, link, description): """Returns an XML Document containing an RSS 2.0 feed with no items.""" import xml.dom.minidom rss = xml.dom.minidom.Document() root = rss.appendChild(rss.createElement("rss")) root.setAttribute("version", "2.0") channel = root.appendChild(rss.createElement("channel")) addEl = lambda tag,value: channel.appendChild(rss.createElement(tag)).appendChild(rss.createTextNode(value)) addEl("title", title) addEl("link", link) addEl("description", description) return rss # has no items def writeRss(db, cl, nodeid, olddata): """ Reacts to a created or changed issue. Puts new messages and the change note in items in the RSS feed, as determined by the rsswriter.py FILENAME setting. If no RSS feed exists where FILENAME specifies, a new feed is created with rsswriter.newRss. """ filename = FILENAME % (db.config.TRACKER_NAME,) # open the RSS import xml.dom.minidom try: rss = xml.dom.minidom.parse(filename) except IOError, e: if 2 <> e.errno: raise # File not found rss = newRss( "%s tracker" % (db.config.TRACKER_NAME,), db.config.TRACKER_WEB, "Recent changes to the %s Roundup issue tracker" % (db.config.TRACKER_NAME,) ) channel = rss.documentElement.getElementsByTagName('channel')[0] addEl = lambda parent,tag,value: parent.appendChild(rss.createElement(tag)).appendChild(rss.createTextNode(value)) issuelink = '%sissue%s' % (db.config.TRACKER_WEB, nodeid) if olddata: chg = cl.generateChangeNote(nodeid, olddata) else: chg = cl.generateCreateNote(nodeid) def addItem(desc, date, userid): """ Adds an RSS item to the RSS document. The title, link, and comments link are those of the current issue. desc: the description text to use date: an appropriately formatted string for pubDate userid: a Roundup user ID to use as author """ item = rss.createElement('item') addEl(item, 'title', db.issue.get(nodeid, 'title')) addEl(item, 'link', issuelink) addEl(item, 'comments', issuelink) addEl(item, 'description', desc.replace('\n', '
\n')) addEl(item, 'pubDate', date) addEl(item, 'author', '%s (%s)' % ( db.user.get(userid, 'address'), db.user.get(userid, 'username') ) ) channel.appendChild(item) from nosyreaction import determineNewMessages for msgid in determineNewMessages(cl, nodeid, olddata): desc = db.msg.get(msgid, 'summary') if chg: desc += chg chg = None addItem(desc, db.msg.get(msgid, 'date').pretty(RSS20_DATE_FORMAT), db.msg.get(msgid, 'author')) if chg: from time import strftime addItem(chg.replace('\n----------\n', ''), strftime(RSS20_DATE_FORMAT), db.curuserid) for c in channel.getElementsByTagName('item')[0:-MAX_ITEMS]: # leaves at most MAX_ITEMS at the end channel.removeChild(c) # write the RSS out = file(FILENAME, 'w') out.write(rss.toxml()) def init(db): db.issue.react('create', writeRss) db.issue.react('set', writeRss)