~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/util/web.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""
3
3
    MoinMoin - Helper functions for WWW stuff
4
4
 
5
 
    @copyright: 2002 by J�rgen Hermann <jh@web.de>
 
5
    @copyright: 2002 Juergen Hermann <jh@web.de>
6
6
    @license: GNU GPL, see COPYING for details.
7
7
"""
8
8
 
9
 
import re
10
 
from MoinMoin import config
11
 
 
12
9
def getIntegerInput(request, fieldname, default=None, minval=None, maxval=None):
13
10
    """ Get an integer value from a request parameter. If the value
14
11
        is out of bounds, it's made to fit into those bounds.
28
25
        return result
29
26
 
30
27
 
31
 
def getLinkIcon(request, formatter, scheme):
32
 
    """ Get icon for fancy links, or '' if user doesn't want them.
33
 
    """
34
 
    if scheme in ["mailto", "news", "telnet", "ftp", "file"]:
35
 
        icon = scheme
36
 
    else:
37
 
        icon = "www"
38
 
 
39
 
    return request.theme.make_icon(icon)
40
 
 
41
 
def makeSelection(name, values, selectedval=None, size=1):
 
28
def makeSelection(name, values, selectedval=None, size=1, multiple=False):
42
29
    """ Make a HTML <select> element named `name` from a value list.
43
30
        The list can either be a list of strings, or a list of
44
31
        (value, label) tuples.
46
33
        `selectedval` is the value that should be pre-selected.
47
34
    """
48
35
    from MoinMoin.widget import html
49
 
    result = html.SELECT(name=name, size="%d" % int(size))
 
36
    result = html.SELECT(name=name, size="%d" % int(size), multiple=multiple)
50
37
    for val in values:
51
38
        if not isinstance(val, type(())):
52
39
            val = (val, val)
57
44
 
58
45
    return result
59
46
 
 
47
def makeMultiSelection(name, values, selectedvals=None, size=5):
 
48
    """Make a HTML multiple <select> element with named `name` from a value list.
 
49
 
 
50
    The list can either be a list of strings, or a list of (value, label) tuples.
 
51
    `selectedvals` is a list of values that should be pre-selected.
 
52
 
 
53
    """
 
54
    from MoinMoin.widget import html
 
55
    result = html.SELECT(name=name, size="%d" % int(size), multiple=True)
 
56
    for val in values:
 
57
        if not isinstance(val, type(())):
 
58
            val = (val, val)
 
59
        result.append(html.OPTION(
 
60
            value=val[0], selected=(val[0] in selectedvals))
 
61
            .append(html.Text(val[1]))
 
62
        )
 
63
 
 
64
    return result
 
65
 
 
66
class Color:
 
67
    """ RGB-Triple that automatically converts from and to
 
68
        "#RRGGBB" encoding, and also takes Netscape color names.
 
69
 
 
70
        The color values are stored in the attributes `r`, `g` and `b`.
 
71
 
 
72
        Example:
 
73
            >>> from color import Color
 
74
            >>> Color('yellow')
 
75
            Color(255, 255, 0)
 
76
            >>> str(Color('yellow'))
 
77
            '#FFFF00'
 
78
            >>> str(Color((128, 0, 128)))
 
79
            '#800080'
 
80
            >>> Color('#FF00FF')
 
81
            Color(255, 0, 255)
 
82
    """
 
