[Zope3-dev] Explicitly declare use of utilities

Steve Alexander steve@cat-box.net
Sat, 16 Feb 2002 13:03:05 +0000


Hi Folks,

I'm working on my first Zope3 product.

To start with, I'm working through the Contact example from Docs in cvs.

I like the style of using __implements__ and __used_for__ attributes in 
classes to say what interfaces a component offers, and what interfaces 
it depends on.

However, I would also like an explicit way of saying that a component 
depends on a particular utility.

For example, look at ContactCityState.py:

----
from IPostal import IPostalLookup, IPostalInfo
from IContactInfo import IContactInfo
from Zope.ComponentArchitecture import getUtility

class ContactCityState:
     "Provide access to city and state information for a contact"

     __implements__=IPostalInfo

     __used_for__=IContactInfo

     def __init__(self, contact):
         self._contact=contact
         lookup = getUtility(contact, IPostalLookup)
         info = lookup.lookup(contact.postal_code())
         if info is None:
             self._city, self._state = '', ''
         else:
             self._city, self._state = info.city(), info.state()

     def city(self): return self._city

     def state(self): return self._state
----

If I wanted to draw a UML diagram to show the ContactCityState 
component, I could look at the __implements__ and __used_for__ 
attributes, and draw it like this:

     o  IPostalInfo
     |
     |
   --------------------
   | ContactCityState |
   --------------------
        :
        + - - - ->o IContactInfo


However, I would be missing out the dependency on the IPostalLookup utility.

If there were a declaration like this:

     __uses_utility__=IPostalLookup

I'd be able to have something automatically draw a more complete diagram:


     o  IPostalInfo
     |
     |
   --------------------
   | ContactCityState |
   --------------------
        :   :
        :   :  <<utility>>
        :   + - - - - - ->o IPostalLookup
        :
        + - - - ->o IContactInfo



I imagine that such a declaration would be optional. That is, a 
component would not need to declare that it uses a particular utility.

--
Steve Alexander