'Get options of a field as <select>
@param table: the Table
@param fieldname: the Field name
@param parent: the parent element in the tree
@param show_uids: include UUIDs in foreign key options
@param hierarchy: include parent ID in foreign key options (if
the lookup table is hierarchical)'

@classmethod
def get_field_options(cls, table, fieldname, parent=None, show_uids=False, hierarchy=False):

    options = None
    try:
        field = table[fieldname]
    except (KeyError, AttributeError):
        pass
    else:
        requires = field.requires
        if requires:
            if (not isinstance(requires, (list, tuple))):
                requires = [requires]
            requires = requires[0]
            if isinstance(requires, IS_EMPTY_OR):
                requires = requires.other
            try:
                options = requires.options()
            except AttributeError:
                pass
    TAG = cls.TAG
    if options:
        ATTRIBUTE = cls.ATTRIBUTE
        UID = cls.UID
        SubElement = etree.SubElement
        if (parent is not None):
            select = SubElement(parent, TAG.select)
        else:
            select = etree.Element(TAG.select)
        select.set(ATTRIBUTE.name, fieldname)
        select.set(ATTRIBUTE.id, ('%s_%s' % (table._tablename, fieldname)))
        uids = {}
        parents = {}
        if (show_uids or hierarchy):
            ids = set((int(o[0]) for o in options if str(o[0]).isdigit()))
            (ktablename, key) = s3_get_foreign_key(field, m2m=False)[:2]
            try:
                ktable = current.s3db[ktablename]
            except AttributeError:
                pass
            else:
                if show_uids:
                    query = ktable[key].belongs(ids)
                    rows = current.db(query).select(ktable[key], ktable[UID])
                    for row in rows:
                        uids[str(row[key])] = row[UID]
                if hierarchy:
                    from s3hierarchy import S3Hierarchy
                    h = S3Hierarchy(ktablename)
                    if h.config:
                        for _id in ids:
                            parents[str(_id)] = h.parent(_id)
        OPTION = TAG.option
        VALUE = ATTRIBUTE.value
        PARENT = ATTRIBUTE.parent
        for (value, text) in options:
            option = SubElement(select, OPTION)
            option.text = s3_unicode(s3_strip_markup(text))
            attr = option.attrib
            value = s3_unicode(value)
            attr[VALUE] = value
            if (show_uids and (value in uids)):
                uid = uids[value]
                if uid:
                    attr[UID] = uid
            if (hierarchy and (value in parents)):
                p = parents[value]
                if p:
                    attr[PARENT] = str(p)
    elif (parent is not None):
        select = None
    else:
        select = etree.Element(TAG.select)
    return select
