Edgewall Software

Ticket #130 (closed enhancement: fixed)

Opened 7 years ago

Last modified 7 years ago

`callback` was not beying fetched by Genshi's Buffet pugin interface

Reported by: palgarvio Owned by: palgarvio
Priority: major Milestone: 0.4.3
Component: Template engine plugin Version: 0.4.2
Keywords: Cc:

Description

Here a fix for that problem:

  • genshi/template/plugin.py

     
    4848 
    4949        self.default_encoding = options.get('genshi.default_encoding', 'utf-8') 
    5050        auto_reload = options.get('genshi.auto_reload', '1') 
     51        callback = options.get('genshi.callback', None) 
    5152        if isinstance(auto_reload, basestring): 
    5253            auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') 
    5354        search_path = filter(None, options.get('genshi.search_path', '').split(':')) 
     
    6768                                     auto_reload=auto_reload, 
    6869                                     max_cache_size=max_cache_size, 
    6970                                     default_class=self.template_class, 
    70                                      variable_lookup=lookup_errors) 
     71                                     variable_lookup=lookup_errors, 
     72                                     callback=callback) 
    7173 
    7274    def load_template(self, templatename, template_string=None): 
    7375        """Find a template specified in python 'dot' notation, or load one from 

Attachments

loader_callback.diff Download (2.0 KB) - added by cmlenz 7 years ago.
More comprehensive patch

Change History

  Changed 7 years ago by cmlenz

  • priority changed from blocker to major
  • type changed from defect to enhancement

This is a bit weird, I'd normally expect every config option to be represented (or at least representable) as a string in a configuration file.

Is there no other way to set this up via Pylons? Can't you access the plugin object, and through that the associated template loader, and set up the callback that way?

follow-up: ↓ 3   Changed 7 years ago by palgarvio

  • status changed from new to assigned
  • owner changed from cmlenz to palgarvio

First of all why not include that callback option in AbstractTemplateEnginePluginsince you already have default_encoding, auto_reload, max_cache_size and search_path there, which are also options of TemplateLoader?

Seccond, next release of Pylons will accept specifying the templating engine when creating a new project which will make the necessary changes/additions and still keep it pretty simple.

The path you're specifying might be possible(I haven't tested it) but will complicate stuff a lot more.

Currently to use genshi as templating engine all one has to do is:

config.init_app(global_conf, app_conf, package='<app_name>',
                template_engine="genshi")

With the callback option we use the above and on another file:

def template_loaded(template):
    template.filters.insert(0, Translator(ugettext))

 (.....)

tmpl_options['genshi.callback'] = template_loaded

Or in just a single file:

def template_loaded(template):
    template.filters.insert(0, Translator(ugettext))

 (.....)

config.init_app(global_conf, app_conf, package='audioserv')
config.template_engines = []
config.add_template_engine('genshi', 'audioserv.templates',
                           {'genshi.callback': template_loaded})

Still want me to investigate your option?

in reply to: ↑ 2   Changed 7 years ago by anonymous

Replying to palgarvio:

First of all why not include that callback option in AbstractTemplateEnginePluginsince you already have default_encoding, auto_reload, max_cache_size and search_path there, which are also options of TemplateLoader?

As I said, those options are expected to be strings, usually, because they are often read from configuration files. I'm not sure about adding an option that cannot be easily represented as a string, in this case a function that needs context to be useful.

Still want me to investigate your option?

Yeah. If it's possible, I can't imagine it'd be that much more complex.

  Changed 7 years ago by palgarvio

  • status changed from assigned to closed
  • resolution set to worksforme

Ok here's the solution for Pylons, aka, monkey patching:

    config.init_app(global_conf, app_conf, package='audioserv',
                    template_engine="genshi")
    # Monkey patch Genshi's TemplateLoader so we can get i18n working
    from genshi.template.loader import TemplateLoader
    TemplateLoader.callback = template_loaded

  Changed 7 years ago by palgarvio

  • status changed from closed to reopened
  • resolution worksforme deleted

Re-Opening as per cmlenz request.

  Changed 7 years ago by palgarvio

Also consider this one an ugly approach, ie, I instantiate it myself?

    from pylons.templating import available_engines
    genshi = available_engines['genshi']()
    genshi.loader.callback = template_loaded

Pylons seems to pass only the method, which is probably called elsewhere. Look  here.

  Changed 7 years ago by palgarvio

The above Monkey Patch approaches do not solve the problem, sometimes the Translator filter does not get added to the template filters. Seems accepting this patch might be the only way.

Changed 7 years ago by cmlenz

More comprehensive patch

  Changed 7 years ago by cmlenz

  • status changed from reopened to closed
  • resolution set to fixed
  • milestone changed from 0.5 to 0.4.3

Done in [646], ported to 0.4.x in [647].

Add/Change #130 (`callback` was not beying fetched by Genshi's Buffet pugin interface)

Author


E-mail address and user name can be saved in the Preferences.


Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.