#!/usr/bin/python2.3

"""
	Kenneth <langly> Ostbys Booklist plugin
	for PyBlosxom.
	kenneth.ostby@gmail.com
	http://www.langly.org
		  
	At the moment, the text file containing the
	booklist should be formated something like this:
	--
	AISN Booktitle
	AISN Booktitle Can be several words
	--
"""

maxBooks = 4 # Maximum number of books
path = "./blist.txt" #Where to find the booklist

class Book:
	def __init__(self,title,aisn):
		self.bookdata = {'title':title,'aisn':aisn};

	def __str__(self):
		book = """
		<div class="book">
			<a href="http://www.amazon.com/exec/obidos/tg/detail/-/%(aisn)s/">
			<img src="http://images.amazon.com/images/P/%(aisn)s.01.__SCTZZZZZZZ_.jpg"><br>
			%(title)s
			</a>
		</div>"""
		return book % self.bookdata
		
class Booklist:
	def __init__(self,file,max):
		self._html = None
		self.maxPosts = max
		self.books = []
		file = open(file)

		for line in file:
			tmp = line.split(' ',1)
			b = Book(tmp[1],tmp[0])
			self.books.append(b)

		file.close()
		file = None

	def __str__(self):
		return self.generateHTML()
	
	def generateHTML(self):
		t = """<div class="books" style="text-align:center">
				%(books)s
</div>"""
		s=""
		i=0;
		for book in self.books:
			s += str(book)	
			if i >= self.maxPosts:
				break;
			i +=1
		return t % {'books':s}

def cb_prepare(args):
        request = args["request"]
        data = request.getData()
        data["booklist"] = Booklist(path,maxBooks)

#For debugging only
if __name__ == "__main__":
	list = Booklist(path,maxBooks)
	print list
