Show
Ignore:
Timestamp:
01/21/08 21:00:58 (16 years ago)
Author:
jerome
Message:

Made the CGI scripts work again.
Moved even more functions to the utils module.
Removed the cgifuncs module, moved (and changed) content into utils.
If no output encoding defined, use UTF-8 : when wget is used to try
the CGI scripts, it doesn't set by default the accepted charset and
language headers.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/utils.py

    r3294 r3295  
    2323 
    2424import sys 
     25import os 
    2526import locale 
    2627import gettext 
     
    6364    except : 
    6465        gettext.NullTranslations().install(unicode=True) 
     66         
     67def getpreferredlanguage() : 
     68    """Returns the preferred language.""" 
     69    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "") 
     70    langs = [l.strip().split(';')[0] for l in languages.split(",")] 
     71    return langs[0].replace("-", "_") 
     72     
     73def getpreferredcharset() : 
     74    """Returns the preferred charset.""" 
     75    charsets = os.environ.get("HTTP_ACCEPT_CHARSET", "UTF-8") 
     76    charsets = [l.strip().split(';')[0] for l in charsets.split(",")] 
     77    return charsets[0] 
     78 
     79def reinitcgilocale() :         
     80    """Reinitializes the locale and gettext translations for CGI scripts, according to browser's preferences.""" 
     81    initgettext(*initlocale(getpreferredlanguage(), getpreferredcharset())) 
     82     
     83def N_(message) : 
     84    """Fake translation marker for translatable strings extraction.""" 
     85    return message 
    6586 
    6687def databaseToUnicode(text) : 
     
    80101def logerr(text) : 
    81102    """Logs an unicode text to stderr.""" 
    82     sys.stderr.write(text.encode(sys.stdout.encoding, "replace")) 
     103    sys.stderr.write(text.encode(sys.stdout.encoding or locale.getlocale()[1], \ 
     104                                 "replace")) 
     105    sys.stderr.flush() 
    83106             
     107def crashed(message="Bug in PyKota") :     
     108    """Minimal crash method.""" 
     109    import traceback 
     110    from pykota.version import __version__ 
     111    lines = [] 
     112    for line in traceback.format_exception(*sys.exc_info()) : 
     113        lines.extend([l for l in line.split("\n") if l]) 
     114    msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 
     115    logerr(msg) 
     116    return msg