#!/usr/bin/python python
"""Traverse filespec of *.html looking for select tag and write out report of options."""

import os
import sys

from xml.sax.saxutils import escape

def process(filename, fp):
 print "* Processing:", filename, "\n"
 # parse the file
 select_of_htmlFile = open(filename)  
 fp.write("<h1 style='color:blue;'>Filename: " + filename + "</h1>\n")
 prefix = "<p style='color:purple;'>"
 inSelect = 0
 line = select_of_htmlFile.readline( )
 while line:
  line = " " + line.strip( )
  if line.find("<select") > 0:
   if inSelect:
    fp.write(prefix + "</p>\n")
    
   inSelect = 1
   if line.find("id=") > 0:
    v = line.index("id=")
    dm = line[(v+3):(v+4)]
    v2 = line[(v+4):].index(dm) + v + 4
    fp.write("<h2 style='color:red;'>Dropdown: " + line[(v+4):v2] + "</h2>\n")

  if line.find("</select>") > 0:
   if inSelect:
    fp.write(prefix + "</p>\n")
    inSelect = 0
    
  elif line.find("document.write") > 0:
   inSelect = inSelect
   
  elif line.find("<option") > 0 and line.find("<") > 0 and inSelect:
   v2 = line.index("<option")
   v = line[v2:].index(">") + v2
   v2 = line[v:].index("<") + v
   fp.write(prefix + line[(v+1):v2] + "<br>\n")
   prefix = ""

  line = select_of_htmlFile.readline( )

 select_of_htmlFile.close( )
 if inSelect:
  fp.write(prefix + "</p>\n")
  prefix = ""
  inSelect = 0

def finder(fp, dirname, names):
 """Add files in the directory dirname to a list."""
 for name in names:
  if name.endswith(".html"):
   path = os.path.join(dirname, name)
   if os.path.isfile(path):
    process(path, fp)

def main( ):
 print "[extract_select_option_values.py started]"

 xmlFd = open("extract_select_option_values.htm", "w")
 xmlFd.write("<html>\n")
 xmlFd.write("<body>\n")

 os.path.walk(sys.argv[1], finder, xmlFd)

 xmlFd.write("</body>\n")
 xmlFd.write("</html>\n")
 xmlFd.close( )

 print "[extract_select_option_values.py finished]"
if __name__ == "__main__":
 main( ) 