Ignore:
Timestamp:
22 Mar 2012, 01:48:58 (13 years ago)
Author:
uli
Message:

Add new helper function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py

    r7933 r7941  
    2828from zope.component.interfaces import IFactory
    2929from zope.interface import implementedBy
     30from zope.interface.interface import Method, Attribute
    3031from zope.schema import getFieldNames
    3132from zope.schema.fieldproperty import FieldProperty
     
    478479    account = authenticator.getAccount(principal_id)
    479480    return account
     481
     482def iface_names(iface, omit=[], exclude_attribs=True, exclude_methods=True):
     483    """Get all attribute names of an interface.
     484
     485    Searches also base interfaces.
     486
     487    Names of fields that are pure attributes
     488    (i.e. zope.interface.Attribute) or methods are excluded by
     489    default.
     490
     491    Names of typical fields derived from zope.schema are included.
     492
     493    The `omit` paramter can give a list of names to exclude.
     494
     495    Returns an unsorted list of strings.
     496    """
     497    ifaces = [iface] + list(iface.getBases())
     498    names = []
     499    for item in ifaces:
     500        names += item.names()
     501    names = [x for x in names if x not in omit]
     502    result = []
     503    for name in names:
     504        cls = iface.get(name).__class__
     505        if exclude_attribs and cls is Attribute:
     506            continue
     507        if exclude_methods and cls is Method:
     508            continue
     509        result.append(name)
     510    return result
Note: See TracChangeset for help on using the changeset viewer.