Show
Ignore:
Timestamp:
01/18/08 23:39:41 (16 years ago)
Author:
jerome
Message:

Added modules to store utility functions and application
intialization code, which has nothing to do in classes.
Modified tool.py accordingly (far from being finished)
Use these new modules where necessary.
Now converts all command line arguments to unicode before
beginning to work. Added a proper logging method for already
encoded query strings.

Files:
1 copied

Legend:

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

    r3275 r3294  
    1919# $Id$ 
    2020# 
     21 
     22"""This module defines some utility functions which make no sense as methods.""" 
     23 
     24import sys 
     25import locale 
     26import gettext 
     27 
     28def initlocale(lang="", cset=None) : 
     29    """Initializes the locale stuff.""" 
     30    try : 
     31        locale.setlocale(locale.LC_ALL, (lang, cset)) 
     32    except (locale.Error, IOError) : 
     33        locale.setlocale(locale.LC_ALL, None) 
     34    (language, charset) = locale.getlocale() 
     35    language = language or "C" 
     36    try : 
     37        charset = charset or locale.getpreferredencoding() 
     38    except locale.Error :     
     39        charset = sys.stdout.encoding or sys.getfilesystemencoding() 
     40 
     41    # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead 
     42    # This has the advantage of allowing transparent support for recent 
     43    # versions of CUPS which (en-)force charset to UTF-8 when printing. 
     44    # This should be needed only when printing, but is probably (?) safe 
     45    # to do when using interactive commands. 
     46    if charset.upper() in ('ASCII', 'ANSI_X3.4-1968') : 
     47        charset = "UTF-8" 
     48    return (language, charset) 
     49 
     50def initgettext(lang, cset) : 
     51    """Initializes gettext translations for PyKota.""" 
     52    try : 
     53        try : 
     54            trans = gettext.translation("pykota", \ 
     55                                        languages=["%s.%s" % (lang,  
     56                                                              cset)],  
     57                                        codeset=cset) 
     58        except TypeError : # Python <2.4 
     59            trans = gettext.translation("pykota",  
     60                                        languages=["%s.%s" % (lang,  
     61                                                              cset)]) 
     62        trans.install(unicode=True) 
     63    except : 
     64        gettext.NullTranslations().install(unicode=True) 
     65 
     66def databaseToUnicode(text) : 
     67    """Converts from database format (UTF-8) to unicode.""" 
     68    if text is not None : 
     69        return text.decode("UTF-8", "replace") 
     70    else :  
     71        return None 
     72     
     73def unicodeToDatabase(text) : 
     74    """Converts from unicode to database format (UTF-8).""" 
     75    if text is not None :  
     76        return text.encode("UTF-8", "replace") 
     77    else :     
     78        return None 
     79             
     80def logerr(text) : 
     81    """Logs an unicode text to stderr.""" 
     82    sys.stderr.write(text.encode(sys.stdout.encoding, "replace")) 
     83