<s> '''<STR_LIT>''' <EOL> import codecs <EOL> import colorsys <EOL> from xml . dom import minidom <EOL> from xml . dom import Node <EOL> import sys , getopt , os <EOL> class ColorMapEntry ( ) : <EOL> rgb = "<STR_LIT>" <EOL> transparent = False <EOL> sourceValue = "<STR_LIT>" <EOL> value = "<STR_LIT>" <EOL> label = "<STR_LIT>" <EOL> ref = "<STR_LIT>" <EOL> nodata = False <EOL> def __hash__ ( self ) : <EOL> return hash ( self . sourceValue ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . sourceValue . cmp ( other . sourceValue ) <EOL> def __eq__ ( self , other ) : <EOL> return self . sourceValue . eq ( other . sourceValue ) <EOL> class Entries ( ) : <EOL> minLabel = "<STR_LIT>" <EOL> maxLabel = "<STR_LIT>" <EOL> colormapentries = [ ] <EOL> def __hash__ ( self ) : <EOL> return hash ( self . minLabel ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . minLabel . cmp ( other . minLabel ) <EOL> def __eq__ ( self , other ) : <EOL> return self . minLabel . eq ( other . minLabel ) <EOL> class LegendEntry ( ) : <EOL> rgb = "<STR_LIT>" <EOL> label = "<STR_LIT>" <EOL> id = "<STR_LIT>" <EOL> showTick = False <EOL> showValue = False <EOL> def __hash__ ( self ) : <EOL> return hash ( self . label ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . label . cmp ( other . label ) <EOL> def __eq__ ( self , other ) : <EOL> return self . label . eq ( other . label ) <EOL> class Legend ( ) : <EOL> type = "<STR_LIT>" <EOL> minLabel = "<STR_LIT>" <EOL> maxLabel = "<STR_LIT>" <EOL> legendentries = [ ] <EOL> def __hash__ ( self ) : <EOL> return hash ( self . type ) <EOL> class ColorMap ( ) : <EOL> title = "<STR_LIT>" <EOL> units = "<STR_LIT>" <EOL> entries = None <EOL> legend = None <EOL> def __hash__ ( self ) : <EOL> return hash ( self . title ) <EOL> class ColorMaps ( ) : <EOL> colormaps = [ ] <EOL> product = "<STR_LIT>" <EOL> def __hash__ ( self ) : <EOL> return hash ( self . product ) <EOL> def __cmp__ ( self , other ) : <EOL> return self . product . cmp ( other . product ) <EOL> def __eq__ ( self , other ) : <EOL> return self . product . eq ( other . product ) <EOL> colorMapsList = [ ] <EOL> def getText ( nodelist ) : <EOL> rc = [ ] <EOL> for node in nodelist : <EOL> if node . nodeType == node . TEXT_NODE : <EOL> rc . append ( node . data ) <EOL> return '<STR_LIT>' . join ( rc ) <EOL> def parseColorMaps ( sourceXml , fileName ) : <EOL> xmldoc = minidom . parse ( sourceXml ) <EOL> colorMaps = ColorMaps ( ) <EOL> colorMaps . product = os . path . basename ( fileName ) <EOL> for colorMapNode in xmldoc . documentElement . getElementsByTagName ( "<STR_LIT>" ) : <EOL> colorMapAttrDict = dict ( colorMapNode . attributes . items ( ) ) <EOL> colorMap = ColorMap ( ) <EOL> colorMap . title = colorMapAttrDict . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> colorMap . units = colorMapAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> colorMap . entries = parseEntries ( colorMapNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] ) <EOL> if len ( colorMapNode . getElementsByTagName ( "<STR_LIT>" ) ) > <NUM_LIT:0> : <EOL> colorMap . legend = parseLegend ( colorMapNode . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] ) <EOL> colorMaps . colormaps . append ( colorMap ) <EOL> colorMapsList . append ( colorMaps ) <EOL> def parseEntries ( entriesNode ) : <EOL> entriesAttrDict = dict ( entriesNode . attributes . items ( ) ) <EOL> entries = Entries ( ) <EOL> entries . minLabel = entriesAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> entries . maxLabel = entriesAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> entries . colormapentries = [ ] <EOL> for entryNode in entriesNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> entryAttrDict = dict ( entryNode . attributes . items ( ) ) <EOL> cmEntry = ColorMapEntry ( ) <EOL> if '<STR_LIT>' in entryAttrDict : <EOL> cmEntry . transparent = entryAttrDict [ '<STR_LIT>' ] <EOL> else : <EOL> cmEntry . transparent = False <EOL> if '<STR_LIT>' in entryAttrDict : <EOL> cmEntry . nodata = entryAttrDict [ '<STR_LIT>' ] <EOL> else : <EOL> cmEntry . nodata = False <EOL> cmEntry . rgb = entryAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> cmEntry . value = entryAttrDict . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> cmEntry . sourceValue = entryAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> cmEntry . label = entryAttrDict . get ( '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> cmEntry . ref = entryAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> entries . colormapentries . append ( cmEntry ) <EOL> return entries <EOL> def parseLegend ( legendNode ) : <EOL> legendAttrDict = dict ( legendNode . attributes . items ( ) ) <EOL> legend = Legend ( ) <EOL> legend . type = legendAttrDict . get ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> legend . minLabel = legendAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> legend . maxLabel = legendAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> legend . legendentries = [ ] <EOL> for entryNode in legendNode . getElementsByTagName ( "<STR_LIT>" ) : <EOL> legendAttrDict = dict ( entryNode . attributes . items ( ) ) <EOL> legendEntry = LegendEntry ( ) <EOL> legendEntry . rgb = legendAttrDict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> legendEntry . label = legendAttrDict . get ( '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> legendEntry . id = legendAttrDict . get ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> legendEntry . showTick = False if '<STR_LIT>' not in legendAttrDict else legendAttrDict . get ( '<STR_LIT>' ) == "<STR_LIT:true>" <EOL> legendEntry . showLabel = False if '<STR_LIT>' not in legendAttrDict else legendAttrDict . get ( '<STR_LIT>' ) == "<STR_LIT:true>" <EOL> legend . legendentries . append ( legendEntry ) <EOL> return legend <EOL> def is_bright ( color ) : <EOL> """<STR_LIT>""" <EOL> rgb = color_string_to_list ( color ) <EOL> yiq = ( ( rgb [ <NUM_LIT:0> ] * <NUM_LIT> ) + ( rgb [ <NUM_LIT:1> ] * <NUM_LIT> ) + ( rgb [ <NUM_LIT:2> ] * <NUM_LIT> ) ) / <NUM_LIT:1000> <EOL> return yiq > <NUM_LIT> <EOL> def rgb_to_hex ( color ) : <EOL> rgb = color_string_to_list ( color ) <EOL> return '<STR_LIT>' % ( rgb [ <NUM_LIT:0> ] , rgb [ <NUM_LIT:1> ] , rgb [ <NUM_LIT:2> ] ) <EOL> def color_string_to_list ( color ) : <EOL> rgb = [ ] <EOL> commaIdx0 = <NUM_LIT:0> <EOL> commaIdx1 = color . find ( '<STR_LIT:U+002C>' , commaIdx0 ) <EOL> rgb . append ( int ( color [ commaIdx0 : commaIdx1 ] ) ) <EOL> commaIdx0 = commaIdx1 + <NUM_LIT:1> <EOL> commaIdx1 = color . find ( '<STR_LIT:U+002C>' , commaIdx0 ) <EOL> rgb . append ( int ( color [ commaIdx0 : commaIdx1 ] ) ) <EOL> commaIdx0 = commaIdx1 + <NUM_LIT:1> <EOL> commaIdx1 = len ( color ) <EOL> rgb . append ( int ( color [ commaIdx0 : commaIdx1 ] ) ) <EOL> return rgb <EOL> def generateHTML ( ) : <EOL> for colorMaps in colorMapsList : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + colorMaps . product + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + colorMaps . product + "<STR_LIT>" ) <EOL> for colorMap in colorMaps . colormaps : <EOL> print ( "<STR_LIT>" + colorMap . title . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" + colorMap . units . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + colorMap . entries . minLabel . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" + colorMap . entries . maxLabel . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> for entry in colorMap . entries . colormapentries : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + rgb_to_hex ( entry . rgb ) + "<STR_LIT:>>" + "<STR_LIT>" + ( "<STR_LIT>" if is_bright ( entry . rgb ) else "<STR_LIT>" ) + "<STR_LIT>" + entry . rgb + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + str ( entry . transparent ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( str ( entry . sourceValue . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) ) if entry . sourceValue != None else "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( str ( entry . value . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) ) if entry . value != None else "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + entry . label . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( str ( entry . ref . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) ) if entry . ref != None else "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + str ( entry . nodata ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if colorMap . legend == None : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" + colorMap . legend . type . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" + colorMap . legend . minLabel . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" + colorMap . legend . maxLabel . encode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> for entry in colorMap . legend . legendentries : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + rgb_to_hex ( entry . rgb ) + "<STR_LIT:>>" + <EOL> "<STR_LIT>" + ( "<STR_LIT>" if is_bright ( entry . rgb ) else "<STR_LIT>" ) + "<STR_LIT>" + <EOL> entry . rgb + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( "<STR_LIT:True>" if entry . showTick else "<STR_LIT:False>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( "<STR_LIT:True>" if entry . showTick else "<STR_LIT:False>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + entry . label . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + ( str ( entry . id . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) ) if entry . id != None else "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if not colorMap == colorMaps . colormaps [ - <NUM_LIT:1> ] : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> def main ( argv ) : <EOL> colormapFile = "<STR_LIT>" <EOL> try : <EOL> opts , args = getopt . getopt ( argv , "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> except getopt . GetoptError : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> elif opt in ( "<STR_LIT:-c>" , "<STR_LIT>" ) : <EOL> colormapFile = arg <EOL> parseColorMaps ( colormapFile , colormapFile ) <EOL> generateHTML ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
