Changeset 4535

Show
Ignore:
Timestamp:
04/28/08 17:35:03 (4 months ago)
Author:
alberto
Message:

documenting...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/ToscaWidgets/trunk/docs/source/api.rst

    r4530 r4535  
    44========== 
    55 
    6 All ToscaWidgets live inside the :mod:`tw` namespace, including widget eggs 
    7 such as `tw.forms`_, etc. [1]_ 
     6Public symbols from the ToscaWidgets distribution should be imported from 
     7:mod:`tw.api`. 
     8 
     9All ToscaWidgets widget eggs, such as `tw.forms`_, etc. graft their namespaces 
     10in the :mod:`tw` namespace [1]_. For example, to import form widgtes from 
     11:mod:`tw.forms` on would do:: 
     12 
     13    from tw.forms import TextField, PasswordField 
     14 
     15 
    816 
    917Widget 
     
    1826   :members: 
    1927 
     28   .. autoattribute:: tw.api.Widget.id 
     29   .. autoattribute:: tw.api.Widget.is_root 
     30   .. autoattribute:: tw.api.Widget.root 
     31   .. autoattribute:: tw.api.Widget.displays_on 
     32   .. autoattribute:: tw.api.Widget.path 
     33   .. autoattribute:: tw.api.Widget.key 
     34   .. attribute:: params 
     35 
     36      A set with all the params declared in all the widget's bases and itself. 
     37      This set is computed by :class:`tw.core.meta.WidgetType` from the class 
     38      attribute. 
     39 
     40      Example: 
     41 
     42      .. doctest:: 
     43 
     44         >>> from tw.api import Widget 
     45         >>> class SomeWidget(Widget): 
     46         ...    params = ["a", "b"] 
     47         ... 
     48         >>> "a" in SomeWidget.params 
     49         True 
     50          
     51         All params from Widget are present in SomeWidget's set too. 
     52 
     53         >>> "id" in SomeWidget.params 
     54         True 
     55 
     56 
     57 
     58 
     59WidgetsList 
     60----------- 
     61 
     62This object is just syntax sugar to have a DSL-like way to declare a list 
     63of widgets. Although it's subclasses appear to have attributes, there's some 
     64metaclass magic behind the scenes which turns the class into a list factory. 
     65 
     66Example: 
     67 
     68.. doctest:: 
     69     
     70    >>> from tw.api import Widget, WidgetsList 
     71    >>> class SomeWidgets(WidgetsList): 
     72    ...     a = Widget() 
     73    ...     b = Widget() 
     74    ...     c = Widget() 
     75    ... 
     76    >>> [w.id for w in SomeWidgets()] 
     77    ['a', 'b', 'c'] 
     78 
     79Notice how we don't need to pass and ``id`` parameter and the widgets are 
     80sorted by ascending declaration time. 
     81 
     82.. autoclass:: tw.api.WidgetsList 
     83   :members: 
     84 
    2085 
    2186.. rubric:: Footnotes 
     
    2691 
    2792  
    28  
    29  
  • projects/ToscaWidgets/trunk/docs/source/conf.py

    r4530 r4535  
    2727# Add any Sphinx extension module names here, as strings. They can be extensions 
    2828# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 
    29 extensions = ['sphinx.ext.autodoc'
     29extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest'
    3030 
    3131# Add any paths that contain templates here, relative to this directory. 
  • projects/ToscaWidgets/trunk/tests/__init__.py

    r4425 r4535  
    1414 
    1515DOCTEST_MODULES = [ 
    16     "tw.core", 
     16    "tw.core.base", 
    1717    "tw.core.meta", 
    1818    "tw.core.util", 
  • projects/ToscaWidgets/trunk/tw/core/base.py

    r4475 r4535  
    183183 
    184184    #XXX: Some of these properties could be implemented as static attributes 
    185  
    186     @property 
    187185    def id(self): 
    188186        return '_'.join(reversed( 
    189187            [w.id_path_elem for w in self.path if w.id_path_elem] 
    190188            )) or None 
    191  
    192     @property 
     189    id = property(id, doc="""\ 
     190        The calculated id of the widget. This string will provide a unique 
     191        id for each widget in the tree in a format which allows to re-recreate 
     192        the nested structure. 
     193        Example:: 
     194 
     195            >>> A = Widget("A", children=[ 
     196            ...     Widget("B", children=[ 
     197            ...         Widget("C") 
     198            ...         ]) 
     199            ...     ]) 
     200            ... 
     201            >>> C = A.c.B.c.C 
     202            >>> C.id 
     203            'A_B_C' 
     204 
     205        """) 
     206 
    193207    def key(self): 
    194208        return '.' + '.'.join(reversed( 
    195209            [w.id_path_elem for w in self.path if w.id_path_elem][:-1] 
    196210            )) or None 
    197  
    198  
    199     @property 
     211    key = property(key, doc="""\ 
     212        A string that can be used as a key to index the dictionary of 
     213        parameters sent to the root widget so it reaches this widget when 
     214        displaying. 
     215 
     216        Example:: 
     217 
     218            >>> A = Widget("A", children=[ 
     219            ...     Widget("B", children=[ 
     220            ...         Widget("C") 
     221            ...         ]) 
     222            ...     ]) 
     223            ... 
     224            >>> C = A.c.B.c.C 
     225            >>> C.key 
     226            '.B.C' 
     227        """) 
     228 
     229 
    200230    def path(self): 
    201231        yield self 
     
    203233            for i in self.parent.path: 
    204234                yield i 
    205  
    206     @property 
     235    path = property(path, doc="""\ 
     236        Iterates a walk from this widget to the root of the tree 
     237        """) 
     238 
    207239    def displays_on(self): 
    208240        if self.is_root: 
     
    210242        else: 
    211243            return self.parent.engine_name 
     244    displays_on = property(displays_on, doc="""\ 
     245        Where the widget is being displayed on 
     246        """) 
    212247 
    213248    @property 
     
    216251 
    217252 
    218     @property 
    219253    def root(self): 
    220254        return list(self.path)[-1] 
    221  
    222     @property 
     255    root = property(root, doc="The root of this widget tree") 
     256 
    223257    def is_root(self): 
    224258        return self.parent is None 
     259    is_root = property(is_root, doc="True if the widget doesn't have a parent") 
    225260 
    226261    def __new__(cls, id=None, parent=None, children=[], **kw): 
     
    294329    @only_if_initialized 
    295330    def clone(self, *args, **kw): 
    296         """Returns a cloned version of the widget instance, optionally 
     331        """ 
     332        Returns a cloned version of the widget instance, optionally 
    297333        overriding initialization parameters. 
    298334 
    299         Example: 
    300  
    301         .. sourcecode:: python 
     335        This is the only way to safely "modify" a widget instance. 
     336 
     337        Example:: 
    302338 
    303339            >>> w = Widget('foo', a=2) 
     
    364400 
    365401    def post_init(self, *args, **kw): 
     402        """ 
     403        This method is called for all :class:`tw.api.Widget` base classes to 
     404        perform final setup after the widget is initialized but before it is 
     405        locked. 
     406        """ 
    366407        self._collect_resources()  
    367408        if len(self._js_calls) > 0: 
     
    381422 
    382423    def walk(self, filter=None, recur_if_filtered=True): 
    383         """Does a pre-order walk on widget tree rooted at self optionally  
     424        """ 
     425        Does a pre-order walk on widget tree rooted at self optionally  
    384426        applying a filter on them. 
    385427         
    386         .. sourcecode:: python 
     428        Example:: 
    387429     
    388430            >>> W = Widget 
     
    414456 
    415457    def add_call(self, call): 
    416         """Adds a js_function call that will be made when the  widget  
    417         is rendered.""" 
     458        """ 
     459        Adds a :func:`tw.api.js_function` call that will be made when the  
     460        widget is rendered. 
     461        """ 
    418462        if self._is_initialized and self.include_dynamic_js_calls: 
    419463            log.debug("Adding call <%s> for %r dynamically.", call, self) 
     
    460504 
    461505    def render(self, value=None, **kw): 
     506        """ 
     507        Renders a widget as an unicode string. 
     508        """ 
    462509        kw = self.prepare_dict(value, kw) 
    463510        if self.is_root: 
     
    466513 
    467514    def display(self, value=None, **kw): 
     515        """ 
     516        Renders a widget and adapts the output. This method **must** be used 
     517        to display child widgets inside their parent's template so output is 
     518        adapted. 
     519         
     520        Unlike :meth:`tw.api.Widget.render`, :meth:`tw.api.Widget.display` 
     521        returns adapted output compatible with the template the widget is being 
     522        rendered on. For example, this is needed so Genshi doesn't autoescape 
     523        string output from mako and to serialize Genshi output on the other way 
     524        around. 
     525        """ 
    468526        kw = self.prepare_dict(value, kw) 
    469527        if self.is_root: