==========
Interfaces
==========

.. contents::

Interfaces are objects that specify (document) the external behavior
of objects that "provide" them.  An interface specifies behavior
through: 

- Informal documentation in a doc string

- Attribute definitions

- Invariants, which are conditions that must hold for objects that
  provide the interface

Attribute definitions specify specific attributes. They define the
attribute name and provide documentation and constraints of attribute
values.  Attribute definitions can take a number of forms, as we'll
see below.

Defining interfaces
===================

Interfaces are defined using Python class statements:

  >>> import z3locales.interface
  >>> class IFoo(z3locales.interface.Interface):
  ...    """Foo blah blah"""
  ...
  ...    x = z3locales.interface.Attribute("""X blah blah""")
  ...
  ...    def bar(q, r=None):
  ...        """bar blah blah"""

In the example above, we've created an interface, `IFoo`.  We
subclassed `z3locales.interface.Interface`, which is an ancestor interface for
all interfaces, much as `object` is an ancestor of all new-style
classes [#create]_.   The interface is not a class, it's an Interface,
an instance of `InterfaceClass`::

  >>> type(IFoo)
  <class 'z3locales.interface.interface.InterfaceClass'>
  
We can ask for the interfaces documentation::

  >>> IFoo.__doc__
  'Foo blah blah'

and it's name::

  >>> IFoo.__name__
  'IFoo'

and even it's module::

  >>> IFoo.__module__
  '__main__'

The interface defined two attributes:

`x`
  This is the simplest form of attribute definition.  It has a name
  and a doc string.  It doesn't formally specify anything else.

`bar`
  This is a method.  A method is defined via a function definition.  A
  method is simply an attribute constrained to be a callable with a
  particular signature, as provided by the function definition.

  Note that `bar` doesn't take a `self` argument.  Interfaces document
  how an object is *used*.  When calling instance methods, you don't
  pass a `self` argument, so a `self` argument isn't included in the
  interface signature.  The `self` argument in instance methods is
  really an implementation detail of Python instances. Other objects,
  besides instances can provide interfaces and their methods might not
  be instance methods. For example, modules can provide interfaces and
  their methods are usually just functions.  Even instances can have
  methods that are not instance methods.

You can access the attributes defined by an interface using mapping
syntax::

  >>> x = IFoo['x']
  >>> type(x)
  <class 'z3locales.interface.interface.Attribute'>
  >>> x.__name__
  'x'
  >>> x.__doc__
  'X blah blah'

You can use `in` to determine if an interface defines a name::

  >>> 'x' in IFoo
  True

You can iterate over interfaces to get the names they define::

  >>> names = list(IFoo)
  >>> names.sort()
  >>> names
  ['bar', 'x']

Remember that interfaces aren't classes. You can't access attribute
definitions as attributes of interfaces::

  >>> IFoo.x
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  AttributeError: 'InterfaceClass' object has no attribute 'x'

Methods provide access to the method signature::

  >>> bar = IFoo['bar']
  >>> bar.getSignatureString()
  '(q, r=None)'

XXX
  Methods really should have a better API.  This is something that
  needs to be improved.

Declaring interfaces
====================

Having defined interfaces, we can *declare* that objects provide
them.  Before we describe the details, lets define some some terms:

*provide*
   We say that objects *provide* interfaces.  If an object provides an
   interface, then the interface specifies the behavior of the
   object. In other words, interfaces specify the behavior of the
   objects that provide them.

*implement*
   We normally say that classes *implement* interfaces.  If a class
   implements an interface, then the instances of the class provide
   the interface.  Objects provide interfaces that their classes
   implement [#factory]_.  (Objects can provide interfaces directly,
   in addition to what their classes implement.)

   It is important to note that classes don't usually provide the
   interfaces that the implement.

Now that we've defined these terms, we can talk about the API for
declaring interfaces.

The most common way to declare interfaces is using the implements
function in a class statement::

  >>> class Foo:
  ...     z3locales.interface.implements(IFoo)
  ...
  ...     def __init__(self, x=None):
  ...         self.x = x
  ...     
  ...     def bar(self, q, r=None):
  ...         return q, r, self.x
  ...
  ...     def __repr__(self):
  ...         return "Foo(%s)" % self.x


In this example, we declared that `Foo` implements `IFoo`. This means
that instances of `Foo` provide `IFoo`.  Having made this declaration,
there are several ways we can introspect the declarations.  First, we
can ask an interface whether it is implemented by a class::

  >>> IFoo.implementedBy(Foo)
  True
  
And we can ask whether an interface is provided by an object::

  >>> foo = Foo()
  >>> IFoo.providedBy(foo)
  True

Of course, `Foo` doesn't provide `IFoo`, it implements it::

  >>> IFoo.providedBy(Foo)
  False

We can also ask what interfaces are implemented by an object::

  >>> list(z3locales.interface.implementedBy(Foo))
  [<InterfaceClass __main__.IFoo>]

It's an error to ask for interfaces implemented by a non-class::

  >>> IFoo.implementedBy(foo)
  Traceback (most recent call last):
  ...
  TypeError: ('ImplementedBy called for non-type', Foo(None))

  >>> list(z3locales.interface.implementedBy(foo))
  Traceback (most recent call last):
  ...
  TypeError: ('ImplementedBy called for non-type', Foo(None))

Similarly, we can ask what interfaces are provided by an object::

  >>> list(z3locales.interface.providedBy(foo))
  [<InterfaceClass __main__.IFoo>]
  >>> list(z3locales.interface.providedBy(Foo))
  []

We can declare interfaces directly provided by objects.  Suppose that
we want to document what the `__init__` method of the `Foo` class
does.  It's not *really* part of `IFoo`.  You wouldn't normally call
the `__init__` method on Foo instances.  Rather, the `__init__` method
is part of the `Foo`'s `__call__` method::

  >>> class IFooFactory(z3locales.interface.Interface):
  ...     """Create foos"""
  ...
  ...     def __call__(x=None):
  ...         """Create a foo
  ...         
  ...         The argument provides the initial value for x ...
  ...         """

It's the class that provides this interface, so we declare the
interface on the class::

  >>> z3locales.interface.directlyProvides(Foo, IFooFactory)

And then, we'll see that Foo provides some interfaces::

  >>> list(z3locales.interface.providedBy(Foo))
  [<InterfaceClass __main__.IFooFactory>]
  >>> IFooFactory.providedBy(Foo)
  True

Declaring class interfaces is common enough that there's a special
declaration function for it, `classProvides`, that allows the
declaration from within a class statement::

  >>> class Foo2:
  ...     z3locales.interface.implements(IFoo)
  ...     z3locales.interface.classProvides(IFooFactory)
  ...
  ...     def __init__(self, x=None):
  ...         self.x = x
  ...     
  ...     def bar(self, q, r=None):
  ...         return q, r, self.x
  ...
  ...     def __repr__(self):
  ...         return "Foo(%s)" % self.x

  >>> list(z3locales.interface.providedBy(Foo2))
  [<InterfaceClass __main__.IFooFactory>]
  >>> IFooFactory.providedBy(Foo2)
  True

There's a similar function, `moduleProvides`, that supports interface
declarations from within module definitions.  For example, see the use
of `moduleProvides` call in `z3locales.interface.__init__`, which declares that
the package `z3locales.interface` provides `IInterfaceDeclaration`.

Sometimes, we want to declare interfaces on instances, even though
those instances get interfaces from their classes.  Suppose we create
a new interface, `ISpecial`::

  >>> class ISpecial(z3locales.interface.Interface):
  ...     reason = z3locales.interface.Attribute("Reason why we're special")
  ...     def brag():
  ...         "Brag about being special"

We can make a an existing foo instance special by providing `reason`
and `brag` attributes::

  >>> foo.reason = 'I just am'
  >>> def brag():
  ...      return "I'm special!"
  >>> foo.brag = brag
  >>> foo.reason
  'I just am'
  >>> foo.brag()
  "I'm special!"

and by declaring the interface::

  >>> z3locales.interface.directlyProvides(foo, ISpecial)

then the new interface is included in the provided interfaces::

  >>> ISpecial.providedBy(foo)
  True
  >>> list(z3locales.interface.providedBy(foo))
  [<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]

We can find out what interfaces are directly provided by an object::

  >>> list(z3locales.interface.directlyProvidedBy(foo))
  [<InterfaceClass __main__.ISpecial>]

  >>> newfoo = Foo()
  >>> list(z3locales.interface.directlyProvidedBy(newfoo))
  []

Inherited declarations
----------------------

Normally, declarations are inherited::

  >>> class SpecialFoo(Foo):
  ...     z3locales.interface.implements(ISpecial)
  ...     reason = 'I just am'
  ...     def brag(self):
  ...         return "I'm special because %s" % self.reason

  >>> list(z3locales.interface.implementedBy(SpecialFoo))
  [<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]

  >>> list(z3locales.interface.providedBy(SpecialFoo()))
  [<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]

Sometimes, you don't want to inherit declarations.  In that case, you
can use `implementsOnly`, instead of `implements`::

  >>> class Special(Foo):
  ...     z3locales.interface.implementsOnly(ISpecial)
  ...     reason = 'I just am'
  ...     def brag(self):
  ...         return "I'm special because %s" % self.reason

  >>> list(z3locales.interface.implementedBy(Special))
  [<InterfaceClass __main__.ISpecial>]

  >>> list(z3locales.interface.providedBy(Special()))
  [<InterfaceClass __main__.ISpecial>]

External declarations
---------------------

Normally, we make implementation declarations as part of a class
definition. Sometimes, we may want to make declarations from outside
the class definition. For example, we might want to declare interfaces
for classes that we didn't write.  The function `classImplements` can
be used for this purpose::

  >>> class C:
  ...     pass

  >>> z3locales.interface.classImplements(C, IFoo)
  >>> list(z3locales.interface.implementedBy(C))
  [<InterfaceClass __main__.IFoo>]

We can use `classImplementsOnly` to exclude inherited interfaces::

  >>> class C(Foo):
  ...     pass

  >>> z3locales.interface.classImplementsOnly(C, ISpecial)
  >>> list(z3locales.interface.implementedBy(C))
  [<InterfaceClass __main__.ISpecial>]



Declaration Objects
-------------------

When we declare interfaces, we create *declaration* objects.  When we
query declarations, declaration objects are returned::

  >>> type(z3locales.interface.implementedBy(Special))
  <class 'z3locales.interface.declarations.Implements'>

Declaration objects and interface objects are similar in many ways. In
fact, they share a common base class.  The important thing to realize
about them is that they can be used where interfaces are expected in
declarations. Here's a silly example::

  >>> class Special2(Foo):
  ...     z3locales.interface.implementsOnly(
  ...          z3locales.interface.implementedBy(Foo),
  ...          ISpecial, 
  ...          )
  ...     reason = 'I just am'
  ...     def brag(self):
  ...         return "I'm special because %s" % self.reason

The declaration here is almost the same as
``z3locales.interface.implements(ISpecial)``, except that the order of
interfaces in the resulting declaration is different::

  >>> list(z3locales.interface.implementedBy(Special2))
  [<InterfaceClass __main__.IFoo>, <InterfaceClass __main__.ISpecial>]


Interface Inheritance
=====================

Interfaces can extend other interfaces. They do this simply by listing
the other interfaces as base interfaces::

  >>> class IBlat(z3locales.interface.Interface):
  ...     """Blat blah blah"""
  ...  
  ...     y = z3locales.interface.Attribute("y blah blah")
  ...     def eek():
  ...         """eek blah blah"""

  >>> IBlat.__bases__
  (<InterfaceClass z3locales.interface.Interface>,)

  >>> class IBaz(IFoo, IBlat):
  ...     """Baz blah"""
  ...     def eek(a=1):
  ...         """eek in baz blah"""
  ... 

  >>> IBaz.__bases__
  (<InterfaceClass __main__.IFoo>, <InterfaceClass __main__.IBlat>)
  
  >>> names = list(IBaz)
  >>> names.sort()
  >>> names
  ['bar', 'eek', 'x', 'y']

Note that `IBaz` overrides eek::

  >>> IBlat['eek'].__doc__
  'eek blah blah'
  >>> IBaz['eek'].__doc__
  'eek in baz blah'

We were careful to override eek in a compatible way.  When an
extending an interface, the extending interface should be compatible
[#compat]_ with the extended interfaces.

We can ask whether one interface extends another::

  >>> IBaz.extends(IFoo)
  True
  >>> IBlat.extends(IFoo)
  False

Note that interfaces don't extend themselves::

  >>> IBaz.extends(IBaz)
  False

Sometimes we wish they did, but we can, instead use `isOrExtends`::

  >>> IBaz.isOrExtends(IBaz)
  True
  >>> IBaz.isOrExtends(IFoo)
  True
  >>> IFoo.isOrExtends(IBaz)
  False

When we iterate over an interface, we get all of the names it defines,
including names defined by base interfaces. Sometimes, we want *just*
the names defined by the interface directly. We bane use the `names`
method for that::

  >>> list(IBaz.names())
  ['eek']


Specifications
--------------

Interfaces and declarations are both special cases of specifications.
What we described above for interface inheritence applies to both
declarations and specifications.  Declarations actually extend the
interfaces that they declare:

  >>> class Baz:
  ...     z3locales.interface.implements(IBaz)

  >>> baz_implements = z3locales.interface.implementedBy(Baz)
  >>> baz_implements.__bases__
  (<InterfaceClass __main__.IBaz>,)

  >>> baz_implements.extends(IFoo)
  True

  >>> baz_implements.isOrExtends(IFoo)
  True
  >>> baz_implements.isOrExtends(baz_implements)
  True

Specifications (interfaces and declarations) provide an `__sro__`
that lists the specification and all of it's ancestors:

  >>> baz_implements.__sro__
  (<implementedBy __main__.Baz>, 
   <InterfaceClass __main__.IBaz>, 
   <InterfaceClass __main__.IFoo>, 
   <InterfaceClass __main__.IBlat>, 
   <InterfaceClass z3locales.interface.Interface>)


Tagged Values
=============

Interfaces and attribute descriptions support an extension mechanism,
borrowed from UML, called "tagged values" that lets us store extra
data::

  >>> IFoo.setTaggedValue('date-modified', '2004-04-01')
  >>> IFoo.setTaggedValue('author', 'Jim Fulton')
  >>> IFoo.getTaggedValue('date-modified')
  '2004-04-01'
  >>> IFoo.queryTaggedValue('date-modified')
  '2004-04-01'
  >>> IFoo.queryTaggedValue('datemodified')
  >>> tags = list(IFoo.getTaggedValueTags())
  >>> tags.sort()
  >>> tags
  ['author', 'date-modified']

Function attributes are converted to tagged values when method
attribute definitions are created::

  >>> class IBazFactory(z3locales.interface.Interface):
  ...     def __call__():
  ...         "create one"
  ...     __call__.return_type = IBaz

  >>> IBazFactory['__call__'].getTaggedValue('return_type')
  <InterfaceClass __main__.IBaz>


Invariants
==========

Interfaces can express conditions that must hold for objects that
provide them. These conditions are expressed using one or more
invariants.  Invariants are callable objects that will be called with
an object that provides an interface. An invariant raises an `Invalid`
exception if the condition doesn't hold.  Here's an example::

  >>> class RangeError(z3locales.interface.Invalid):
  ...     """A range has invalid limits"""
  ...     def __repr__(self):
  ...         return "RangeError(%r)" % self.args

  >>> def range_invariant(ob):
  ...     if ob.max < ob.min:
  ...         raise RangeError(ob)

Given this invariant, we can use it in an interface definition::

  >>> class IRange(z3locales.interface.Interface):
  ...     min = z3locales.interface.Attribute("Lower bound")
  ...     max = z3locales.interface.Attribute("Upper bound")
  ...
  ...     z3locales.interface.invariant(range_invariant)

Interfaces have a method for checking their invariants::

  >>> class Range(object):
  ...     z3locales.interface.implements(IRange)
  ...
  ...     def __init__(self, min, max):
  ...         self.min, self.max = min, max
  ...
  ...     def __repr__(self):
  ...         return "Range(%s, %s)" % (self.min, self.max)

  >>> IRange.validateInvariants(Range(1,2))
  >>> IRange.validateInvariants(Range(1,1))
  >>> IRange.validateInvariants(Range(2,1))
  Traceback (most recent call last):
  ...
  RangeError: Range(2, 1)

If you have multiple invariants, you may not want to stop checking
after the first error.  If you pass a list to `validateInvariants`,
then a single `Invalid` exception will be raised with the list of
exceptions as it's argument::

  >>> errors = []
  >>> IRange.validateInvariants(Range(2,1), errors)
  Traceback (most recent call last):
  ...
  Invalid: [RangeError(Range(2, 1))]

And the list will be filled with the individual exceptions::
  
  >>> errors
  [RangeError(Range(2, 1))]





.. [#create] The main reason we subclass `Interface` is to cause the
             Python class statement to create an interface, rather
             than a class.

             It's possible to create interfaces by calling a special
             interface class directly.  Doing this, it's possible
             (and, on rare occasions, useful) to create interfaces
             that don't descend from `Interface`.  Using this
             technique is beyond the scope of this document.

.. [#factory] Classes are factories.  They can be called to create
              their instances.  We expect that we will eventually
              extend the concept of implementation to other kinds of
              factories, so that we can declare the interfaces
              provided by the objects created.

.. [#compat] The goal is substitutability.  An object that provides an
             extending interface should be substitutable for an object
             that provides the extended interface.  In our example, an
             object that provides IBaz should be usable whereever an
             object that provides IBlat is expected.

             The interface implementation doesn't enforce this. XXX
             but maybe it should do some checks.
