def __new__(cls, name, bases, dct): # Makes sure we get the union of params and member_widgets # from all our bases. params = setlike(dct.get('params', [])) member_widgets = setlike(dct.get('member_widgets', [])) compound = False for base in bases: params.add_all(getattr(base, 'params', [])) if getattr(base, 'compound', False): member_widgets.add_all(getattr(base, 'member_widgets', [])) compound = True for param in params: # Swap all params listed at 'params' with a ParamDescriptor try: dct[param_prefix + param] = dct[param] dct[param] = ParamDescriptor(param) except KeyError: for base in bases: if hasattr(base, param): break else: # not declared in any superclass, let default be None dct[param_prefix + param] = None dct[param] = ParamDescriptor(param) params = list(params) dct['params'] = params if compound: dct['member_widgets'] = list(member_widgets) # Pick params_doc from all bases giving priority to the widget's own params_doc = {} for base in bases: params_doc.update(getattr(base, 'params_doc', {})) params_doc.update(dct.get('params_doc', {})) dct['params_doc'] = params_doc return super(MetaWidget, cls).__new__(cls, name, bases, dct)
def __new__(cls, name, bases, dct): # update_data has been deprecated if 'update_data' in dct and name != "Widget": warnings.warn( "update_data has been renamed update_params, please " "rename your method. " "Note: this warning will be removed once 1.0 is " "released and your actual code will stop working.", DeprecationWarning, 2) # Makes sure we get the union of params and member_widgets # from all our bases. params = setlike(dct.get('params', [])) # template_vars has been deprecated if 'template_vars' in dct: params.add_all(dct['template_vars']) warnings.warn( "Use of template_vars inside a widget is deprecated, " "use params instead. " "Note: this warning will be removed once 1.0 is " "released and your actual code will stop working.", DeprecationWarning, 2) member_widgets = setlike(dct.get('member_widgets', [])) compound = False for base in bases: params.add_all(getattr(base, 'params', [])) if getattr(base, 'compound', False): member_widgets.add_all(getattr(base, 'member_widgets', [])) compound = True for param in params: # Swap all params listed at 'params' with a ParamDescriptor try: dct[param_prefix + param] = dct[param] dct[param] = ParamDescriptor(param) except KeyError: # declared in a superclass, skip it... pass params = list(params) dct['params'] = params #XXX: Remove when deprecation is effective dct['template_vars'] = params if compound: dct['member_widgets'] = list(member_widgets) # Pick params_doc from all bases giving priority to the widget's own params_doc = {} for base in bases: params_doc.update(getattr(base, 'params_doc', {})) params_doc.update(dct.get('params_doc', {})) dct['params_doc'] = params_doc return super(MetaWidget, cls).__new__(cls, name, bases, dct)
def __new__(cls, name, bases, dct): # update_data has been deprecated if 'update_data' in dct and name != "Widget": warnings.warn( "update_data has been renamed update_params, please " "rename your method. " "Note: this warning will be removed once 1.0 is " "released and your actual code will stop working.", DeprecationWarning, 2) # Makes sure we get the union of params and member_widgets # from all our bases. params = setlike(dct.get('params', [])) # template_vars has been deprecated if 'template_vars' in dct: params.add_all(dct['template_vars']) warnings.warn( "Use of template_vars inside a widget is deprecated, " "use params instead. " "Note: this warning will be removed once 1.0 is " "released and your actual code will stop working.", DeprecationWarning, 2) member_widgets = setlike(dct.get('member_widgets', [])) compound = False for base in bases: params.add_all(getattr(base, 'params', [])) if getattr(base, 'compound', False): member_widgets.add_all(getattr(base, 'member_widgets', [])) compound = True for param in params: # Swap all params listed at 'params' with a ParamDescriptor try: dct[param_prefix+param] = dct[param] dct[param] = ParamDescriptor(param) except KeyError: # declared in a superclass, skip it... pass params = list(params) dct['params'] = params #XXX: Remove when deprecation is effective dct['template_vars'] = params if compound: dct['member_widgets'] = list(member_widgets) # Pick params_doc from all bases giving priority to the widget's own params_doc = {} for base in bases: params_doc.update(getattr(base, 'params_doc', {})) params_doc.update(dct.get('params_doc', {})) dct['params_doc'] = params_doc return super(MetaWidget, cls).__new__(cls, name, bases, dct)
def retrieve_javascript(self): """ Return a setlike instance with all the JSLinks and JSSources the widget needs. """ scripts = setlike() for script in self.javascript: scripts.add(script) return scripts
def retrieve_css(self): """ Return a setlike instance with all the CSSLinks and CSSSources the widget needs. """ css = setlike() for cssitem in self.css: css.add(cssitem) return css
def index(self, name=None): from turbogears import widgets from turbogears.widgets import js_location, Tabber, SyntaxHighlighter all_descs = self.all_descs if not all_descs: widgets.load_widgets() all_descs = dict() for widgetdesc in widgets.all_widgets: wd = widgetdesc() all_descs[wd.full_class_name.replace(".", "_")] = wd self.all_descs = all_descs if name: all_descs = {name: all_descs[name]} desclist = list(all_descs.values()) desclist.sort(cmp=lambda x, y: cmp(x.name, y.name)) output = dict(descs=desclist, viewing_one=name != None) if name: # do not extend desclist! desclist = desclist + [Tabber(), SyntaxHighlighter()] css = setlike() js = dict() for l in js_location: js[l] = setlike() for widgetdesc in desclist: if not name and widgetdesc.show_separately: continue css.add_all(widgetdesc.retrieve_css()) for l in js_location: for script in widgetdesc.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) output["widget_css"] = css for l in js_location: output["widget_js_%s" % str(l)] = js[l] return output
def retrieve_javascript(self): """Return the needed JavaScript ressources. Return a setlike instance containing all the JSLinks and JSSources the widget needs. """ scripts = setlike() for script in self.javascript: scripts.add(script) return scripts
def retrieve_css(self): """Return the needed CSS ressources. Return a setlike instance with all the CSSLinks and CSSSources the widget needs. """ css = setlike() for cssitem in self.css: css.add(cssitem) return css
def retrieve_css(self): """ Retrieve the css for all the member widgets and get an ordered union of them. """ css = setlike() for cssitem in self.css: css.add(cssitem) for widget in self.iter_member_widgets(): for cssitem in widget.retrieve_css(): css.add(cssitem) return css
def _process_output(engine, output, template, format): """Produces final output form from the data returned from a controller method. @param tg_format: format of desired output (html or json) @param output: the output returned by the controller @param template: HTML template to use """ if isinstance(output, dict): from turbogears.widgets import js_location css = tg_util.setlike() js = dict(izip(js_location, iter(tg_util.setlike, None))) include_widgets = {} include_widgets_lst = config.get("tg.include_widgets", []) if config.get("tg.mochikit_all", False): include_widgets_lst.insert(0, 'turbogears.mochikit') for i in include_widgets_lst: widget = tg_util.load_class(i) if isclass(widget): widget = widget() include_widgets["tg_%s" % i.split(".")[-1]] = widget for script in widget.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) css.add_all(widget.retrieve_css()) for value in output.itervalues(): if hasattr(value, "retrieve_css"): retrieve = getattr(value, "retrieve_css") if callable(retrieve): css.add_all(value.retrieve_css()) if hasattr(value, "retrieve_javascript"): retrieve = getattr(value, "retrieve_javascript") if callable(retrieve): for script in value.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) output.update(include_widgets) output["tg_css"] = css #output.update([("tg_js_%s" % str(l), js[l]) for l in js_location]) for l in iter(js_location): output["tg_js_%s" % str(l)] = js[l] output["tg_flash"] = output.get("tg_flash") return engine.render(output, format=format, template=template)
def retrieve_javascript(self): """ Retrieve the javascript for all the member widgets and get an ordered union of them. """ scripts = setlike() for script in self.javascript: scripts.add(script) for widget in self.iter_member_widgets(): for script in widget.retrieve_javascript(): scripts.add(script) return scripts
def retrieve_css(self): """Get CSS for the member widgets. Retrieve the CSS for all the member widgets and get an ordered union of them. """ css = setlike() for cssitem in self.css: css.add(cssitem) for widget in self.iter_member_widgets(): for cssitem in widget.retrieve_css(): css.add(cssitem) return css
def retrieve_javascript(self): """Get JavaScript for the member widgets. Retrieve the JavaScript for all the member widgets and get an ordered union of them. """ scripts = setlike() for script in self.javascript: scripts.add(script) for widget in self.iter_member_widgets(): for script in widget.retrieve_javascript(): scripts.add(script) return scripts
def index(self, name=None): from turbogears import widgets from turbogears.widgets import js_location, Tabber, SyntaxHighlighter all_descs = self.all_descs if not all_descs: widgets.load_widgets() all_descs = dict() for widgetdesc in widgets.all_widgets: wd = widgetdesc() all_descs[wd.full_class_name.replace('.', '_')] = wd self.all_descs = all_descs if name: all_descs = {name: all_descs[name]} desclist = all_descs.values() desclist.sort(cmp=lambda x, y: cmp(x.name.lower(), y.name.lower())) output = dict(descs=desclist, viewing_one=name != None) if name: # do not extend desclist! desclist = desclist + [Tabber(), SyntaxHighlighter()] css = setlike() js = dict() for location in js_location: js[location] = setlike() for widgetdesc in desclist: if not name and widgetdesc.show_separately: continue css.add_all(widgetdesc.retrieve_css()) for script in widgetdesc.retrieve_javascript(): js[getattr(script, 'location', js_location.head)].add(script) output['widget_css'] = css for location in js: output['widget_js_%s' % str(location)] = js[location] return output
def set_with_self(self): theset = setlike() theset.add(self) return theset
def _process_output(output, template, format, content_type, fragment=False, **options): """Produce final output form from data returned from a controller method. See the expose() arguments for more info since they are the same. """ if isinstance(output, dict): # import this here to prevent circular import in widgets.forms from turbogears.widgets import js_location css = tg_util.setlike() js = dict(izip(js_location, iter(tg_util.setlike, None))) include_widgets = {} include_widgets_lst = config.get('tg.include_widgets', []) if config.get('tg.mochikit_all', False): include_widgets_lst.insert(0, 'turbogears.mochikit') for name in include_widgets_lst: widget = tg_util.load_class(name) if widget is None: log.debug("Could not load widget %s", name) continue if isclass(widget): widget = widget() if hasattr(widget, 'retrieve_resources') and hasattr(widget, 'inject'): # it's a ToscaWidget, we register it for injection widget.inject() # XXX: widgets with same base name will override each other include_widgets['tg_%s' % name.rsplit('.', 1)[-1]] = widget output.update(include_widgets) # collect JS/CSS resources from widgets in the output dict or # tg.include_widgets for value in output.itervalues(): if hasattr(value, 'retrieve_resources'): # it's a ToscaWidget, will be injected by the ToscaWidget middleware continue else: try: css_resources = value.retrieve_css() except (AttributeError, TypeError): css_resources = [] try: js_resources = value.retrieve_javascript() except (AttributeError, TypeError): js_resources = [] css.add_all(css_resources) for script in js_resources: location = getattr(script, 'location', js_location.head) js[location].add(script) css.sort(key=lambda obj: getattr(obj, 'order', 0)) output['tg_css'] = css for location in iter(js_location): js[location].sort(key=lambda obj: getattr(obj, 'order', 0)) output['tg_js_%s' % location] = js[location] tg_flash = _get_flash() if tg_flash: output['tg_flash'] = tg_flash headers = {'Content-Type': content_type} output = view.render(output, template=template, format=format, headers=headers, fragment=fragment, **options) content_type = headers['Content-Type'] if content_type: response.headers['Content-Type'] = content_type else: content_type = response.headers.get('Content-Type', 'text/plain') if content_type.startswith('text/'): if isinstance(output, unicode): output = output.encode(tg_util.get_template_encoding_default()) return output
def retrieve_css(self): return setlike()
def retrieve_javascript(self): return setlike()
def _process_output(output, template, format, content_type, mapping, fragment=False): """Produces final output form from the data returned from a controller method. @param tg_format: format of desired output (html or json) @param output: the output returned by the controller @param template: HTML template to use """ if isinstance(output, dict): from turbogears.widgets import js_location css = tg_util.setlike() js = dict(izip(js_location, iter(tg_util.setlike, None))) include_widgets = {} include_widgets_lst = config.get("tg.include_widgets", []) if config.get("tg.mochikit_all", False): include_widgets_lst.insert(0, 'turbogears.mochikit') for i in include_widgets_lst: widget = tg_util.load_class(i) if isclass(widget): widget = widget() include_widgets["tg_%s" % i.split(".")[-1]] = widget for script in widget.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) css.add_all(widget.retrieve_css()) for value in output.itervalues(): if hasattr(value, "retrieve_css"): retrieve = getattr(value, "retrieve_css") if callable(retrieve): css.add_all(value.retrieve_css()) if hasattr(value, "retrieve_javascript"): retrieve = getattr(value, "retrieve_javascript") if callable(retrieve): for script in value.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) output.update(include_widgets) output["tg_css"] = css #output.update([("tg_js_%s" % str(l), js[l]) for l in js_location]) for l in iter(js_location): output["tg_js_%s" % str(l)] = js[l] tg_flash = _get_flash() if not tg_flash == None: output["tg_flash"] = tg_flash output = view.render(output, template=template, format=format, mapping=mapping, content_type=content_type, fragment=fragment) else: if content_type: cherrypy.response.headers["Content-Type"] = content_type # fix the Safari XMLHttpRequest encoding problem try: contentType = cherrypy.response.headers["Content-Type"] ua = cherrypy.request.headers["User-Agent"] except KeyError: return output if not contentType.startswith("text/"): return output ua = view.UserAgent(ua) enc = tg_util.get_template_encoding_default() if ua.browser == "safari": if isinstance(output, str): output = output.decode(enc) output = unicodechars.sub( lambda m: "&#x%x;" % ord(m.group(1)), output).encode("ascii") if isinstance(output, unicode): output = output.encode(enc) return output
def _process_output(output, template, format, content_type, mapping, fragment=False): """Produce final output form from data returned from a controller method. See the expose() arguments for more info since they are the same. """ if isinstance(output, dict): from turbogears.widgets import js_location css = tg_util.setlike() js = dict(izip(js_location, iter(tg_util.setlike, None))) include_widgets = {} include_widgets_lst = config.get("tg.include_widgets", []) if config.get("tg.mochikit_all", False): include_widgets_lst.insert(0, 'turbogears.mochikit') for i in include_widgets_lst: widget = tg_util.load_class(i) if isclass(widget): widget = widget() include_widgets["tg_%s" % i.split(".")[-1]] = widget for script in widget.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) css.add_all(widget.retrieve_css()) for value in output.itervalues(): if hasattr(value, "retrieve_css"): retrieve = getattr(value, "retrieve_css") if callable(retrieve): css.add_all(value.retrieve_css()) if hasattr(value, "retrieve_javascript"): retrieve = getattr(value, "retrieve_javascript") if callable(retrieve): for script in value.retrieve_javascript(): if hasattr(script, "location"): js[script.location].add(script) else: js[js_location.head].add(script) output.update(include_widgets) output["tg_css"] = css for location in iter(js_location): output["tg_js_%s" % str(location)] = js[location] tg_flash = _get_flash() if tg_flash: output["tg_flash"] = tg_flash headers = {'Content-Type': content_type} output = view.render(output, template=template, format=format, mapping=mapping, headers=headers, fragment=fragment) content_type = headers['Content-Type'] if content_type: response.headers["Content-Type"] = content_type else: content_type = response.headers.get("Content-Type", 'text/plain') if content_type.startswith("text/"): if isinstance(output, unicode): output = output.encode(tg_util.get_template_encoding_default()) return output