Changeset 5714

Show
Ignore:
Timestamp:
11/18/08 14:42:04 (2 months ago)
Author:
faide
Message:

Import fixes to keep python < 2.5 compatibility. Added support for Genshi translation based on the i18n.run_template_filter config parameter. Thanks to Dag Bralti for this one!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/view/base.py

    r5699 r5714  
    1 """Template processing for TurboGears view templates.""" 
     1"""Template processing for TurboGears view layer. 
     2The engines are configured here. 
     3We also define the functions and variables that will be available in the 
     4template scope. 
     5""" 
    26 
    37import sys 
     
    1014import cherrypy 
    1115import genshi 
     16from genshi.filters import Translator 
    1217import pkg_resources 
    1318 
    1419import turbogears 
    1520from turbogears import identity, config 
    16 from turbogears.i18n import i18n_filter, get_locale 
    17 from turbogears.util import ( 
    18     Bunch, adapt_call, get_template_encoding_default, 
    19     get_mime_type_for_format, mime_type_has_charset) 
     21from turbogears.i18n import i18n_filter, get_locale, gettext 
     22from turbogears.util import Bunch, adapt_call, get_template_encoding_default 
     23from turbogears.util import get_mime_type_for_format, mime_type_has_charset 
    2024 
    2125log = logging.getLogger("turbogears.view") 
     
    8185 
    8286    @param fragment: passed through to tell the template if only a 
    83                      fragment of a page is desired 
     87                     fragment of a page is desired. This is a way to allow 
     88                     xml template engines to generate non valid html/xml 
     89                     because you warn them to not bother about it. 
    8490    @type fragment: bool 
    8591 
     
    125131                if isinstance(content_format, (tuple, list)): 
    126132                    content_format = content_format[0] 
     133 
    127134                if isinstance(content_format, str): 
    128135                    content_format = content_format.split( 
    129136                        )[0].split('-' , 1)[0].lower() 
     137 
    130138                else: 
    131139                    content_format = 'html' 
     140 
    132141            else: 
    133142                content_format = 'html' 
     143 
    134144            content_type = get_mime_type_for_format(content_format) 
     145 
    135146        if mime_type_has_charset( 
    136147                content_type) and '; charset=' not in content_type: 
    137148            charset = get_template_encoding_default(enginename) 
     149 
    138150            if charset: 
    139151                content_type += '; charset=' + charset 
     152 
    140153        headers['Content-Type'] = content_type 
    141154 
     
    285298    if hasattr(element, 'tag'): 
    286299        return genshi.input.ET(element) 
     300 
    287301    elif isinstance(element, list): 
    288302        return chain(*imap(genshi_et, element)) 
     303 
    289304    else: 
    290305        return element 
     
    391406 
    392407    """ 
     408     
     409    def genshi_loader_callback(template): 
     410        """Callback to add a gettext Translator filter to Genshi templates. 
     411        The Translator filter must be first in the list of filters. 
     412 
     413        Defined inside the scope of the load_engines because this is 
     414        not a public function and we don't want to temp people with it. 
     415        """ 
     416        template.filters.insert(0, Translator(gettext)) 
     417     
    393418    get = config.get 
     419 
     420    # Check if the i18n filter is activated in configuration. 
     421    # if so we'll need to apply a translator to the Genshi filter list 
     422    if get("i18n.run_template_filter", False): 
     423        callback = get("genshi.loader_callback", genshi_loader_callback) 
     424        config.update({'genshi.loader_callback': callback}) 
     425     
    394426    engine_options = { 
    395427        "cheetah.importhooks": get("cheetah.importhooks", False), 
     
    421453        "mako.output_encoding": get("mako.output_encoding", "utf-8") 
    422454    } 
     455 
    423456    for entrypoint in pkg_resources.iter_entry_points( 
    424457            "python.templating.engines"): 
    425458        engine = entrypoint.load() 
    426459        engines[entrypoint.name] = engine(stdvars, engine_options) 
     460