CVS: tmda-cgi CharSetAlias.py,NONE,1.1 CgiUtil.py,1.14,1.15 PendList.py,1.12,1.13 Template.py,1.5,1.6 View.py,1.17,1.18 tmda-cgi.py,1.30,1.31
Jim Ramsay <[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-serv6401 Modified Files: CgiUtil.py PendList.py Template.py View.py tmda-cgi.py Added Files: CharSetAlias.py Log Message: Call T.set_charset( "charset" ) instead of T["CharSet"] = "charset" to set the character encoding of a page - it now sets the META tag AND the actual HTTP header printed to the browser (fix for Apache2). Using email.Charset (and CharSetAlias.py) for character set aliasing instead of a home-grown method. --- NEW FILE --- #!/usr/bin/env python # # Copyright (C) 2003 Jim Ramsay <[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 "Sets up characterset aliases for email.Charset" import email.Charset # Aliases to gb2312 for cs in ("csgb2312", "gb_2312-80", "iso-ir-58", "chinese", "csiso58gb231280"): email.Charset.add_alias( cs, "gb2312" ) # Aliases to GBK for cs in ("cp936", "ms936", "windows-936"): email.Charset.add_alias( cs, "gbk" ) # Aliases to Big5 for cs in ("big5-hkscs", "csbig5", "chinesebig5"): email.Charset.add_alias( cs, "big5" ) Index: CgiUtil.py =================================================================== RCS file: /cvsroot/tmda/tmda-cgi/CgiUtil.py,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- CgiUtil.py 6 May 2003 21:50:39 -0000 1.14 +++ CgiUtil.py 12 May 2003 16:59:23 -0000 1.15 @@ -160,29 +160,18 @@ P.write(MsgObj.msgobj.as_string(1)) P.close() -def AliasCharSet(CharSet): - "Recognize a charset's alias." - if CharSet in ("csgb2312", "gb_2312-80", "iso-ir-58", "chinese", - "csiso58gb231280"): - CharSet = "gb2312" - # Is it GBK? - elif CharSet in ("cp936", "ms936", "windows-936"): - CharSet = "gbk" - # Is it Big5? - elif CharSet in ("big5-hkscs", "csbig5", "chinesebig5"): - CharSet = "big5" - return CharSet - def TranslateToUTF8(CharSet, Str, Errors): "Represent a string in UTF-8." + import email.Charset if not CharSet: return Str - CharSet = AliasCharSet(CharSet) + CS = email.Charset.Charset( CharSet ) + RealCharset = CS.input_charset # Find appropriate decoder try: - Decoder = codecs.getdecoder(CharSet) + Decoder = codecs.getdecoder(RealCharset) except LookupError: try: # Is it GB2312? Index: PendList.py =================================================================== RCS file: /cvsroot/tmda/tmda-cgi/PendList.py,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- PendList.py 6 May 2003 21:50:40 -0000 1.12 +++ PendList.py 12 May 2003 16:59:23 -0000 1.13 @@ -93,7 +93,7 @@ # Load the display template T = Template.Template("pending.html") - T["CharSet"] = "utf-8" + T.set_charset( "utf-8" ) # Find the message numbers we'll display FirstMsg = PVars["Pager"] Index: Template.py =================================================================== RCS file: /cvsroot/tmda/tmda-cgi/Template.py,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Template.py 22 Apr 2003 17:50:55 -0000 1.5 +++ Template.py 12 May 2003 16:59:23 -0000 1.6 @@ -109,18 +109,21 @@ # <center>Form may not be submitted.</center> import copy, re +import email.Charset from types import StringType class Template: # Members global across all instantiations: - Dict = {} - BaseDir = "." - VarSearchStr = '<!--\s*var:\s*%s(?:="([^"]+)")?\s*-->' - VarEndSearch = re.compile("<!--\s*/var[^-]*-->", re.I) - LonePctSearch = re.compile("([^%])%([^(%])") - LonePctRepl = r"\1%%\2" - SearchDict = {} - BeenExpanded = 0 + Dict = {} + BaseDir = "." + VarSearchStr = '<!--\s*var:\s*%s(?:="([^"]+)")?\s*-->' + VarEndSearch = re.compile("<!--\s*/var[^-]*-->", re.I) + LonePctSearch = re.compile("([^%])%([^(%])") + LonePctRepl = r"\1%%\2" + SearchDict = {} + BeenExpanded = 0 + FallbackCharset = "utf-8" + charset = None def __init__ \ ( @@ -139,12 +142,30 @@ F.close() self.Items = {} + def set_charset(self, Charset = None ): + "Sets the characterset for this page." + if Charset is None: Charset = self.FallbackCharset + # If this is an alias, find the "real" charset: + chsetObj = email.Charset.Charset( Charset ) + self.charset = chsetObj.input_charset + self["CharSet"] = self.charset + def __setitem__(self, Index, Value): "Assign a substitution variable." if self.Items.has_key(Index): self.Items[Index].HTML = [Value] else: self.Dict[Index] = Value + + def __str__(self): + "Convert to string for printing in browser. (Adds content-type header)" + + if not self.charset: + self.set_charset() + addStr = "; charset=%s" % self.charset + RetVal = "Content-Type: text/html%s\n\n" % addStr + RetVal += self.__repr__() + return RetVal def __repr__(self): "Dump contents of object." Index: View.py =================================================================== RCS file: /cvsroot/tmda/tmda-cgi/View.py,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- View.py 6 May 2003 21:50:40 -0000 1.17 +++ View.py 12 May 2003 16:59:23 -0000 1.18 @@ -226,7 +226,7 @@ for decoded in email.Header.decode_header( Line ): Headers += decoded[0] + " " if decoded[1]: - T["CharSet"] = CgiUtil.AliasCharSet(decoded[1]) + T.set_charset( decoded[1] ) Headers += "\n" T["Headers"] = '<pre class="Headers">%s</pre>' % Headers else: @@ -241,7 +241,7 @@ for decoded in email.Header.decode_header( MsgObj.msgobj[Header] ): value += decoded[0] + " " if decoded[1]: - T["CharSet"] = CgiUtil.AliasCharSet(decoded[1]) + T.set_charset( decoded[1] ) T["Value"] = CgiUtil.Escape(value) HeaderRow.Add() @@ -285,9 +285,6 @@ # text/html - sterilize & display # other - show as an attachment - # Check if there's a character set for this part. - if Part.get_content_charset(): - T["CharSet"] = CgiUtil.AliasCharSet(Part.get_content_charset()) # Display this part if Part.is_multipart(): @@ -313,6 +310,9 @@ Type = Part.get_type("text/plain") # Display the easily display-able parts if Type == "text/plain": + # Check if there's a character set for this part. + if Part.get_content_charset(): + T.set_charset( Part.get_content_charset() ) # Escape & display try: Str = Part.get_payload(decode=1).strip() @@ -324,6 +324,9 @@ pass elif Type == "text/html": # Sterilize & display + # Check if there's a character set for this part. + if Part.get_content_charset(): + T.set_charset( Part.get_content_charset() ) try: T["Content"] = \ CgiUtil.Sterilize(Part.get_payload(decode=1), Allow, Remove) Index: tmda-cgi.py =================================================================== RCS file: /cvsroot/tmda/tmda-cgi/tmda-cgi.py,v retrieving revision 1.30 retrieving revision 1.31 diff -u -r1.30 -r1.31 --- tmda-cgi.py 9 May 2003 05:04:59 -0000 1.30 +++ tmda-cgi.py 12 May 2003 16:59:23 -0000 1.31 @@ -22,6 +22,7 @@ "Web interface to TMDA tools." import MyCgiTb +import CharSetAlias import cgi import os @@ -52,13 +53,12 @@ Session.WebUID = os.getuid() # Prepare the traceback in case of uncaught exception -MyCgiTb.Content() MyCgiTb.ErrTemplate = "prog_err2.html" # Make some global stuff available to all Template.Template.BaseDir = "%s/display/themes/Blue/template" % \ os.path.abspath(os.path.split(sys.argv[0])[0]) -Template.Template.Dict["CharSet"] = "iso-8859-1" +#Template.Template.FallbackCharset = "iso-8859-1" Template.Template.Dict["Script"] = os.environ["SCRIPT_NAME"] Template.Template.Dict["SID"] = "" Template.Template.Dict["DispDir"] = os.environ["TMDA_CGI_DISP_DIR"] _______________________________________ tmda-cvs mailing list http://tmda.net/lists/listinfo/tmda-cvs