#!/usr/bin/env python import cgi import string import sys import re # this defines an exception string in case our template file is messed up badTemplateException = "There was a problem with the HTML template." badFileException = "There was a problem opening the HTML template." #Constants VERSION = "0.2a" DEBUG = 0 cssOrder = ["top","right","bottom","left"] def pxInt(i): return str(i)+"px" def cssDict(top,right,bottom,left): """ Create a dict with the four CSS values """ d={} d["top"]=top d["right"]=right d["bottom"]=bottom d["left"]=left return d class Grid: def __init__(self,width,margins,paddingDict,border,colorBg): """Initializes the Grid class, with general css parameters""" self.width = width-(paddingDict["left"]+paddingDict["right"]) self.paddingDict = paddingDict self.margin = margins if border: self.border = border else: self.border = 'no' if colorBg: self.colorBg = colorBg else: self.colorBg = "#FFFFFF" self.comments = """ CSS file generated by Reticulator v"""+VERSION self.cellStyles = [] def createCells(self,numberX,cellHeight,separateBy,colorFg): """Create cells and calculates all its position""" self.cellMargin = separateBy if colorFg: self.cellColorFg = colorFg else: self.cellColorFg = "#E0E0E0" if DEBUG>0: print "1.1" #The width and height of a cell come #by the total width and the number of cells, without the margin #Precise mode, with float precision #self.cellWidth = (self.width -((numberX-1)*self.cellMargin) )/ float(numberX) ##### #Integer mode, no floats, precise, but width is not exactly the same as specified by the user self.cellWidth = self.width / numberX self.width = (self.cellWidth * numberX)-self.cellMargin self.cellWidth = self.cellWidth - (self.cellMargin) ###### if DEBUG>0: print "1.2" if cellHeight!=0: self.cellHeight = cellHeight else: self.cellHeight = self.cellWidth if DEBUG>0: print "1.3" #Vertical position ## self.columns=[0] #Horizontal position ## self.upRows = [0] self.downRows = [self.cellMargin] for i in range(numberX-1): newColumn = self.columns[-1]+(self.cellWidth+self.cellMargin) self.columns.append(newColumn) #upRows newUpRow = self.upRows[-1]+(self.cellWidth+self.cellMargin) self.upRows.append(newUpRow) #downRows newDownRow = self.downRows[-1]+(self.cellWidth+self.cellMargin) self.downRows.append(newDownRow) #First was only use for reference, so we remove it self.upRows = self.upRows[1:] self.downRows = self.downRows[1:] #Margins for UpRow are negative self.upRows = map(lambda item:(-1)*item,self.upRows) if DEBUG>0: print "1.4" def addStyle(self, borderDict, border, bgcolor): style = {} style["width"] = self.cellWidth \ - borderDict["left"] - borderDict["right"] style["height"] = self.cellHeight \ - borderDict["top"] - borderDict["bottom"] style["widthLong"] = style["width"] \ + self.cellWidth + self.cellMargin style["heightLong"] = style["height"] \ + self.cellHeight + self.cellMargin style["bgcolor"] = str(bgcolor) style["border"] = str(border) style["borderDict"] = borderDict self.cellStyles.append(style) def generateCSS(self): content="" ## try: ## fp = file(filename,"w") ## except: ## print "Unable to open CSS file!" ## sys.exit(0) if DEBUG>0: print "3.1" content+= "/*"+self.comments+"*/\n" content+= "html,p {\n" content+= "\tpadding: 0px ;\n" content+= "\tmargin: 0px ;\n" content+= "}\n\n" if DEBUG>0: print "3.2" content+= "/* Content is the main div. */\n" content+= "#content {\n" content+= "\twidth:"+pxInt(self.width)+";\n" content+= "\tmargin:"+self.margin+";\n" if self.border!="no": content += "\tborder: "+self.border+";\n" content += "\tbackground-color:"+self.colorBg+";\n" temp="" for item in cssOrder: temp=temp+" "+pxInt(self.paddingDict[item]) content+= "\tpadding:"+temp+";\n" content+= "}\n\n" content+= "/* subContent is a secondary main div. It has the cells background image, \n and allows to separate the cells and the main border */\n" content += "#subContent {\n " content+= "\twidth:"+pxInt(self.width)+";\n" content += "\tpadding:0px; \n" content += "\tmargin:0px; \n" content += "\tbackground-color:#FbFbFb;\n" content += "\tbackground-image: url(/cgi-bin/bgimage.php?width="+str(self.cellWidth)+"&height="+str(self.cellHeight)+"&margin="+str(self.cellMargin)+"&colorBg="+self.colorBg[1:]+"&colorFg="+self.cellColorFg[1:]+");\n" #colorBg[1:] The PHP file gets hexadecimal color without the '#' content += "}\n\n" if DEBUG>0: print "3.3" content+= "/* The cell class is the main class to give a div cell properties. */\n" content+= ".cell {\n" content+= "\twidth:"+pxInt(self.cellWidth)+";\n" content+= "\theight:"+pxInt(self.cellHeight)+";\n" content+= "\tmargin-bottom:"+pxInt(self.cellMargin)+";\n" content+= "\toverflow: auto ;\n" content+= "\tbackground-color:gray;\n" content+= "}\n\n" if DEBUG>0: print "3.4" content+= "/* The last cell in your grid must have a #lastCell identifier. */\n" content+= "#lastCell {\n" content+= "\tmargin-bottom:"+pxInt(0)+";\n" content+= "}\n\n" if DEBUG>0: print "3.5" content+= ".cell p {\n" content+= "\tmargin-left:30px;\n" content+= "\tmargin-right:15px;\n" content+= "\tpadding-top:25px;\n" content+= "\tpadding-bottom:15px;\n" content+= "\tfont-size:small;\n" content+= "\tfont-family: Helvetica, Verdana, Arial, sans-serif;\n" content+= "}\n\n" if DEBUG>0: print "3.6" content+= "\n/*Relative grid vertical position */\n\n" content+= "/* Use these classes to position the cells in the column wanted. */\n" cnt=1 for item in self.columns: content+= ".column"+str(cnt)+" {\n" content+= "\tmargin-left:"+pxInt(item)+";\n" content+= "}\n" cnt+=1 if DEBUG>0: print "3.7" content+= "\n/*Relative grid horizontal position */\n\n" content+= "/* Use the upXRows to move up the cells, along the rows.\n Use the downXRows to move down the cells, along the rows.*/\n" cnt=1 for item in self.upRows: content+= ".up"+str(cnt)+"Rows {\n" content+= "\tmargin-top:"+pxInt(item)+";\n" content+= "}\n" cnt+=1 cnt=1 for item in self.downRows: content+= ".down"+str(cnt)+"Rows {\n" content+= "\tmargin-top:"+pxInt(item)+";\n" content+= "}\n" cnt+=1 if len(self.cellStyles)>0: #there are some cell styles content+= "\n/*Cell styles */\n\n" content+= "/* Here are the classes for the styles defined. For each style you have a .cellStyleX class\n which is the style aplied to a normal cell\n and a cellStyleX_LongH and cellStyleX_LongV\n to use long cells. */\n" cnt=1 for style in self.cellStyles: content+= ".cellStyle"+str(cnt)+" {\n" if style["bgcolor"]!="": content+= "\tbackground-color:"+style["bgcolor"]+";\n" #If the style width is the same as the cell width, #we don't need to specify it again if style["width"]!=self.cellWidth and style["width"]!=0: content+= "\twidth:"+pxInt(style["width"])+";\n" #Same for height if style["height"]!=self.cellHeight and style["height"]!=0: content+= "\theight:"+pxInt(style["height"])+ ";\n" #Borders for item in cssOrder: if style["borderDict"][item]!=0: content+= "\tborder-"+item+":"+\ pxInt(style["borderDict"][item])+\ " "+ style["border"]+";\n" content+= "}\n" #LongH ########## content+= ".cellStyle"+str(cnt)+"_LongH {\n" if style["bgcolor"]!="": content+= "\tbackground-color:"+style["bgcolor"]+";\n" content+= "\twidth:"+pxInt(style["widthLong"])+";\n" if style["height"]!=self.cellHeight and style["height"]!=0: content+= "\theight:"+pxInt(style["height"])+ ";\n" #Borders for item in cssOrder: if style["borderDict"][item]!=0: content+= "\tborder-"+item+":"+\ pxInt(style["borderDict"][item])+\ " "+ style["border"]+";\n" content+= "}\n" #LongV ########## content+= ".cellStyle"+str(cnt)+"_LongV {\n" if style["bgcolor"]!="": content+= "\tbackground-color:"+style["bgcolor"]+";\n" if style["width"]!=self.cellWidth and style["width"]!=0: content+= "\twidth:"+pxInt(style["width"])+";\n" content+= "\theight:"+pxInt(style["heightLong"])+ ";\n" #Borders for item in cssOrder: if style["borderDict"][item]!=0: content+= "\tborder-"+item+":"+\ pxInt(style["borderDict"][item])+\ " "+ style["border"]+";\n" content+= "}\n" cnt+=1 content += "\n/* End of CSS generated by the Reticulator */\n" return content def displayWeb(self,output=sys.stdout,templateFile="template.html"): """Define a new function called Display. It takes one parameter, a html template to parses the generated grid""" if DEBUG>0: print "2.1" #Open the template file for read-only and read the entire #file as a string try: templateHandle = open(templateFile, "r") except: raise badFileException templateInput = templateHandle.read() templateHandle.close() if DEBUG>0: print "2.2 2.3" title=VERSION content=self.generateCSS() if DEBUG>0: print content subResult = re.subn("", title,templateInput) if DEBUG>0: print "2.4" if subResult[1] == 0: if DEBUG>0: print "2.41" raise badTemplateException if DEBUG>0: print "2.5" subResult = re.subn("", content,subResult[0]) if subResult[1] == 0: if DEBUG>0: print "2.51" raise badTemplateException if DEBUG>0: print "2.6" bgimage = "/cgi-bin/bgimage.php?width="+str(self.cellWidth)+"&height="+str(self.cellHeight)+"&margin="+str(self.cellMargin)+"&colorBg="+self.colorBg[1:]+"&colorFg="+self.cellColorFg[1:] subResult = re.subn("", bgimage,subResult[0]) if subResult[1] == 0: if DEBUG>0: print "2.61" raise badTemplateException if DEBUG>0: print "2.7" print>>output, subResult[0] def consoleStart(): """Main function used for console mode. Neither webForm nor CGI support here, just for local tests. It generates an output.html file with the grid generated""" # Required header that tells the browser how to render the text. # print "Content-Type: text/html\n\n" if DEBUG>0: print "1" g = Grid(900, "30px auto",cssDict(50,50,50,50), "1px solid gray","#FFFFFF") if DEBUG>0: print "2" g.createCells(4, 4, 5, "#E0E0E0") #content=g.generateCSS() if DEBUG>0: print "3" g.addStyle(cssDict(30,30,0,0),"solid black","white") g.addStyle(cssDict(0,0,0,10),"solid gray","#ebebeb") try: fp = open("output.html", "w") # open in read only mode except: raise badFileException g.displayWeb(output=fp,templateFile="template.html") fp.close() if DEBUG>0: print "4" print "done!" def main(webForm): """CGI Main function. It reads from the webForm who submitted the petition, and generates the grid""" #webForm = cgi.FieldStorage() if DEBUG>0: for name in webForm.keys(): print "Input: " + name + " value: " + \ webForm[name].value + "
" if DEBUG>0: print "1" paddingDict = cssDict(int(webForm["paddingTop"].value),\ int(webForm["paddingRight"].value),\ int(webForm["paddingBottom"].value),\ int(webForm["paddingLeft"].value)) g = Grid(int(webForm["width"].value), webForm["margins"].value, paddingDict, webForm["border"].value, webForm["colorBg"].value) if DEBUG>0: print "2" g.createCells(int(webForm["numberX"].value), int(webForm["cellHeight"].value), int(webForm["separateBy"].value), webForm["colorFg"].value) border_1=cssDict(int(webForm["borderTop_1"].value),\ int(webForm["borderRight_1"].value),\ int(webForm["borderBottom_1"].value),\ int(webForm["borderLeft_1"].value)) border_2=cssDict(int(webForm["borderTop_2"].value),\ int(webForm["borderRight_2"].value),\ int(webForm["borderBottom_2"].value),\ int(webForm["borderLeft_2"].value)) g.addStyle(border_1,webForm["border_1"].value,webForm["color_1"].value) g.addStyle(border_2,webForm["border_2"].value,webForm["color_2"].value) #content=g.generateCSS() if DEBUG>0: print "3" g.displayWeb(templateFile="template.html") if DEBUG>0: print "4" #f = file("sample.css","r") #lines = f.readlines() #print lines if __name__ == "__main__": consoleStart()