#!/usr/bin/python def buildFileToken(fileName): ## Given a .template.html file, change it to ## its token value by removing .tempate.html and ## wrapping with with tempName = fileName.replace(".template.html", "") fileToken = "" return fileToken def normalizeDir(dir): ## Look for trailing dir "/". If not there add it. if (dir[len(dir)-1] != "/"): dir = dir + "/" return dir def validateDir(dir): import os ## If directory doesn't exist, return failure if os.path.exists(dir): return 1 else: return -1 def getHTMRFiles(dir, filelist): import os import fnmatch ## For all diretories, get list of htmr files for root, dirs, files in os.walk(htmrDir): for name in files: if fnmatch.fnmatch(name, "*.htmr.html"): filelist.append(os.path.join(root, name)) for d in dirs: print os.path.join(root, d) def getTemplateTuples(dir, filelist): import os import fnmatch ## For all directories, build template tuples from template files for root, dirs, files in os.walk(htmrDir): for name in files: if fnmatch.fnmatch(name, "*.template.html"): ## Get the token from the file name fileToken = buildFileToken(name) ## Open and read in the template file inFile = open(os.path.join(root, name), "r") fileText = inFile.read() inFile.close() ## Create the tuple and add it to the list templateTuple = (fileToken, fileText) filelist.append(templateTuple) def generateHTML(htmrDir, htmlDir): filesHTMR = [] templateTuples = [] ## Get a list of our HTMR files getHTMRFiles(htmrDir, filesHTMR) ## Get a list of our template tuples, ## the tuple being (, htmlvalue) getTemplateTuples(htmrDir, templateTuples) ## For each HTMR file found, read in the contents, ## add the header/footer info and write it to the HTML file for fileHTMR in filesHTMR: ## Read in HTMR file inFile = open(fileHTMR, "r") contentHTML = inFile.read() inFile.close() ## Replace any existing token with it's htmlvalue for templateTuple in templateTuples: contentHTML = contentHTML.replace(templateTuple[0], templateTuple[1]) ## Write the value of the workingDir/htmr.html file into ## webDir/html file. If dir doesn't exist, make it fileHTML = fileHTMR.replace(htmrDir, htmlDir) fileHTML = fileHTML.replace(".htmr.html", ".html") head, tail = os.path.split(fileHTML) if not os.path.exists(head): os.makedirs(head, 0755) outFile = open(fileHTML, "w") outFile.write(contentHTML) outFile.close() if __name__ == "__main__": import sys import os htmrDir = "~/public_html_htmr/" htmlDir = "~/public_html_gen/" ## If we have arugments, use them if sys.argv[1:]: htmrDir = sys.argv[1] if sys.argv[2:]: htmlDir = sys.argv[2] ## Make sure both directories exist befor continuing returnCode = validateDir(htmrDir) if returnCode == -1: print "A valid htmrDir must be entered. " + htmrDir + " is not valid." exit ## If the destination directory doesn't exist, make it returnCode = validateDir(htmlDir) if returnCode == -1: os.makedirs(htmlDir, 0755) ## Make sure directories have trailing slashes htmrDir = normalizeDir(htmrDir) htmlDir = normalizeDir(htmlDir) ## Do the real work generateHTML(htmrDir, htmlDir)