Roundup Tracker

Attachment 'rsswriter.py'

Download

   1 #!/usr/bin/python
   2 
   3 #
   4 #  RSS writer Roundup reactor
   5 #  Mark Paschal <markpasc@markpasc.org>
   6 #
   7 
   8 
   9 # The filename of a tracker's RSS feed. Tracker config variables are placed
  10 # with the standard '%' operator syntax.
  11 
  12 FILENAME = "%(TEMPLATES)s/rss.xml"  # i.e., roundup.cgi/projects/_file/rss.xml
  13 # FILENAME = "/home/markpasc/public_html/%(TRACKER_NAME)s.xml"
  14 
  15 # How many <item>s to have in the feed, at most.
  16 MAX_ITEMS = 30
  17 
  18 
  19 #
  20 #  Module metadata
  21 #
  22 
  23 __author__ = "Mark Paschal <markpasc@markpasc.org>"
  24 __copyright__ = "Copyright 2003 Mark Paschal"
  25 __version__ = "1.2"
  26 
  27 __changes__ = """
  28 1.1  29 Aug 2003  Produces valid pubDates. Produces pubDates and authors for
  29                   change notes. Consolidates a message and change note into one
  30                   item. Uses TRACKER_NAME in filename to produce one feed per
  31                   tracker. Keeps to MAX_ITEMS limit more efficiently.
  32 1.2   5 Sep 2003  Fixes bug with programmatically submitted issues having
  33                   messages without summaries (?!).
  34 """
  35 
  36 __license__ = 'MIT'
  37 
  38 #
  39 #  Copyright 2003 Mark Paschal
  40 #
  41 #  Permission is hereby granted, free of charge, to any person obtaining a copy
  42 #  of this software and associated documentation files (the "Software"), to deal
  43 #  in the Software without restriction, including without limitation the rights
  44 #  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  45 #  copies of the Software, and to permit persons to whom the Software is
  46 #  furnished to do so, subject to the following conditions:
  47 #
  48 #  The above copyright notice and this permission notice shall be included in all
  49 #  copies or substantial portions of the Software.
  50 #
  51 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  52 #  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  53 #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  54 #  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  55 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  56 #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  57 #  SOFTWARE.
  58 #
  59 
  60 
  61 # The strftime format to use for <pubDate>s.
  62 RSS20_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S %Z'
  63 
  64 
  65 def newRss(title, link, description):
  66 	"""Returns an XML Document containing an RSS 2.0 feed with no items."""
  67 	import xml.dom.minidom
  68 	rss = xml.dom.minidom.Document()
  69 
  70 	root = rss.appendChild(rss.createElement("rss"))
  71 	root.setAttribute("version", "2.0")
  72 
  73 	channel = root.appendChild(rss.createElement("channel"))
  74 	addEl = lambda tag,value: channel.appendChild(rss.createElement(tag)).appendChild(rss.createTextNode(value))
  75 	addEl("title", title)
  76 	addEl("link", link)
  77 	addEl("description", description)
  78 
  79 	return rss  # has no items
  80 
  81 
  82 def writeRss(db, cl, nodeid, olddata):
  83 	"""
  84 	Reacts to a created or changed issue. Puts new messages and the change note
  85 	in items in the RSS feed, as determined by the rsswriter.py FILENAME setting.
  86 	If no RSS feed exists where FILENAME specifies, a new feed is created with
  87 	rsswriter.newRss.
  88 	"""
  89 	filename = FILENAME % db.config.__dict__
  90 
  91 	# open the RSS
  92 	import xml.dom.minidom
  93 	try:
  94 		rss = xml.dom.minidom.parse(filename)
  95 	except IOError, e:
  96 		if 2 <> e.errno: raise
  97 		# File not found
  98 		rss = newRss(
  99 			"%s tracker" % (db.config.TRACKER_NAME,),
 100 			db.config.TRACKER_WEB,
 101 			"Recent changes to the %s Roundup issue tracker" % (db.config.TRACKER_NAME,)
 102 		)
 103 
 104 	channel = rss.documentElement.getElementsByTagName('channel')[0]
 105 	addEl = lambda parent,tag,value: parent.appendChild(rss.createElement(tag)).appendChild(rss.createTextNode(value))
 106 	issuelink = '%sissue%s' % (db.config.TRACKER_WEB, nodeid)
 107 
 108 
 109 	if olddata:
 110 		chg = cl.generateChangeNote(nodeid, olddata)
 111 	else:
 112 		chg = cl.generateCreateNote(nodeid)
 113 
 114 	def addItem(desc, date, userid):
 115 		"""
 116 		Adds an RSS item to the RSS document. The title, link, and comments
 117 		link are those of the current issue.
 118 		
 119 		desc: the description text to use
 120 		date: an appropriately formatted string for pubDate
 121 		userid: a Roundup user ID to use as author
 122 		"""
 123 
 124 		item = rss.createElement('item')
 125 
 126 		addEl(item, 'title', db.issue.get(nodeid, 'title'))
 127 		addEl(item, 'link', issuelink)
 128 		addEl(item, 'comments', issuelink)
 129 		addEl(item, 'description', desc.replace('&','&amp;').replace('<','&lt;').replace('\n', '<br>\n'))
 130 		addEl(item, 'pubDate', date)
 131 		addEl(item, 'author',
 132 			'%s (%s)' % (
 133 				db.user.get(userid, 'address'),
 134 				db.user.get(userid, 'username')
 135 			)
 136 		)
 137 
 138 		channel.appendChild(item)
 139 
 140 
 141 	from nosyreaction import determineNewMessages
 142 	for msgid in determineNewMessages(cl, nodeid, olddata):
 143 		desc = db.msg.get(msgid, 'content')
 144 
 145 		if desc and chg:
 146 			desc += chg
 147 		elif chg:
 148 			desc = chg
 149 		chg = None
 150 
 151 		addItem(desc or '', db.msg.get(msgid, 'date').pretty(RSS20_DATE_FORMAT), db.msg.get(msgid, 'author'))
 152 
 153 	if chg:
 154 		from time import strftime
 155 		addItem(chg.replace('\n----------\n', ''), strftime(RSS20_DATE_FORMAT), db.getuid())
 156 
 157 
 158 	for c in channel.getElementsByTagName('item')[0:-MAX_ITEMS]:  # leaves at most MAX_ITEMS at the end
 159 		channel.removeChild(c)
 160 
 161 	# write the RSS
 162 	out = file(filename, 'w')
 163 	out.write(rss.toxml())
 164 	out.close()
 165 
 166 
 167 def init(db):
 168 	db.issue.react('create', writeRss)
 169 	db.issue.react('set', writeRss)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2014-04-09 18:36:27, 5.1 KB) [[attachment:rsswriter.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.