Changeset 3287

Show
Ignore:
Timestamp:
01/10/08 23:01:47 (16 years ago)
Author:
jerome
Message:

Removed the dependency on python-chardet entirely. Now uses unicode
or UTF-8 strings all over the place : UTF-8 still used for datas
coming from/going to the database (TODO). Conversion to end user's
locale charset is now only done when needed. PyKota Exceptions need
a base class which, for now, will handle the charset translation,
until we get the time to replace internal loggers with Python's
logging module...

Location:
pykota/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pksetup

    r3275 r3287  
    190190                 "python-imaging", 
    191191                 "python-pysnmp4", 
    192                  "python-chardet", 
    193192                 "python-pam" ] 
    194193         
  • pykota/trunk/checkdeps.py

    r3275 r3287  
    8181                       ("Python-PAM", "PAM", "Python-PAM is recommended if you plan to use pknotify+PyKotIcon.\nGrab it from http://www.pangalactic.org/PyPAM/"), 
    8282                       ("Python-pkipplib", "pkipplib", "Python-pkipplib is now mandatory.\nGrab it from http://www.pykota.com/software/pkipplib/"), 
    83                        ("Python-chardet", "chardet", "Python-chardet is recommended.\nGrab it from http://chardet.feedparser.org/"), 
    8483                     ] 
    8584    commandstocheck = [ ("GhostScript", "gs", "Depending on your configuration, GhostScript may be needed in different parts of PyKota."), 
  • pykota/trunk/debian/control

    r3259 r3287  
    1111Architecture: all 
    1212Depends: python, python-egenix-mxdatetime, cupsys,  python-pygresql | python-ldap | python-pysqlite2 | python-mysqldb, jaxml | python-jaxml, python-osd, python-reportlab, python-pysnmp4, python-imaging  
    13 Recommends: qa-assistant, snmp, netatalk, python-psyco, npadmin, python-pam, python-chardet 
     13Recommends: qa-assistant, snmp, netatalk, python-psyco, npadmin, python-pam 
    1414Description: Print Quota/Accounting system for CUPS 
    1515 PyKota is a full featured, internationalized, centralized and extensible  
  • pykota/trunk/pykota/tool.py

    r3286 r3287  
    3939from mx import DateTime 
    4040 
    41 try : 
    42     import chardet 
    43 except ImportError :     
    44     def detectCharset(text) : 
    45         """Fakes a charset detection if the chardet module is not installed.""" 
    46         return "ISO-8859-15" 
    47 else :     
    48     def detectCharset(text) : 
    49         """Uses the chardet module to workaround CUPS lying to us.""" 
    50         return chardet.detect(text)["encoding"] or "UTF-8" 
    51  
    5241from pykota import config, storage, logger 
    5342from pykota.version import __version__, __author__, __years__, __gplblurb__ 
     
    6352        Exception.__init__(self, message) 
    6453    def __repr__(self): 
    65         return self.message 
     54        return self.message.encode(sys.stdout.encoding or "UTF-8", "replace") 
    6655    __str__ = __repr__ 
    6756     
     
    142131            self.charset = self.charset or locale.getpreferredencoding() 
    143132        except locale.Error :     
    144             self.charset = sys.getfilesystemencoding() 
     133            self.charset = sys.stdout.encoding or sys.getfilesystemencoding() 
    145134         
    146135        # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead 
     
    158147            except TypeError : # Python <2.4 
    159148                trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) 
    160             trans.install() 
     149            trans.install(unicode=True) 
    161150        except : 
    162             gettext.NullTranslations().install() 
     151            gettext.NullTranslations().install(unicode=True) 
    163152     
    164153        # pykota specific stuff 
     
    215204        if text is None : 
    216205            return None 
    217         try : 
    218             return text.decode("UTF-8").encode(self.charset, "replace")  
    219         except (UnicodeError, AttributeError) :     
    220             try : 
    221                 # Maybe already in Unicode ? 
    222                 return text.encode(self.charset, "replace")  
    223             except (UnicodeError, AttributeError) : 
    224                 # Try to autodetect the charset 
    225                 return text.decode(detectCharset(text), "replace").encode(self.charset, "replace") 
     206        else :     
     207            return text.decode("UTF-8", "replace").encode(self.charset, "replace")  
    226208         
    227209    def userCharsetToUTF8(self, text) : 
     
    229211        if text is None : 
    230212            return None 
    231         try : 
    232             # We don't necessarily trust the default charset, because 
    233             # xprint sends us titles in UTF-8 but CUPS gives us an ISO-8859-1 charset ! 
    234             # So we first try to see if the text is already in UTF-8 or not, and 
    235             # if it is, we delete characters which can't be converted to the user's charset, 
    236             # then convert back to UTF-8. PostgreSQL 7.3.x used to reject some unicode characters, 
    237             # this is fixed by the ugly line below : 
    238             return text.decode("UTF-8").encode(self.charset, "replace").decode(self.charset).encode("UTF-8", "replace") 
    239         except (UnicodeError, AttributeError) : 
    240             try : 
    241                 return text.decode(self.charset).encode("UTF-8", "replace")  
    242             except (UnicodeError, AttributeError) :     
    243                 try : 
    244                     # Maybe already in Unicode ? 
    245                     return text.encode("UTF-8", "replace")  
    246                 except (UnicodeError, AttributeError) : 
    247                     # Try to autodetect the charset 
    248                     return text.decode(detectCharset(text), "replace").encode("UTF-8", "replace") 
    249         return newtext 
     213        else :     
     214            return text.decode(self.charset, "replace").encode("UTF-8", "replace")     
    250215         
    251216    def display(self, message) : 
    252217        """Display a message but only if stdout is a tty.""" 
    253218        if sys.stdout.isatty() : 
    254             sys.stdout.write(message) 
     219            sys.stdout.write(message.encode(sys.stdout.encoding or "UTF-8", \ 
     220                                            "replace")) 
    255221            sys.stdout.flush() 
    256222             
     
    258224        """Logs something to debug output if debug is enabled.""" 
    259225        if self.debug : 
    260             self.logger.log_message(message, "debug") 
     226            self.logger.log_message(message.encode(sys.stdout.encoding \ 
     227                                                       or "UTF-8", \ 
     228                                                   "replace"), \ 
     229                                    "debug") 
    261230             
    262231    def printInfo(self, message, level="info") :         
    263232        """Sends a message to standard error.""" 
    264         sys.stderr.write("%s: %s\n" % (level.upper(), message)) 
     233        sys.stderr.write("%s: %s\n" % (level.upper(), \ 
     234                                       message.encode(sys.stdout.encoding \ 
     235                                                          or "UTF-8", \ 
     236                                                      "replace"))) 
    265237        sys.stderr.flush() 
    266238         
  • pykota/trunk/qa-assistant/pykota.xml

    r3212 r3287  
    172172      </description> 
    173173    </entry> 
    174     <entry name="python-chardet" display="true"> 
    175       <states> 
    176         <state name="Pass">python-chardet is installed</state> 
    177         <state name="Fail">python-chardet is not installed</state> 
    178         <state name="Non-Blocker">Our system is correctly configured and up-to-date so PyKota won't need this module</state> 
    179       </states> 
    180       <description> 
    181         python-chardet allows PyKota to automatically detect the character set used in a print job's textual information (like its title) 
    182         in the case your system and/or installed release of CUPS doesn't produce UTF-8. 
    183         It can also be useful when dumping incorrectly encoded database contents (because of the problem mentioned 
    184         above). 
    185         You can install it with 'apt-get install python-chardet' 
    186       </description> 
    187     </entry> 
    188174    <entry name="python-pam" display="true"> 
    189175      <states> 
  • pykota/trunk/README

    r3282 r3287  
    154154    - The Python-OSD module to use the graphical print quota reminder. 
    155155      (http://repose.cx/pyosd/) 
    156     - The Python-chardet module to autodetect user's character set when   
    157       printing. (http://chardet.feedparser.org) 
    158156    - SNMP tools (specifically the snmpget command) if you prefer to 
    159       use your own script to request query printers. 
     157      use your own script to request query printers instead of PyKota's 
     158      internal SNMP handling facilities, which is not recommanded... 
    160159      (http://www.net-snmp.org) 
    161160    - Netatalk (specifically the pap command) if you plan to