Ignore:
Timestamp:
30 May 2011, 01:48:59 (14 years ago)
Author:
uli
Message:

Add docs and make setitem check for cert.code == key.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/certificatecontainer.py

    r6236 r6240  
    1111
    1212class CertificateContainer(grok.Container):
    13     """See interfaces for description.
     13    """A storage for certificates.
     14
     15    A :class:`CertificateContainer` stores
     16    :class:`waeup.sirp.university.Certificate` instances.
     17
     18    It is a :class:`grok.Container` basically acting like a standard
     19    Python dictionary. That means you can add certificates like items
     20    in a normal dictionary and also get certificates by using
     21    :meth:`values`, :meth:`keys`, and :meth:`items`.
     22
     23    This container type is picky about its contents: only real
     24    certificates can be stored here and each new certificate must
     25    provide a unique `code`. See :meth:`addCertificate` for details.
     26
     27    Each :class:`CertificateContainer` provides
     28    :iface:`ICertificateContainer`.
    1429    """
    1530    grok.implements(ICertificateContainer)
     
    1732
    1833    def __setitem__(self, name, certificate):
    19         """XXX: docstring missing.
     34        """Insert `certificate` with `name` as key into container.
     35
     36        The certificate must be an object implementing
     37        :iface:`waeup.sirp.university.interfaces.ICertificate`. If
     38        not, a :exc:`TypeError` is raised.
     39
     40        If the certificate `code` does not equal `name` a
     41        :exc:`ValueError` is raised.
     42
     43        If the `code` attribute of `certificate` is already in use by
     44        another certificate stored in the local site
     45        (:class:`waeup.sirp.app.University` instance), then a
     46        :exc:`waeup.sirp.interfaces.DuplicationError` will be raised.
     47
     48        If `name` is already used as a key, a :exc:`KeyError` will be
     49        raised.
    2050        """
    2151        if not ICertificate.providedBy(certificate):
    2252            raise TypeError('CertificateContainers contain only '
    2353                            'ICertificate instances')
     54
     55        # Only accept certs with code == key.
     56        if certificate.code != name:
     57            raise ValueError('key must match certificate code: '
     58                             '%s, %s' % (name, certificate.code))
     59
    2460        # Lookup catalog. If we find none: no duplicates possible.
    2561        cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
     
    3672
    3773    def addCertificate(self, certificate):
     74        """Add `certificate` to the container.
     75
     76        The certificate must be an object implementing
     77        :iface:`waeup.sirp.university.interfaces.ICertificate`. If
     78        not, a :exc:`TypeError` is raised.
     79
     80        The certificate will be stored in the container with its
     81        `code` attribute as key. If this key is already used for
     82        another certificate stored in the local site
     83        (:class:`waeup.sirp.app.University` instance), then a
     84        :exc:`waeup.sirp.interfaces.DuplicationError` will be raised.
     85        """
    3886        self[getattr(certificate, 'code', None)] = certificate
    3987
Note: See TracChangeset for help on using the changeset viewer.