<s> from __future__ import absolute_import , division , with_statement <EOL> import sys <EOL> import string <EOL> import math <EOL> import csv <EOL> import gd <EOL> __author__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __status__ = "<STR_LIT>" <EOL> class SlicedImage : <EOL> def __init__ ( self , width , height ) : <EOL> self . __image = gd . image ( ( width , height ) ) <EOL> self . __xOffset = <NUM_LIT:0> <EOL> self . __height = height <EOL> self . diamondWidth = <NUM_LIT:7> <EOL> """<STR_LIT>""" <EOL> self . diamondLeftMargin = <NUM_LIT:2> <EOL> """<STR_LIT>""" <EOL> self . diamondVerticalMargin = <NUM_LIT:0> <EOL> """<STR_LIT>""" <EOL> self . diamondColor = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> """<STR_LIT>""" <EOL> return <EOL> def addSlice ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> def computeRgbValues ( rgb , saturation , brightness ) : <EOL> return map ( ( lambda x : <EOL> adjustColorComponent ( x , saturation , brightness ) ) , rgb ) <EOL> def adjustColorComponent ( component , saturation , brightness ) : <EOL> answer = component * saturation + brightness * <NUM_LIT> * ( <NUM_LIT:1> - saturation ) <EOL> return int ( round ( answer ) ) <EOL> width = fields [ "<STR_LIT:width>" ] <EOL> height = fields [ "<STR_LIT>" ] <EOL> saturation = fields [ "<STR_LIT>" ] <EOL> brightness = fields [ "<STR_LIT>" ] <EOL> upperHeight = int ( round ( height - ( height * fields [ "<STR_LIT>" ] ) ) ) <EOL> tl = ( self . __xOffset , upperHeight + <NUM_LIT:1> ) <EOL> br = ( self . __xOffset + width , height ) <EOL> lowerColor = self . __image . colorAllocate ( <EOL> computeRgbValues ( fields [ "<STR_LIT>" ] , saturation , brightness ) ) <EOL> self . __image . filledRectangle ( tl , br , lowerColor ) <EOL> tl = ( self . __xOffset , upperHeight ) <EOL> br = ( self . __xOffset + width , upperHeight + <NUM_LIT:1> ) <EOL> dividerColor = self . __image . colorAllocate ( <EOL> computeRgbValues ( fields [ "<STR_LIT>" ] , saturation , brightness ) ) <EOL> self . __image . filledRectangle ( tl , br , dividerColor ) <EOL> tl = ( self . __xOffset , <NUM_LIT:0> ) <EOL> br = ( self . __xOffset + width , upperHeight ) <EOL> upperColor = self . __image . colorAllocate ( <EOL> computeRgbValues ( fields [ "<STR_LIT>" ] , saturation , brightness ) ) <EOL> self . __image . filledRectangle ( tl , br , upperColor ) <EOL> self . __xOffset += width <EOL> return <EOL> def drawDiamond ( self , startX ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . __diamondColorIndex = self . __image . colorAllocate ( <EOL> self . diamondColor ) <EOL> bottomPoint = ( startX + int ( math . ceil ( self . diamondWidth / <NUM_LIT:2> ) ) , <EOL> self . __height - <NUM_LIT:1> - self . diamondVerticalMargin ) <EOL> rightPoint = ( startX + self . diamondWidth - <NUM_LIT:1> , int ( ( self . __height - <NUM_LIT:1> ) / <NUM_LIT:2> ) ) <EOL> topPoint = ( startX + int ( math . ceil ( self . diamondWidth / <NUM_LIT:2> ) ) , <EOL> self . diamondVerticalMargin ) <EOL> leftPoint = ( startX + self . diamondLeftMargin , int ( self . __height / <NUM_LIT:2> ) ) <EOL> diamond = ( bottomPoint , rightPoint , topPoint , leftPoint ) <EOL> self . __image . filledPolygon ( diamond , self . __diamondColorIndex ) <EOL> return <EOL> def drawFilledSlice ( self , startX , width , color ) : <EOL> """<STR_LIT>""" <EOL> colorIndex = self . __image . colorAllocate ( color ) <EOL> self . __image . filledRectangle ( ( startX , <NUM_LIT:0> ) , <EOL> ( startX + width - <NUM_LIT:1> , self . __height ) , <EOL> colorIndex ) <EOL> return <EOL> def generate ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> file = open ( filename , "<STR_LIT:w>" ) <EOL> self . __image . writePng ( file ) <EOL> file . close ( ) <EOL> return <EOL> def parseHexColor ( hex ) : <EOL> justHex = hex . split ( '<STR_LIT:#>' ) [ - <NUM_LIT:1> ] <EOL> return map ( ( lambda x : string . atoi ( x , <NUM_LIT:16> ) ) , ( justHex [ <NUM_LIT:0> : <NUM_LIT:2> ] , justHex [ <NUM_LIT:2> : <NUM_LIT:4> ] , justHex [ <NUM_LIT:4> : <NUM_LIT:6> ] ) ) <EOL> def calculateTotalSize ( inputFileName ) : <EOL> width = <NUM_LIT:0> <EOL> height = <NUM_LIT:0> <EOL> reader = csv . reader ( file ( inputFileName ) ) <EOL> for row in reader : <EOL> fields = mapInputFields ( row ) <EOL> width += fields [ "<STR_LIT:width>" ] <EOL> if fields [ "<STR_LIT>" ] > height : <EOL> height = fields [ "<STR_LIT>" ] <EOL> return int ( width ) , int ( height ) <EOL> def mapInputFields ( row ) : <EOL> fields = [ "<STR_LIT>" , "<STR_LIT:width>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> numericFieldNos = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:7> ] <EOL> percentFieldNos = [ <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:7> ] <EOL> hexFieldNos = [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] <EOL> i = <NUM_LIT:0> <EOL> answer = { } <EOL> for field in fields : <EOL> value = row [ i ] <EOL> if i in numericFieldNos : <EOL> value = string . atof ( value ) <EOL> if i in percentFieldNos : <EOL> value = value / <NUM_LIT> <EOL> else : <EOL> if i in hexFieldNos : <EOL> value = parseHexColor ( value ) <EOL> answer [ field ] = value <EOL> i = i + <NUM_LIT:1> <EOL> return answer <EOL> def makeSlices ( width , height , inputFileName , outputFileName ) : <EOL> slices = SlicedImage ( width , height ) ; <EOL> reader = csv . reader ( file ( inputFileName ) ) <EOL> for row in reader : <EOL> fields = mapInputFields ( row ) <EOL> slices . addSlice ( fields ) <EOL> slices . generate ( outputFileName ) <EOL> return <EOL> def generateImage ( inFile , outFile ) : <EOL> print "<STR_LIT>" % ( inFile , outFile ) <EOL> width , height = calculateTotalSize ( inFile ) <EOL> slices = makeSlices ( width , height , inFile , outFile ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> print "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" <EOL> else : <EOL> inFile = sys . argv [ <NUM_LIT:1> ] <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> outFile = inFile . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> else : <EOL> outFile = sys . argv [ <NUM_LIT:2> ] <EOL> generateImage ( inFile , outFile ) </s>
