root / pykota / trunk / pykota / utils.py @ 3294

Revision 3294, 3.0 kB (checked in by jerome, 16 years ago)

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.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: UTF-8 -*-
2#
3# PyKota : Print Quotas for CUPS
4#
5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
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           
Note: See TracBrowser for help on using the browser.