Changeset 4535
- Timestamp:
- 04/28/08 17:35:03 (4 months ago)
- Files:
-
- projects/ToscaWidgets/trunk/docs/source/api.rst (modified) (3 diffs)
- projects/ToscaWidgets/trunk/docs/source/conf.py (modified) (1 diff)
- projects/ToscaWidgets/trunk/tests/__init__.py (modified) (1 diff)
- projects/ToscaWidgets/trunk/tw/core/base.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/ToscaWidgets/trunk/docs/source/api.rst
r4530 r4535 4 4 ========== 5 5 6 All ToscaWidgets live inside the :mod:`tw` namespace, including widget eggs 7 such as `tw.forms`_, etc. [1]_ 6 Public symbols from the ToscaWidgets distribution should be imported from 7 :mod:`tw.api`. 8 9 All ToscaWidgets widget eggs, such as `tw.forms`_, etc. graft their namespaces 10 in 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 8 16 9 17 Widget … … 18 26 :members: 19 27 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 59 WidgetsList 60 ----------- 61 62 This object is just syntax sugar to have a DSL-like way to declare a list 63 of widgets. Although it's subclasses appear to have attributes, there's some 64 metaclass magic behind the scenes which turns the class into a list factory. 65 66 Example: 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 79 Notice how we don't need to pass and ``id`` parameter and the widgets are 80 sorted by ascending declaration time. 81 82 .. autoclass:: tw.api.WidgetsList 83 :members: 84 20 85 21 86 .. rubric:: Footnotes … … 26 91 27 92 28 29 projects/ToscaWidgets/trunk/docs/source/conf.py
r4530 r4535 27 27 # Add any Sphinx extension module names here, as strings. They can be extensions 28 28 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 29 extensions = ['sphinx.ext.autodoc' ]29 extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest'] 30 30 31 31 # Add any paths that contain templates here, relative to this directory. projects/ToscaWidgets/trunk/tests/__init__.py
r4425 r4535 14 14 15 15 DOCTEST_MODULES = [ 16 "tw.core ",16 "tw.core.base", 17 17 "tw.core.meta", 18 18 "tw.core.util", projects/ToscaWidgets/trunk/tw/core/base.py
r4475 r4535 183 183 184 184 #XXX: Some of these properties could be implemented as static attributes 185 186 @property187 185 def id(self): 188 186 return '_'.join(reversed( 189 187 [w.id_path_elem for w in self.path if w.id_path_elem] 190 188 )) 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 193 207 def key(self): 194 208 return '.' + '.'.join(reversed( 195 209 [w.id_path_elem for w in self.path if w.id_path_elem][:-1] 196 210 )) 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 200 230 def path(self): 201 231 yield self … … 203 233 for i in self.parent.path: 204 234 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 207 239 def displays_on(self): 208 240 if self.is_root: … … 210 242 else: 211 243 return self.parent.engine_name 244 displays_on = property(displays_on, doc="""\ 245 Where the widget is being displayed on 246 """) 212 247 213 248 @property … … 216 251 217 252 218 @property219 253 def root(self): 220 254 return list(self.path)[-1] 221 222 @property 255 root = property(root, doc="The root of this widget tree") 256 223 257 def is_root(self): 224 258 return self.parent is None 259 is_root = property(is_root, doc="True if the widget doesn't have a parent") 225 260 226 261 def __new__(cls, id=None, parent=None, children=[], **kw): … … 294 329 @only_if_initialized 295 330 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 297 333 overriding initialization parameters. 298 334 299 Example:300 301 .. sourcecode:: python335 This is the only way to safely "modify" a widget instance. 336 337 Example:: 302 338 303 339 >>> w = Widget('foo', a=2) … … 364 400 365 401 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 """ 366 407 self._collect_resources() 367 408 if len(self._js_calls) > 0: … … 381 422 382 423 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 384 426 applying a filter on them. 385 427 386 .. sourcecode:: python428 Example:: 387 429 388 430 >>> W = Widget … … 414 456 415 457 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 """ 418 462 if self._is_initialized and self.include_dynamic_js_calls: 419 463 log.debug("Adding call <%s> for %r dynamically.", call, self) … … 460 504 461 505 def render(self, value=None, **kw): 506 """ 507 Renders a widget as an unicode string. 508 """ 462 509 kw = self.prepare_dict(value, kw) 463 510 if self.is_root: … … 466 513 467 514 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 """ 468 526 kw = self.prepare_dict(value, kw) 469 527 if self.is_root: