def
getsvgcolors(src_file='http://www.w3.org/TR/2003/CR-css3-color-20030514/',
                 table_start='<table class="x11colortable">',
                 init_dict=None):
    """Get the names and RGB values for SVG colors.  
    
    You'll probably have to fetch the document and save it locally first.
    The default is just the URL of the current recommendation/draft.  Note
    that this portion of the spec has been quite stable for years; the
changes
    have been in extensions that we do not support.  -- Jim Jewett
    """
    Color = None
    RGB   = None
    l = " "
    final_dict = init_dict or {}
    
    f = open(src_file)
    
    # skip to the right table
    while not l.startswith(table_start):
        l = f.readline().strip()
    
    # read the table
    while not l.startswith('</table>'):
        l = f.readline().strip()
        
        # new table row; ensure we aren't half-through
        if l.startswith('<tr>'):
            if Color or RGB:
                print "Error New Row:  Color %s RGB %s" % (Color, RGB)
        
        # unclassed td is the colorname
        elif l.startswith('<td>'):
            if Color or RGB:
                print "Error New Color:  Color %s RGB %s" % (Color, RGB)
            Color = l[4:].strip().lower()
        
        elif l.startswith('<td class'):
            if l[-7] == '#':
                if RGB:
                    print "Error New RGB:  Color %s RGB %s" % (Color, RGB)
                if not Color:
                    print "Error missed Color:  Color %s RGB %s" % (Color,
RGB)
                RGB = l[-6:].upper()
                final_dict[Color] = RGB
                Color = None
                RGB = None
             # not a colordef line, pass
        # a whitespace line, pass
    
    f.close()
    return final_dict

# I saved the spec as C:\colorsrc.txt, so I wouldn't have to worry about
proxies in this.
def write_as_constant(src_file=r"C:\colorsrc.txt", 
                      dict_name="Colornames_SVG",
                      indent=(" "*8),
                      init_indent=""
                      ):
    """Write a string that could fill the dictionary.
    
    Note that __repr__ isn't enough because we want it in alphabetical
order.
    """
    cn = getsvgcolors(src_file=r"C:\colorsrc.txt")
    cnk = cn.keys()
    cnk.sort()
    
    print "%s%s = {" % (init_indent, dict_name)
    
    name_len = 1 + max ([len (entry) for entry in cnk])
    item_indent = init_indent + indent
    
    for key in cnk:
        name_indent = " "*(name_len - len(key))
        print "%s'%s':%s'%s'," % (item_indent, key, name_indent, cn[key])
    
    print "%s}" % init_indent
_______________________________________________
plucker-dev mailing list
[EMAIL PROTECTED]
http://lists.rubberchicken.org/mailman/listinfo/plucker-dev

Reply via email to