Changeset 3295 for pykota/trunk/pykota

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.

Location:
pykota/trunk/pykota
Files:
1 removed
3 modified

Legend:

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

    r3291 r3295  
    3939    hasJAXML = True 
    4040 
     41from pykota.utils import * 
     42 
    4143from pykota import version 
    42 from pykota.tool import PyKotaTool, N_ 
     44from pykota.tool import PyKotaTool 
    4345from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    4446 
  • pykota/trunk/pykota/tool.py

    r3294 r3295  
    3838from mx import DateTime 
    3939 
     40from pykota import utils 
    4041from pykota.errors import PyKotaCommandLineError 
    4142from pykota import config, storage, logger 
    4243from pykota.version import __version__, __author__, __years__, __gplblurb__ 
    43  
    44 def N_(message) : 
    45     """Fake translation marker for translatable strings extraction.""" 
    46     return message 
    47  
    48 def crashed(message="Bug in PyKota") :     
    49     """Minimal crash method.""" 
    50     import traceback 
    51     lines = [] 
    52     for line in traceback.format_exception(*sys.exc_info()) : 
    53         lines.extend([l for l in line.split("\n") if l]) 
    54     msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 
    55     sys.stderr.write(msg) 
    56     sys.stderr.flush() 
    57     return msg 
    5844 
    5945class Percent : 
     
    10389class Tool : 
    10490    """Base class for tools with no database access.""" 
    105     def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
     91    def __init__(self, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
    10692        """Initializes the command line tool.""" 
    10793        self.debug = True # in case of early failure 
     
    11096        # Saves a copy of the locale settings 
    11197        (self.language, self.charset) = locale.getlocale() 
     98        if not self.charset : 
     99            self.charset = "UTF-8" 
    112100         
    113101        # pykota specific stuff 
     
    247235    def crashed(self, message="Bug in PyKota") :     
    248236        """Outputs a crash message, and optionally sends it to software author.""" 
    249         msg = crashed(message) 
     237        msg = utils.crashed(message) 
    250238        fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ 
    251239                        (msg, \ 
     
    347335class PyKotaTool(Tool) :     
    348336    """Base class for all PyKota command line tools.""" 
    349     def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
    350         """Initializes the command line tool and opens the database.""" 
    351         Tool.__init__(self, lang, charset, doc) 
    352          
    353337    def deferredInit(self) :     
    354338        """Deferred initialization.""" 
  • 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