#!/usr/bin/python

#
#  RSS writer Roundup reactor
#  Mark Paschal <markpasc@markpasc.org>
#


# The filename of a tracker's RSS feed. Tracker config variables are placed
# with the standard '%' operator syntax.

FILENAME = "%(TEMPLATES)s/rss.xml"  # i.e., roundup.cgi/projects/_file/rss.xml
# FILENAME = "/home/markpasc/public_html/%(TRACKER_NAME)s.xml"

# How many <item>s to have in the feed, at most.
MAX_ITEMS = 30


#
#  Module metadata
#

__author__ = "Mark Paschal <markpasc@markpasc.org>"
__copyright__ = "Copyright 2003 Mark Paschal"
__version__ = "1.2"

__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.
1.2   5 Sep 2003  Fixes bug with programmatically submitted issues having
                  messages without summaries (?!).
"""

__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.
#


# The strftime format to use for <pubDate>s.
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.__dict__

	# 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('&','&amp;').replace('<','&lt;').replace('\n', '<br>\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, 'content')

		if desc and chg:
			desc += chg
		elif chg:
			desc = chg
		chg = None

		addItem(desc or '', 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.getuid())


	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())
	out.close()


def init(db):
	db.issue.react('create', writeRss)
	db.issue.react('set', writeRss)
