示例#1
0
    def _property_facet ( self, getter, setter, validate, metadata ):
        """ Returns a properly constructed 'property' facet.
        """
        n = 0
        if validate is not None:
            n = _arg_count( validate )

        facet = CFacet( 4 )
        facet.property(
            getter,   _arg_count( getter ),
            setter,   _arg_count( setter ),
            validate, n
        )

        facet.value_allowed(  True )
        facet.value_property( True )
        facet.__dict__ = metadata

        return facet
示例#2
0
def Property ( fget = None, fset = None, fvalidate = None, force = False,
               handler = None, facet = None, **metadata ):
    """ Returns a facet whose value is a Python property.

        Parameters
        ----------
        fget : function
            The "getter" function for the property
        fset : function
            The "setter" function for the property
        fvalidate : function
            The validation function for the property
        force : Boolean
            Indicates whether to use only the function definitions spedficied by
            **fget** and **fset**, and not look elsewhere on the class.
        handler : function
            A facet handler function for the facet
        facet : a facet definition or value that can be converted to a facet
            A facet definition that constrains the values of the property facet

        Description
        -----------
        If no getter or setter functions are specified (and **force** is not
        True), it is assumed that they are defined elsewhere on the class whose
        attribute this facet is assigned to. For example::

            class Bar(HasFacets):
                foo = Property(Float)
                # Shadow facet attribute
                _foo = Float

                def _set_foo(self,x):
                    self._foo = x

                def _get_foo(self):
                    return self._foo

        You can use the **depends_on** metadata attribute to indicate that the
        property depends on the value of another facet. The value of
        **depends_on** is an extended name specifier for facets that the
        property depends on. The property will a facet change notification if
        any of the facets specified by **depends_on** change. For example::

            class Wheel ( Part ):
                axle     = Instanced( Axle )
                position = Property( depends_on = 'axle.chassis.position' )

        For details of the extended facet name syntax, refer to the
        on_facet_set() method of the HasFacets class.
    """
    metadata[ 'type' ] = 'property'

    # If no parameters specified, must be a forward reference (if not forced):
    if (not force) and (fset is None):
        sum = ((fget      is not None) +
               (fvalidate is not None) +
               (facet     is not None))
        if sum <= 1:
            if sum == 0:
                return ForwardProperty( metadata )

            handler = None
            if fget is not None:
                facet = fget

            if facet is not None:
                facet = facet_cast( facet )
                if facet is not None:
                    fvalidate = handler = facet.handler
                    if fvalidate is not None:
                        fvalidate = handler.validate

            if (fvalidate is not None) or (facet is not None):
                if 'editor' not in metadata:
                    if (facet is not None) and (facet.editor is not None):
                        metadata[ 'editor' ] = facet.editor

                return ForwardProperty( metadata, fvalidate, handler )

    if fget is None:
        metadata[ 'transient' ] = True
        if fset is None:
            fget = _undefined_get
            fset = _undefined_set
        else:
            fget = _write_only

    elif fset is None:
        fset = _read_only
        metadata[ 'transient' ] = True

    if facet is not None:
        facet   = facet_cast( facet )
        handler = facet.handler
        if (fvalidate is None) and (handler is not None):
            fvalidate = handler.validate

        if ('editor' not in metadata) and (facet.editor is not None):
            metadata[ 'editor' ] = facet.editor

    metadata.setdefault( 'depends_on', getattr( fget, 'depends_on', None ) )
    if ((metadata.get( 'depends_on' ) is not None) and
         getattr( fget, 'cached_property', False )):
        metadata.setdefault( 'cached', True )

    n     = 0
    facet = CFacet( 4 )
    facet.__dict__ = metadata.copy()
    if fvalidate is not None:
        n = _arg_count( fvalidate )

    facet.property( fget,      _arg_count( fget ),
                    fset,      _arg_count( fset ),
                    fvalidate, n )
    facet.handler = handler

    return facet