def __init__(self, name=None, template=None, default=None, **params): """Widget initialization. All initialization has to take place in this method. It's not thread-safe to mutate widget's attributes outside this method or anytime after widget's first display. *Must* call super(MyWidget, self).__init__(*args, **kw) cooperatively, unless, of course, your know what you're doing. Preferably this should be done before any actual work is done in the method. Parameters: name : The widget's name. In input widgets, this will also be the name of the variable that the form will send to the controller. This is the only param that is safe to pass as a positional argument to __init__. template : The template that the widget should use to display itself. Currently only Kid templates are supported. You can both initialize with a template string or with the path to a file-base template: "myapp.templates.widget_tmpl" default : Default value to display when no value is passed at display time. **params : Keyword arguments specific to your widget or to any of it's bases. If listed at class attribute 'params' the will be bound automatically to the widget instance. Note: Do not confuse these parameters with parameters listed at "params". Some widgets accept parameters at the constructor which are not listed params, these parameter won't be passed to the template, be automatically called, etc.. """ self._declaration_counter = counter.next() if name: self.name = name if template: self.template_c, self.template = load_kid_template(template) if default is not None: self.default = default # logic for managing the params attribute for param in self.__class__.params: if param in params: # make sure we don't keep references to mutables setattr(self, param, copy_if_mutable(params.pop(param))) else: # make sure we don't alter mutable class attributes value, mutable = copy_if_mutable( getattr(self.__class__, param), True) if mutable: # re-set it only if mutable setattr(self, param, value) for unused in params.iterkeys(): warnings.warn('keyword argument "%s" is unused at %r instance' % ( unused, self.__class__.__name__))
# so their order is preserved. counter = itertools.count() # Load all widgets provided by the widget entry point def load_widgets(): for widget_mod in pkg_resources.iter_entry_points("turbogears.widgets"): try: widget_mod.load() except Exception, e: raise ImportError, 'Error loading plugin "%s"\n%s: %s' % ( widget_mod, e.__class__.__name__, e) all_widgets = set() PlainHTML = load_kid_template(""" <html xmlns:py="http://purl.org/kid/ns#" py:replace="elements"/> """, modname='turbogears.widgets.plainhtml')[0] ############################################################################# # Widgets base classes # ############################################################################# class Widget(object): """A TurboGears Widget. '__init__' and 'update_params' are the only methods you might need to care extending. Attributes you should know about:
def __init__(self, name=None, template=None, default=None, **params): """Widget initialization. All initialization has to take place in this method. It's not thread-safe to mutate widget's attributes outside this method or anytime after widget's first display. *Must* call super(MyWidget, self).__init__(*args, **kw) cooperatively, unless, of course, your know what you're doing. Preferably this should be done before any actual work is done in the method. Parameters: name : The widget's name. In input widgets, this will also be the name of the variable that the form will send to the controller. This is the only param that is safe to pass as a positional argument to __init__. template : The template that the widget should use to display itself. Currently only Kid templates are supported. You can both initialize with a template string or with the path to a file-base template: "myapp.templates.widget_tmpl" default : Default value to display when no value is passed at display time. **params : Keyword arguments specific to your widget or to any of it's bases. If listed at class attribute 'params' the will be bound automatically to the widget instance. Note: Do not confuse these parameters with parameters listed at "params". Some widgets accept parameters at the constructor which are not listed params, these parameter won't be passed to the template, be automatically called, etc.. """ self._declaration_counter = counter.next() if name: self.name = name if template: self.template_c, self.template = load_kid_template(template) if default is not None: self.default = default # logic for managing the params attribute for param in self.__class__.params: if param in params: # make sure we don't keep references to mutables setattr(self, param, copy_if_mutable(params.pop(param))) else: if hasattr(self, param): # make sure we don't alter mutable class attributes value = copy_if_mutable(getattr(self.__class__, param), True) if value[1]: # re-set it only if mutable setattr(self, param, value[0]) else: setattr(self, param, None) for unused in params.iterkeys(): warnings.warn('keyword argument "%s" is unused at %r instance' % (unused, self.__class__.__name__))
# Load all widgets provided by the widget entry point def load_widgets(): for widget_mod in pkg_resources.iter_entry_points("turbogears.widgets"): try: widget_mod.load() except Exception, e: raise ImportError, 'Error loading plugin "%s"\n%s: %s' % ( widget_mod, e.__class__.__name__, e) all_widgets = set() PlainHTML = load_kid_template(""" <html xmlns:py="http://purl.org/kid/ns#" py:replace="elements"/> """, modname='turbogears.widgets.plainhtml')[0] ############################################################################# # Widgets base classes # ############################################################################# class Widget(object): """A TurboGears Widget. '__init__' and 'update_params' are the only methods you might need to care extending. Attributes you should know about: