CVS: tmda-cgi Unicode.py,NONE,1.1 CgiUtil.py,1.17,1.18 PendList.py,1.14,1.15 Template.py,1.7,1.8 tmda-cgi.py,1.33,1.34

Gre7g Luterman <[email protected]>
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda-cgi
In directory sc8-pr-cvs1:/tmp/cvs-serv31587

Modified Files:
	CgiUtil.py PendList.py Template.py tmda-cgi.py 
Added Files:
	Unicode.py 
Log Message:
Moved Unicode translator to its own module.

Added my own translator for iso-8859-1.  The built-in one can't handle fancy 
quotes.


--- NEW FILE ---
#!/usr/bin/env python
#
# Copyright (C) 2002 Gre7g Luterman <[email protected]>
#
# This file is part of TMDA.
#
# TMDA is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  A copy of this license should
# be included in the file COPYING.
#
# TMDA is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with TMDA; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"Unicode utilities for tmda-cgi."

import codecs
import re

import Template

# Handy values
AltChar  = re.compile("[\x80-\xFF]")
UTF8     = codecs.getencoder("utf-8")

def Xlate(Chr):
  if ord(Chr) >= 160: return unichr(ord(Chr))
  if Chr == "€": return u"\u20AC"
  if Chr == "‚": return u"\u201A"
  if Chr == "ƒ": return u"\u2061"
  if Chr == "„": return u"\u201E"
  if Chr == "…": return u"\u2026"
  if Chr == "†": return u"\u2020"
  if Chr == "‡": return u"\u2021"
  if Chr == "‰": return u"\u2030"
  if Chr == "‹": return u"\u2039"
  if Chr == "‘": return u"\u2018"
  if Chr == "’": return u"\u2019"
  if Chr == "“": return u"\u201C"
  if Chr == "”": return u"\u201D"
  if Chr == "•": return u"\u2022"
  if Chr == "–": return u"\u2014"
  if Chr == "—": return u"\u2015"
  if Chr == "™": return u"\u2122"
  return u"\u007F"

def Iso8859(Str):
  RetVal = u""
  while 1:
    Match = AltChar.search(Str)
    if Match:
      RetVal += Str[:Match.start()] + Xlate(Match.group(0))
      Str = Str[Match.end():]
    else:
      break
  RetVal += Str
  return (RetVal,)

def TranslateToUTF8(CharSet, Str, Errors):
  "Represent a string in UTF-8."
  import email.Charset

  if not CharSet:
    return Str
  CS = email.Charset.Charset(CharSet)
  CharSet = CS.input_charset

  # Find appropriate decoder
  if CharSet == "iso-8859-1":
    Decoder = Iso8859
  else:
    try:
      Decoder = codecs.getdecoder(CharSet)
    except LookupError:
      try:
        # Is it GB2312?
        if CharSet == "gb2312":
          import chinese.gb2312
          Lib = chinese.gb2312
        # Is it GBK?
        elif CharSet == "gbk":
          import chinese.gbk
          Lib = chinese.gbk
        # Is it Big5?
        elif CharSet == "big5":
          import chinese.big5
          Lib = chinese.big5
        # Don't recognize it.  Was it our fallback?
        elif CharSet == PVars[("General", "CSEncoding")]:
          # It was our fallback!  Give up now!
          return Str
        # Mark it and use the fallback
        else:
          return "(%s) %s" % (CharSet,
            TranslateToUTF8(PVars[("General", "CSEncoding")], Str, Errors))
        Decoder = Lib.Codec().decode
      except ImportError:
        # We know what it was, but we don't have the library installed.
        return "(%s) %s" % (CharSet, Str)

  # Decode string to Unicode
  try:
    Uni = Decoder(Str, errors = Errors)[0]
  except TypeError:
    Uni = Decoder(Str)[0]

  # Encode for UTF-8
  return UTF8(Uni)[0]

Index: CgiUtil.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/CgiUtil.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- CgiUtil.py	13 May 2003 16:12:36 -0000	1.17
+++ CgiUtil.py	13 May 2003 21:29:08 -0000	1.18
@@ -39,7 +39,6 @@
 UnquotedString = re.compile(r"^(\S+)\s*")
 HomeDirSearch  = re.compile("^~/")
 HTMLTagSearch  = re.compile("</?([^\s>]*).*?>", re.S)
-UTF8           = codecs.getencoder("utf-8")
 
 # CGI exception classes
 class NotInstalled(Errors.TMDAError):
@@ -159,54 +158,6 @@
   P = os.popen(Command, "w")
   P.write(MsgObj.msgobj.as_string(1))
   P.close()
