Back to index

moin  1.9.0~rc2
web.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Helper functions for WWW stuff
00004 
00005     @copyright: 2002 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 def getIntegerInput(request, fieldname, default=None, minval=None, maxval=None):
00010     """ Get an integer value from a request parameter. If the value
00011         is out of bounds, it's made to fit into those bounds.
00012 
00013         Returns `default` in case of errors (not a valid integer, or field
00014         is missing).
00015     """
00016     try:
00017         result = int(request.values[fieldname])
00018     except (KeyError, ValueError):
00019         return default
00020     else:
00021         if minval is not None:
00022             result = max(result, minval)
00023         if maxval is not None:
00024             result = min(result, maxval)
00025         return result
00026 
00027 
00028 def makeSelection(name, values, selectedval=None, size=1, multiple=False, enabled=True):
00029     """ Make a HTML <select> element named `name` from a value list.
00030         The list can either be a list of strings, or a list of
00031         (value, label) tuples.
00032 
00033         `selectedval` is the value that should be pre-selected.
00034     """
00035     from MoinMoin.widget import html
00036     result = html.SELECT(name=name, size="%d" % int(size), multiple=multiple, disabled=not enabled)
00037     for val in values:
00038         if not isinstance(val, type(())):
00039             val = (val, val)
00040         result.append(html.OPTION(
00041             value=val[0], selected=(val[0] == selectedval))
00042             .append(html.Text(val[1]))
00043         )
00044 
00045     return result
00046 
00047 def makeMultiSelection(name, values, selectedvals=None, size=5, enabled=True):
00048     """Make a HTML multiple <select> element with named `name` from a value list.
00049 
00050     The list can either be a list of strings, or a list of (value, label) tuples.
00051     `selectedvals` is a list of values that should be pre-selected.
00052 
00053     """
00054     from MoinMoin.widget import html
00055     result = html.SELECT(name=name, size="%d" % int(size), multiple=True, disabled=not enabled)
00056     for val in values:
00057         if not isinstance(val, type(())):
00058             val = (val, val)
00059         result.append(html.OPTION(
00060             value=val[0], selected=(val[0] in selectedvals))
00061             .append(html.Text(val[1]))
00062         )
00063 
00064     return result
00065 
00066 class Color:
00067     """ RGB-Triple that automatically converts from and to
00068         "#RRGGBB" encoding, and also takes Netscape color names.
00069 
00070         The color values are stored in the attributes `r`, `g` and `b`.
00071 
00072         Example:
00073             >>> from color import Color
00074             >>> Color('yellow')
00075             Color(255, 255, 0)
00076             >>> str(Color('yellow'))
00077             '#FFFF00'
00078             >>> str(Color((128, 0, 128)))
00079             '#800080'
00080             >>> Color('#FF00FF')
00081             Color(255, 0, 255)
00082     """
00083 
00084     COLORS = {
00085         'aliceblue': (0xF0, 0xF8, 0xFF),
00086         'antiquewhite': (0xFA, 0xEB, 0xD7),
00087         'aqua': (0x00, 0xFF, 0xFF),
00088         'aquamarine': (0x7F, 0xFF, 0xD4),
00089         'azure': (0xF0, 0xFF, 0xFF),
00090         'beige': (0xF5, 0xF5, 0xDC),
00091         'bisque': (0xFF, 0xE4, 0xC4),
00092         'black': (0x00, 0x00, 0x00),
00093         'blanchedalmond': (0xFF, 0xEB, 0xCD),
00094         'blue': (0x00, 0x00, 0xFF),
00095         'blueviolet': (0x8A, 0x2B, 0xE2),
00096         'brown': (0xA5, 0x2A, 0x2A),
00097         'burlywood': (0xDE, 0xB8, 0x87),
00098         'cadetblue': (0x5F, 0x9E, 0xA0),
00099         'chartreuse': (0x7F, 0xFF, 0x00),
00100         'chocolate': (0xD2, 0x69, 0x1E),
00101         'coral': (0xFF, 0x7F, 0x50),
00102         'cornflowerblue': (0x64, 0x95, 0xED),
00103         'cornsilk': (0xFF, 0xF8, 0xDC),
00104         'crimson': (0xDC, 0x14, 0x3C),
00105         'cyan': (0x00, 0xFF, 0xFF),
00106         'darkblue': (0x00, 0x00, 0x8B),
00107         'darkcyan': (0x00, 0x8B, 0x8B),
00108         'darkgoldenrod': (0xB8, 0x86, 0x0B),
00109         'darkgray': (0xA9, 0xA9, 0xA9),
00110         'darkgreen': (0x00, 0x64, 0x00),
00111         'darkkhaki': (0xBD, 0xB7, 0x6B),
00112         'darkmagenta': (0x8B, 0x00, 0x8B),
00113         'darkolivegreen': (0x55, 0x6B, 0x2F),
00114         'darkorange': (0xFF, 0x8C, 0x00),
00115         'darkorchid': (0x99, 0x32, 0xCC),
00116         'darkred': (0x8B, 0x00, 0x00),
00117         'darksalmon': (0xE9, 0x96, 0x7A),
00118         'darkseagreen': (0x8F, 0xBC, 0x8F),
00119         'darkslateblue': (0x48, 0x3D, 0x8B),
00120         'darkslategray': (0x2F, 0x4F, 0x4F),
00121         'darkturquoise': (0x00, 0xCE, 0xD1),
00122         'darkviolet': (0x94, 0x00, 0xD3),
00123         'deeppink': (0xFF, 0x14, 0x93),
00124         'deepskyblue': (0x00, 0xBF, 0xFF),
00125         'dimgray': (0x69, 0x69, 0x69),
00126         'dodgerblue': (0x1E, 0x90, 0xFF),
00127         'firebrick': (0xB2, 0x22, 0x22),
00128         'floralwhite': (0xFF, 0xFA, 0xF0),
00129         'forestgreen': (0x22, 0x8B, 0x22),
00130         'fuchsia': (0xFF, 0x00, 0xFF),
00131         'gainsboro': (0xDC, 0xDC, 0xDC),
00132         'ghostwhite': (0xF8, 0xF8, 0xFF),
00133         'gold': (0xFF, 0xD7, 0x00),
00134         'goldenrod': (0xDA, 0xA5, 0x20),
00135         'gray': (0x80, 0x80, 0x80),
00136         'green': (0x00, 0x80, 0x00),
00137         'greenyellow': (0xAD, 0xFF, 0x2F),
00138         'honeydew': (0xF0, 0xFF, 0xF0),
00139         'hotpink': (0xFF, 0x69, 0xB4),
00140         'indianred': (0xCD, 0x5C, 0x5C),
00141         'indigo': (0x4B, 0x00, 0x82),
00142         'ivory': (0xFF, 0xFF, 0xF0),
00143         'khaki': (0xF0, 0xE6, 0x8C),
00144         'lavender': (0xE6, 0xE6, 0xFA),
00145         'lavenderblush': (0xFF, 0xF0, 0xF5),
00146         'lawngreen': (0x7C, 0xFC, 0x00),
00147         'lemonchiffon': (0xFF, 0xFA, 0xCD),
00148         'lightblue': (0xAD, 0xD8, 0xE6),
00149         'lightcoral': (0xF0, 0x80, 0x80),
00150         'lightcyan': (0xE0, 0xFF, 0xFF),
00151         'lightgoldenrodyellow': (0xFA, 0xFA, 0xD2),
00152         'lightgreen': (0x90, 0xEE, 0x90),
00153         'lightgrey': (0xD3, 0xD3, 0xD3),
00154         'lightpink': (0xFF, 0xB6, 0xC1),
00155         'lightsalmon': (0xFF, 0xA0, 0x7A),
00156         'lightseagreen': (0x20, 0xB2, 0xAA),
00157         'lightskyblue': (0x87, 0xCE, 0xFA),
00158         'lightslategray': (0x77, 0x88, 0x99),
00159         'lightsteelblue': (0xB0, 0xC4, 0xDE),
00160         'lightyellow': (0xFF, 0xFF, 0xE0),
00161         'lime': (0x00, 0xFF, 0x00),
00162         'limegreen': (0x32, 0xCD, 0x32),
00163         'linen': (0xFA, 0xF0, 0xE6),
00164         'magenta': (0xFF, 0x00, 0xFF),
00165         'maroon': (0x80, 0x00, 0x00),
00166         'mediumaquamarine': (0x66, 0xCD, 0xAA),
00167         'mediumblue': (0x00, 0x00, 0xCD),
00168         'mediumorchid': (0xBA, 0x55, 0xD3),
00169         'mediumpurple': (0x93, 0x70, 0xDB),
00170         'mediumseagreen': (0x3C, 0xB3, 0x71),
00171         'mediumslateblue': (0x7B, 0x68, 0xEE),
00172         'mediumspringgreen': (0x00, 0xFA, 0x9A),
00173         'mediumturquoise': (0x48, 0xD1, 0xCC),
00174         'mediumvioletred': (0xC7, 0x15, 0x85),
00175         'midnightblue': (0x19, 0x19, 0x70),
00176         'mintcream': (0xF5, 0xFF, 0xFA),
00177         'mistyrose': (0xFF, 0xE4, 0xE1),
00178         'moccasin': (0xFF, 0xE4, 0xB5),
00179         'navajowhite': (0xFF, 0xDE, 0xAD),
00180         'navy': (0x00, 0x00, 0x80),
00181         'oldlace': (0xFD, 0xF5, 0xE6),
00182         'olive': (0x80, 0x80, 0x00),
00183         'olivedrab': (0x6B, 0x8E, 0x23),
00184         'orange': (0xFF, 0xA5, 0x00),
00185         'orangered': (0xFF, 0x45, 0x00),
00186         'orchid': (0xDA, 0x70, 0xD6),
00187         'palegoldenrod': (0xEE, 0xE8, 0xAA),
00188         'palegreen': (0x98, 0xFB, 0x98),
00189         'paleturquoise': (0xAF, 0xEE, 0xEE),
00190         'palevioletred': (0xDB, 0x70, 0x93),
00191         'papayawhip': (0xFF, 0xEF, 0xD5),
00192         'peachpuff': (0xFF, 0xDA, 0xB9),
00193         'peru': (0xCD, 0x85, 0x3F),
00194         'pink': (0xFF, 0xC0, 0xCB),
00195         'plum': (0xDD, 0xA0, 0xDD),
00196         'powderblue': (0xB0, 0xE0, 0xE6),
00197         'purple': (0x80, 0x00, 0x80),
00198         'red': (0xFF, 0x00, 0x00),
00199         'rosybrown': (0xBC, 0x8F, 0x8F),
00200         'royalblue': (0x41, 0x69, 0xE1),
00201         'saddlebrown': (0x8B, 0x45, 0x13),
00202         'salmon': (0xFA, 0x80, 0x72),
00203         'sandybrown': (0xF4, 0xA4, 0x60),
00204         'seagreen': (0x2E, 0x8B, 0x57),
00205         'seashell': (0xFF, 0xF5, 0xEE),
00206         'sienna': (0xA0, 0x52, 0x2D),
00207         'silver': (0xC0, 0xC0, 0xC0),
00208         'skyblue': (0x87, 0xCE, 0xEB),
00209         'slateblue': (0x6A, 0x5A, 0xCD),
00210         'slategray': (0x70, 0x80, 0x90),
00211         'snow': (0xFF, 0xFA, 0xFA),
00212         'springgreen': (0x00, 0xFF, 0x7F),
00213         'steelblue': (0x46, 0x82, 0xB4),
00214         'tan': (0xD2, 0xB4, 0x8C),
00215         'teal': (0x00, 0x80, 0x80),
00216         'thistle': (0xD8, 0xBF, 0xD8),
00217         'tomato': (0xFF, 0x63, 0x47),
00218         'turquoise': (0x40, 0xE0, 0xD0),
00219         'violet': (0xEE, 0x82, 0xEE),
00220         'wheat': (0xF5, 0xDE, 0xB3),
00221         'white': (0xFF, 0xFF, 0xFF),
00222         'whitesmoke': (0xF5, 0xF5, 0xF5),
00223         'yellow': (0xFF, 0xFF, 0x00),
00224         'yellowgreen': (0x9A, 0xCD, 0x32),
00225     }
00226 
00227     def __init__(self, color):
00228         """ Init color value, the 'color' parameter may be
00229             another Color instance, a tuple containing 3 color values,
00230             a Netscape color name or a HTML color ("#RRGGBB").
00231         """
00232         if isinstance(color, tuple) and len(color) == 3:
00233             self.r, self.g, self.b = int(color[0]), int(color[1]), int(color[2])
00234         elif isinstance(color, Color):
00235             self.r, self.g, self.b = color.r, color.g, color.b
00236         elif not isinstance(color, str):
00237             raise TypeError("Color() expects a Color instance, a RGB triple or a color string")
00238         elif color[0] == '#':
00239             color = long(color[1:], 16)
00240             self.r = (color >> 16) & 255
00241             self.g = (color >> 8) & 255
00242             self.b = color & 255
00243         elif color not in self.COLORS:
00244             raise ValueError("Unknown color name '%s'" % color)
00245         else:
00246             # known color name
00247             self.r, self.g, self.b = self.COLORS[color]
00248 
00249     def __str__(self):
00250         return "#%02X%02X%02X" % (self.r, self.g, self.b)
00251 
00252     def __repr__(self):
00253         return "Color(%d, %d, %d)" % (self.r, self.g, self.b)
00254 
00255     def __int__(self):
00256         return self.__long__()
00257 
00258     def __long__(self):
00259         return (self.r << 16) | (self.g << 8) | self.b
00260