#!/usr/bin/env python
import os
from coral.modeler import modeler
import coral.modeler.tools.documentationbrowser as docb
import coral
import tempfile
import qt

app = qt.QApplication([])
mb = docb.DocumentationBrowser(parent=None)
app.setMainWidget(mb)
mb.show()


def compareFile2Lang(fileName, language):
    print "\ntestExportHTML.py: Checking filename " + fileName + " to language " + language.getName() + " " + str(language.getVersion()) + "." + str(language.getRevision())  
    fd = os.popen("grep '>Element Definition' " + fileName + " | sed -e 's/.* \\(.*\\)<.*/\\1/;' | sort")
    read_elems = {}
    for name in fd.readlines():
        name = name.strip()
        read_elems[name] = 1
    elems = [ed for ed in language.__elements__() ]
    assert len(elems) > 0
    for ed in elems:
        print "Checking " + ed.getName()
        assert read_elems.has_key(ed.getName())
    assert len(elems) == len(read_elems)




tempfile1 = tempfile.mktemp('.html')
tempfile2 = tempfile.mktemp('.html')
tempfile3 = tempfile.mktemp('.html')
tempfile4 = tempfile.mktemp('.html')
tempfile5 = tempfile.mktemp('.html')
tempfile6 = tempfile.mktemp('.html')

mb.exportLang2HTML(tempfile1, langPointer=coral.Language.__getLanguage__("Config", 0, 1))
print "testExportHTML.py Tried to export Config 0.1 to html to filename: " + tempfile1
mb.exportLang2HTML(tempfile2, langPointer=coral.Language.__getLanguage__("SMD", 0, 1))
print "testExportHTML.py Tried to export SMD 0.1 to html to filename: " + tempfile2
mb.exportLang2HTML(tempfile3, langPointer=coral.Language.__getLanguage__("UML", 1, 1))
print "testExportHTML.py Tried to export UML 1.1 to html to filename: " + tempfile3
mb.exportLang2HTML(tempfile4, langPointer=coral.Language.__getLanguage__("UML", 1, 3))
print "testExportHTML.py Tried to export UML 1.3 html to filename: " + tempfile4
mb.exportLang2HTML(tempfile5, langPointer=coral.Language.__getLanguage__("UML", 1, 4))
print "testExportHTML.py Tried to export UML 1.4 to html to filename: " + tempfile5
mb.exportLang2HTML(tempfile6, langPointer=coral.Language.__getLanguage__("UML", 1, 5))
print "testExportHTML.py Tried to export UML 1.5 to html to filename: " + tempfile6

compareFile2Lang(tempfile1, coral.Language.__getLanguage__("Config", 0, 1))
compareFile2Lang(tempfile2, coral.Language.__getLanguage__("SMD", 0, 1))
compareFile2Lang(tempfile3, coral.Language.__getLanguage__("UML", 1, 1))
compareFile2Lang(tempfile4, coral.Language.__getLanguage__("UML", 1, 3))
compareFile2Lang(tempfile5, coral.Language.__getLanguage__("UML", 1, 4))
compareFile2Lang(tempfile6, coral.Language.__getLanguage__("UML", 1, 5))

