#!/usr/bin/python

from mod_python import apache, Cookie
from xml.dom.minidom import parse, parseString
from os import listdir, chdir, remove
from datetime import datetime

PICTUREDIR = "/home/rumith/smdc/img/photon/2h/"

def index(req):
	chdir(PICTUREDIR)
	req.content_type = "text/html"
	domFile = parse(PICTUREDIR + "embed.html")

	files = sorted(listdir(PICTUREDIR), cmp)
	selectList = domFile.getElementsByTagName("select")
	select = getElem(selectList, "id", "picSelect")
	for f in files:
		try:
			dt = datetime.strptime(f[:-4], "%Y-%m-%d_%H:%M:%S")
		except:
			continue
		option = domFile.createElement("option")
		option.setAttribute("title", f)
		option.appendChild(domFile.createTextNode(dt.strftime("%d %b %Y, %H:00")))
		select.appendChild(option)

	return domFile.toxml()

def getElem(elemList, attrName, attrValue):
	result = None
	for i in range (0, elemList.length):
		if elemList.item(i).getAttribute(attrName) == attrValue:
			result = elemList.item(i)
			break
	return result