83
 
 
84
    COLORS = {
 
85
        'aliceblue': (0xF0, 0xF8, 0xFF),
 
86
        'antiquewhite': (0xFA, 0xEB, 0xD7),
 
87
        'aqua': (0x00, 0xFF, 0xFF),
 
88
        'aquamarine': (0x7F, 0xFF, 0xD4),
 
89
        'azure': (0xF0, 0xFF, 0xFF),
 
90
        'beige': (0xF5, 0xF5, 0xDC),
 
91
        'bisque': (0xFF, 0xE4, 0xC4),
 
92
        'black': (0x00, 0x00, 0x00),
 
93
        'blanchedalmond': (0xFF, 0xEB, 0xCD),
 
94
        'blue': (0x00, 0x00, 0xFF),
 
95
        'blueviolet': (0x8A, 0x2B, 0xE2),
 
96
        'brown': (0xA5, 0x2A, 0x2A),
 
97
        'burlywood': (0xDE, 0xB8, 0x87),
 
98
        'cadetblue': (0x5F, 0x9E, 0xA0),
 
99
        'chartreuse': (0x7F, 0xFF, 0x00),
 
100
        'chocolate': (0xD2, 0x69, 0x1E),
 
101
        'coral': (0xFF, 0x7F, 0x50),
 
102
        'cornflowerblue': (0x64, 0x95, 0xED),
 
103
        'cornsilk': (0xFF, 0xF8, 0xDC),
 
104
        'crimson': (0xDC, 0x14, 0x3C),
 
105
        'cyan': (0x00, 0xFF, 0xFF),
 
106
        'darkblue': (0x00, 0x00, 0x8B),
 
107
        'darkcyan': (0x00, 0x8B, 0x8B),
 
108
        'darkgoldenrod': (0xB8, 0x86, 0x0B),
 
109
        'darkgray': (0xA9, 0xA9, 0xA9),
 
110
        'darkgreen': (0x00, 0x64, 0x00),
 
111
        'darkkhaki': (0xBD, 0xB7, 0x6B),
 
112
        'darkmagenta': (0x8B, 0x00, 0x8B),
 
113
        'darkolivegreen': (0x55, 0x6B, 0x2F),
 
114
        'darkorange': (0xFF, 0x8C, 0x00),
 
115
        'darkorchid': (0x99, 0x32, 0xCC),
 
116
        'darkred': (0x8B, 0x00, 0x00),
 
117
        'darksalmon': (0xE9, 0x96, 0x7A),
 
118
        'darkseagreen': (0x8F, 0xBC, 0x8F),
 
119
        'darkslateblue': (0x48, 0x3D, 0x8B),
 
120
        'darkslategray': (0x2F, 0x4F, 0x4F),
 
121
        'darkturquoise': (0x00, 0xCE, 0xD1),
 
122
        'darkviolet': (0x94, 0x00, 0xD3),
 
123
        'deeppink': (0xFF, 0x14, 0x93),
 
124
        'deepskyblue': (0x00, 0xBF, 0xFF),
 
125
        'dimgray': (0x69, 0x69, 0x69),
 
126
        'dodgerblue': (0x1E, 0x90, 0xFF),
 
127
        'firebrick': (0xB2, 0x22, 0x22),
 
128
        'floralwhite': (0xFF, 0xFA, 0xF0),
 
129
        'forestgreen': (0x22, 0x8B, 0x22),
 
130
        'fuchsia': (0xFF, 0x00, 0xFF),
 
131
        'gainsboro': (0xDC, 0xDC, 0xDC),
 
132
        'ghostwhite': (0xF8, 0xF8, 0xFF),
 
133
        'gold': (0xFF, 0xD7, 0x00),
 
134
        'goldenrod': (0xDA, 0xA5, 0x20),
 
135
        'gray': (0x80, 0x80, 0x80),
 
136
        'green': (0x00, 0x80, 0x00),
 
137
        'greenyellow': (0xAD, 0xFF, 0x2F),
 
138
        'honeydew': (0xF0, 0xFF, 0xF0),
 
139
        'hotpink': (0xFF, 0x69, 0xB4),
 
140
        'indianred': (0xCD, 0x5C, 0x5C),
 
141
        'indigo': (0x4B, 0x00, 0x82),
 
142
        'ivory': (0xFF, 0xFF, 0xF0),
 
143
        'khaki': (0xF0, 0xE6, 0x8C),
 
144
        'lavender': (0xE6, 0xE6, 0xFA),
 
145
        'lavenderblush': (0xFF, 0xF0, 0xF5),
 
146
        'lawngreen': (0x7C, 0xFC, 0x00),
 
147
        'lemonchiffon': (0xFF, 0xFA, 0xCD),
 
148
        'lightblue': (0xAD, 0xD8, 0xE6),
 
149
        'lightcoral': (0xF0, 0x80, 0x80),
 
150
        'lightcyan': (0xE0, 0xFF, 0xFF),
 
151
        'lightgoldenrodyellow': (0xFA, 0xFA, 0xD2),
 
152
        'lightgreen': (0x90, 0xEE, 0x90),
 
153
        'lightgrey': (0xD3, 0xD3, 0xD3),
 
154
        'lightpink': (0xFF, 0xB6, 0xC1),
 
155
        'lightsalmon': (0xFF, 0xA0, 0x7A),
 
156
        'lightseagreen': (0x20, 0xB2, 0xAA),
 
157
        'lightskyblue': (0x87, 0xCE, 0xFA),
 
158
        'lightslategray': (0x77, 0x88, 0x99),
 
159
        'lightsteelblue': (0xB0, 0xC4, 0xDE),
 
160
        'lightyellow': (0xFF, 0xFF, 0xE0),
 
161
        'lime': (0x00, 0xFF, 0x00),
 
162
        'limegreen': (0x32, 0xCD, 0x32),
 
163
        'linen': (0xFA, 0xF0, 0xE6),
 
164
        'magenta': (0xFF, 0x00, 0xFF),
 
165
        'maroon': (0x80, 0x00, 0x00),
 
166
        'mediumaquamarine': (0x66, 0xCD, 0xAA),
 
167
        'mediumblue': (0x00, 0x00, 0xCD),
 
168
        'mediumorchid': (0xBA, 0x55, 0xD3),
 
169
        'mediumpurple': (0x93, 0x70, 0xDB),
 
170
        'mediumseagreen': (0x3C, 0xB3, 0x71),
 
171
        'mediumslateblue': (0x7B, 0x68, 0xEE),
 
172
        'mediumspringgreen': (0x00, 0xFA, 0x9A),
 
173
        'mediumturquoise': (0x48, 0xD1, 0xCC),
 
174
        'mediumvioletred': (0xC7, 0x15, 0x85),
 
175
        'midnightblue': (0x19, 0x19, 0x70),
 
176
        'mintcream': (0xF5, 0xFF, 0xFA),
 
177
        'mistyrose': (0xFF, 0xE4, 0xE1),
 
178
        'moccasin': (0xFF, 0xE4, 0xB5),
 
179
        'navajowhite': (0xFF, 0xDE, 0xAD),
 
180
        'navy': (0x00, 0x00, 0x80),
 
181
        'oldlace': (0xFD, 0xF5, 0xE6),
 
182
        'olive': (0x80, 0x80, 0x00),
 
183
        'olivedrab': (0x6B, 0x8E, 0x23),
 
184
        'orange': (0xFF, 0xA5, 0x00),
 
185
        'orangered': (0xFF, 0x45, 0x00),
 
186
        'orchid': (0xDA, 0x70, 0xD6),
 
187
        'palegoldenrod': (0xEE, 0xE8, 0xAA),
 
188
        'palegreen': (0x98, 0xFB, 0x98),
 
189
        'paleturquoise': (0xAF, 0xEE, 0xEE),
 
190
        'palevioletred': (0xDB, 0x70, 0x93),
 
191
        'papayawhip': (0xFF, 0xEF, 0xD5),
 
192
        'peachpuff': (0xFF, 0xDA, 0xB9),
 
193
        'peru': (0xCD, 0x85, 0x3F),
 
194
        'pink': (0xFF, 0xC0, 0xCB),
 
195
        'plum': (0xDD, 0xA0, 0xDD),
 
196
        'powderblue': (0xB0, 0xE0, 0xE6),
 
197
        'purple': (0x80, 0x00, 0x80),
 
198
        'red': (0xFF, 0x00, 0x00),
 
199
        'rosybrown': (0xBC, 0x8F, 0x8F),
 
200
        'royalblue': (0x41, 0x69, 0xE1),
 
201
        'saddlebrown': (0x8B, 0x45, 0x13),
 
202
        'salmon': (0xFA, 0x80, 0x72),
 
203
        'sandybrown': (0xF4, 0xA4, 0x60),
 
204
        'seagreen': (0x2E, 0x8B, 0x57),
 
205
        'seashell': (0xFF, 0xF5, 0xEE),
 
206
        'sienna': (0xA0, 0x52, 0x2D),
 
207
        'silver': (0xC0, 0xC0, 0xC0),
 
208
        'skyblue': (0x87, 0xCE, 0xEB),
 
209
        'slateblue': (0x6A, 0x5A, 0xCD),
 
210
        'slategray': (0x70, 0x80, 0x90),
 
211
        'snow': (0xFF, 0xFA, 0xFA),
 
212
        'springgreen': (0x00, 0xFF, 0x7F),
 
213
        'steelblue': (0x46, 0x82, 0xB4),
 
214
        'tan': (0xD2, 0xB4, 0x8C),
 
215
        'teal': (0x00, 0x80, 0x80),
 
216
        'thistle': (0xD8, 0xBF, 0xD8),
 
217
        'tomato': (0xFF, 0x63, 0x47),
 
218
        'turquoise': (0x40, 0xE0, 0xD0),
 
219
        'violet': (0xEE, 0x82, 0xEE),
 
220
        'wheat': (0xF5, 0xDE, 0xB3),
 
221
        'white': (0xFF, 0xFF, 0xFF),
 
222
        'whitesmoke': (0xF5, 0xF5, 0xF5),
 
223
        'yellow': (0xFF, 0xFF, 0x00),
 
224
        'yellowgreen': (0x9A, 0xCD, 0x32),
 
225
    }
 