-
-def TranslateToUTF8(CharSet, Str, Errors):
-  "Represent a string in UTF-8."
-  import email.Charset
-
-  if not CharSet:
-    return Str
-  CS = email.Charset.Charset(CharSet)
-  CharSet = CS.input_charset
-
-  # Find appropriate decoder
-  try:
-    Decoder = codecs.getdecoder(CharSet)
-  except LookupError:
-    try:
-      # Is it GB2312?
-      if CharSet == "gb2312":
-        import chinese.gb2312
-        Lib = chinese.gb2312
-      # Is it GBK?
-      elif CharSet == "gbk":
-        import chinese.gbk
-        Lib = chinese.gbk
-      # Is it Big5?
-      elif CharSet == "big5":
-        import chinese.big5
-        Lib = chinese.big5
-      # Don't recognize it.  Was it our fallback?
-      elif CharSet == PVars[("General", "CSEncoding")]:
-        # It was our fallback!  Give up now!
-        return Str
-      # Mark it and use the fallback
-      else:
-        return "(%s) %s" % (CharSet,
-          TranslateToUTF8(PVars[("General", "CSEncoding")], Str, Errors))
-      Decoder = Lib.Codec().decode
-    except ImportError:
-      # We know what it was, but we don't have the library installed.
-      return "(%s) %s" % (CharSet, Str)
-
-  # Decode string to Unicode
-  try:
-    Uni = Decoder(Str, errors = Errors)[0]
-  except TypeError:
-    Uni = Decoder(Str)[0]
-
-  # Encode for UTF-8
-  return UTF8(Uni)[0]
 
 def FindCharSet(MsgObj):
   "Find the character set in an e-mail."

Index: PendList.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/PendList.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- PendList.py	13 May 2003 16:12:36 -0000	1.14
+++ PendList.py	13 May 2003 21:29:08 -0000	1.15
@@ -29,6 +29,7 @@
 import time
 import CgiUtil
 import Template
+import Unicode
 from TMDA import Defaults
 from TMDA import Errors
 from TMDA import Pending
@@ -229,11 +230,11 @@
         for decoded in email.Header.decode_header( MsgObj.msgobj["subject"] ):
           if decoded[1]:
             try:
-              value += CgiUtil.TranslateToUTF8(decoded[1], decoded[0], "strict")
+              value += Unicode.TranslateToUTF8(decoded[1], decoded[0], "strict")
             except UnicodeError:
-              value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+              value += Unicode.TranslateToUTF8(CharSet, decoded[0], "ignore")
           else:
-            value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+            value += Unicode.TranslateToUTF8(CharSet, decoded[0], "ignore")
         Subject = value
         if len(Subject) > int(PVars[("PendingList", "CropSubject")]):
           Subject = \
@@ -252,11 +253,11 @@
         for decoded in email.Header.decode_header( MsgObj.msgobj["from"] ):
           if decoded[1]:
             try:
-              value += CgiUtil.TranslateToUTF8(decoded[1], decoded[0], "strict")
+              value += Unicode.TranslateToUTF8(decoded[1], decoded[0], "strict")
             except UnicodeError:
-              value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+              value += Unicode.TranslateToUTF8(CharSet, decoded[0], "ignore")
           else:
-            value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+            value += Unicode.TranslateToUTF8(CharSet, decoded[0], "ignore")
         From = value
         Temp = Address.search(From)
         if Temp:

Index: Template.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/Template.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Template.py	13 May 2003 16:12:36 -0000	1.7
+++ Template.py	13 May 2003 21:29:08 -0000	1.8
@@ -117,6 +117,9 @@
 
 def Debug(Str):
   "Output a debugging string."
+
+  global SentHeaders
+
   if not SentHeaders: print "Content-Type: text/html\n\n"
   SentHeaders = 1
   print Str

Index: tmda-cgi.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/tmda-cgi.py,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- tmda-cgi.py	13 May 2003 16:37:05 -0000	1.33
+++ tmda-cgi.py	13 May 2003 21:29:08 -0000	1.34
@@ -42,9 +42,10 @@
   "pythonlib"))
 from TMDA import Errors
 
-import CharSetAlias
 import CgiUtil
+import CharSetAlias
 import Session
+import Unicode
 def Call(Library, Str = None):
   "Launch a library with the appropriate globals."
   Library.Form  = Form
@@ -98,6 +99,7 @@
 
 # Share "globals"
 CgiUtil.PVars = PVars
+Unicode.PVars = PVars
 
 # First visit to any page?
 if not Form.keys():

_______________________________________
tmda-cvs mailing list
http://tmda.net/lists/listinfo/tmda-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.