'Generates a SELECT tag, with OPTIONs grouped by OPTGROUPs
@param field: the field needing the widget
@param value: value
@param options: list
@param options: a list of tuples, each either (label, value) or (label, {options})
@param attributes: any other attributes to be applied
@return: SELECT object'

@classmethod
def widget(cls, field, value, options, **attributes):

    default = dict(value=value)
    attr = cls._attributes(field, default, **attributes)
    select_items = []
    for option in options:
        if isinstance(option[1], dict):
            items = [(v, k) for (k, v) in option[1].items()]
            if (not items):
                continue
            items.sort()
            opts = [OPTION(v, _value=k) for (v, k) in items]
            select_items.append(OPTGROUP(_label=option[0], *opts))
        else:
            select_items.append(OPTION(option[1], _label=option[0]))
    return SELECT(select_items, **attr)