226
 
 
227
    def __init__(self, color):
 
228
        """ Init color value, the 'color' parameter may be
 
229
            another Color instance, a tuple containing 3 color values,
 
230
            a Netscape color name or a HTML color ("#RRGGBB").
 
231
        """
 
232
        if isinstance(color, tuple) and len(color) == 3:
 
233
            self.r, self.g, self.b = int(color[0]), int(color[1]), int(color[2])
 
234
        elif isinstance(color, Color):
 
235
            self.r, self.g, self.b = color.r, color.g, color.b
 
236
        elif not isinstance(color, str):
 
237
            raise TypeError("Color() expects a Color instance, a RGB triple or a color string")
 
238
        elif color[0] == '#':
 
239
            color = long(color[1:], 16)
 
240
            self.r = (color >> 16) & 255
 
241
            self.g = (color >> 8) & 255
 
242
            self.b = color & 255
 
243
        elif color not in self.COLORS:
 
244
            raise ValueError("Unknown color name '%s'" % color)
 
245
        else:
 
246
            # known color name
 
247
            self.r, self.g, self.b = self.COLORS[color]
 
248
 
 
249
    def __str__(self):
 
250
        return "#%02X%02X%02X" % (self.r, self.g, self.b)
 
251
 
 
252
    def __repr__(self):
 
253
        return "Color(%d, %d, %d)" % (self.r, self.g, self.b)
 
254
 
 
255
    def __int__(self):
 
256
        return self.__long__()
 
257
 
 
258
    def __long__(self):
 
259
        return (self.r << 16) | (self.g << 8) | self.b
 
260