doc/book/en/development/entityclasses/interfaces.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 02 Mar 2010 19:11:46 +0100
branchstable
changeset 4750 875dc33551a9
parent 4437 21f2e01fdd6a
child 4936 a4b772a0d801
permissions -rw-r--r--
[book/entities] a bit of flesh to the interface chapter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
Interfaces
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
     4
Same thing as object-oriented programming interfaces.
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
     5
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     6
Definition of an interface is quite trivial. An example from cubicweb
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     7
itself (found in cubicweb/interfaces.py):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     8
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     9
.. sourcecode:: python
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    10
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    11
    class ITree(Interface):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    12
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    13
        def parent(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    14
            """returns the parent entity"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    15
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    16
        def children(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    17
            """returns the item's children"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    18
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    19
        def children_rql(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    20
            """XXX returns RQL to get children"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    21
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    22
        def iterchildren(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    23
            """iterates over the item's children"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    24
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    25
        def is_leaf(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    26
            """returns true if this node as no child"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    27
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    28
        def is_root(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    29
            """returns true if this node has no parent"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    30
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    31
        def root(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    32
            """returns the root object"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    33
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
Declaration of interfaces implemented by a class
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    37
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    38
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    40
  from cubicweb.interfaces import ITree
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    41
  from cubicweb.mixins import TreeMixIn
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    42
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    43
  class MyEntity(TreeMixIn, AnyEntity):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    44
      __regid__ = 'MyEntity'
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    45
      __implements__ = AnyEntity.__implements__ + ('ITree',)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    47
      tree_attribute = 'filed_under'
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    48
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    49
The TreeMixIn here provides a default implementation for the
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    50
interface. The tree_attribute class attribute is actually used by this
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    51
implementation to help implement correct behaviour.
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    52
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    53
Interfaces (and some implementations as mixins) defined in the library
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    54
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2539
diff changeset
    56
.. automodule:: cubicweb.interface
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2539
diff changeset
    57
   :members:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    58
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    59
.. automodule:: cubicweb.mixins
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    60
   :members:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    61
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    62
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    63