Unicode safe "convertTmplPathToModuleName"

Gerold Penz <[email protected]> Wed, 07 Apr 2010 14:47:08 +0200
Newsgroups gmane.comp.python.cheetah
Organization Waldhart Software
Message-ID <[email protected]>
Hi!

After an upgrade from *Cheetah 2.4.0* to *Cheetah 2.4.2.1*, I had a 
problem with the module "convertTmplPathToModuleName".

Traceback BEGIN
=====================================================================
[07/Apr/2010:10:05:14] HTTP
Request Headers:
   'REFERER': 'https://amjet.skischoolshop.com/de/rent/'
   'KEEP-ALIVE': '115'
   'ACCEPT-CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
   'USER-AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; 
rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)'
   'CONNECTION': 'keep-alive'
   'COOKIE': 'L=de; beaker.session.id=2a91fc79d912c5e1618c93a5434139b6; 
from=2010-04-14; to=2010-04-20'
   'Remote-Addr': '87.247.196.133'
   'X-REQUESTED-WITH': 'XMLHttpRequest'
   'HOST': 'amjet.skischoolshop.com'
   'ACCEPT': 'text/html, */*'
   'ACCEPT-LANGUAGE': 'de-at,de-de;q=0.8,de;q=0.6,en-us;q=0.4,en;q=0.2'
   'ACCEPT-ENCODING': 'gzip,deflate'

'Traceback (most recent call last):
   File "/usr/local/lib/python2.6/dist-packages/cherrypy/_cprequest.py", 
line 606, in respond
     cherrypy.response.body = self.handler()
   File 
"/usr/local/lib/python2.6/dist-packages/cherrypy/_cpdispatch.py", line 
25, in __call__
     return self.callable(*self.args, **self.kwargs)
   File 
"/home/amjet_skischoolshop_com/onlineshop/assistant/__init__.py", line 
598, in get_confirm_dialog
     template = CheetahBaseTemplate(file = template_path)
   File "/home/amjet_skischoolshop_com/onlineshop/lib/template.py", line 
109, in __init__
     Cheetah.Template.Template.__init__(self, *args, **kwargs)
   File 
"/usr/local/lib/python2.6/dist-packages/Cheetah-2.4.2.1-py2.6-linux-x86_64.egg/Cheetah/Template.py", 
line 1259, in __init__
     self._compile(source, file, compilerSettings=compilerSettings)
   File 
"/usr/local/lib/python2.6/dist-packages/Cheetah-2.4.2.1-py2.6-linux-x86_64.egg/Cheetah/Template.py", 
line 1553, in _compile
     keepRefToGeneratedCode=True)
   File 
"/usr/local/lib/python2.6/dist-packages/Cheetah-2.4.2.1-py2.6-linux-x86_64.egg/Cheetah/Template.py", 
line 637, in compile
     moduleName = convertTmplPathToModuleName(file)
   File 
"/usr/local/lib/python2.6/dist-packages/Cheetah-2.4.2.1-py2.6-linux-x86_64.egg/Cheetah/convertTmplPathToModuleName.py", 
line 20, in convertTmplPathToModuleName
     return splitdrive(tmplPath)[1].translate(_pathNameTransChars)
TypeError: character mapping must return integer, None or unicode
'
------------------------------------------------------------
params: {'template_dirname': u'rent'}
path_info: '/assistant/get_confirm_dialog'
=====================================================================
Traceback END

I commited a unicode-string as *file*-parameter onto 
´´Cheetah.Template``. This was no problem with *Cheetah 2.4.0*.

In my opinion, Cherrypy should be unicode-safe. So maybe you can do 
something with my suggestion for a new *convertTmplPathToModuleName*-module.


CODE BEGIN
=====================================================================
#!/usr/bin/env python
# coding: utf-8

import os.path
import string


def _get_transchars():
     try:
         chars = string.ascii_letters + string.digits
     except AttributeError:
         chars = string.letters + string.digits
     letterlist = ['_'] * 256
     for char in chars:
         letterlist[ord(char)] = char
     return ''.join(letterlist)

_transchars = _get_transchars()
_transchars_unicode = unicode(_transchars)


def convertTmplPathToModuleName(
     tmplPath, _pathNameTransChars=None, splitdrive=os.path.splitdrive
):
     if _pathNameTransChars is None:
         if isinstance(tmplPath, unicode):
             _pathNameTransChars = _transchars_unicode
         else:
             _pathNameTransChars = _transchars
     return splitdrive(tmplPath)[1].translate(_pathNameTransChars)
=====================================================================
CODE END


Kind regards,
Gerold Penz
:-)


-- 
Waldhart Software
A-6405 Pfaffenhofen, Unterdorf 145
Tel: +43 5262 64574, Fax: +43 5262 64514

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Cheetahtemplate-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss