"Miscellaneous utilities for checking html"

#from mx.Tidy import tidy
import sgmllib
from pprint import pprint as pp


class PageChecker(sgmllib.SGMLParser):
    """This scans web pages and checks for any
    stuff we don't like.  Extend it over time with
    new tags!
    """
    def __init__(self, pageContent):
        sgmllib.SGMLParser().__init__(self)
        self._inputFields = {}
        self.reset()
        self.feed(pageContent)
        self.close()
        

    def hasFormField(self, fieldName):
        return self._inputFields.has_key(fieldName)

    def getFormFieldValue(self, fieldName):
        return self._inputFields[fieldName]


    def start_input(self, attributes):
        tmpDict = {}
        for (key, value) in attributes:
            tmpDict[key.lower()] = value
        typ = tmpDict.get('type', None)
        name = tmpDict.get('name', None)
        value = tmpDict.get('value', None)
        self._inputFields[name] = value

        
def getHiddenFields(html):
    """Parses a page crudely and returns a dict of
    all hidden fields i.e. things like
      <input type="hidden" name="login_saved_params" value="fXEBKFUGY">
    """
    pass

    





##def checkPage(input):
##    (nerrors, nwarnings, output, errordata) = tidy(input)
##    print '%d erros, %d warnings' % (nerrors, nwarnings)
##    print 'error data follows:'
##    print errordata


samples = [
"""<html><head><title>My Page</title></head>
<body>Hello world</body></html>""",
"""<html><head><title>My Page</title></head>
<body>Hello world</bod></html>""",
"""<html><head><title>My Page</title></head>
<body>
    <form>
        <input type="hidden" name="dinner" value="spam">
    </form>
</body></html>"""
    ]

def test():
    for i in range(len(samples)):
        print 'sample %d:' % i
        pcp = PageChecker(samples[i])
        print 'fields:'
        pp(pcp.inputFields)
        print 'stack'
        pp(pcp.stack)
        #checkPage(samples[i])


if __name__=='__main__':
    test()

    


