dabo Commit
Revision 4935
Date: 2009-01-17 17:15:43 -0800 (Sat, 17 Jan 2009)
Author: Ed
Trac: http://trac.dabodev.com/changeset/4935
Changed:
U trunk/dabo/lib/DesignerXmlConverter.py
U trunk/dabo/lib/xmltodict.py
U trunk/dabo/ui/uiwx/dEditor.py
Log:
Added lots of changes to better handle unicode within the editor and the Class
Designer. I've adopted the convention that all of the Dabo files will be
written and read using utf-8 encoding, making the handling of any non-ASCII
text much simpler.
Diff:
Modified: trunk/dabo/lib/DesignerXmlConverter.py
===================================================================
--- trunk/dabo/lib/DesignerXmlConverter.py 2009-01-17 02:50:23 UTC (rev
4934)
+++ trunk/dabo/lib/DesignerXmlConverter.py 2009-01-18 01:15:43 UTC (rev
4935)
@@ -62,13 +62,15 @@
self.createClassText(dct)
# Work-around for bug in which a trailing comment line throws
an error
self.classText += "\n"
+ if isinstance(self.classText, unicode):
+ self.classText =
self.classText.encode(self.Application.Encoding)
open(self._classFileName, "w").write(self.classText)
## For debugging. This creates a copy of the generated code
## so that you can help determine any problems.
## egl: removed 2007-02-10. If you want to see the output,
## just uncomment the next line.
-# open("CLASSTEXT.py", "w").write(self.classText)
+ open("CLASSTEXT.py", "w").write(self.classText)
# jfcs added self._codeFileName to below
# egl - created a tmp file for the main class code that we can
use
@@ -97,7 +99,7 @@
else:
parseCode = False
self._srcFile = os.getcwd()
- dct = xtd.xmltodict(xml, addCodeFile=True)
+ dct = xtd.xmltodict(xml, addCodeFile=True, encoding="utf-8")
# Traverse the dct, looking for superclass information
super = xtd.flattenClassDict(dct)
if super:
@@ -203,7 +205,7 @@
if mthd == "importStatements":
self._import += cd + LINESEP
continue
- self.classText += LINESEP + self.indentCode(cd, 1)
+ self.classText = "%s%s%s" % (self.classText, LINESEP,
self.indentCode(cd, 1))
# Add any property definitions
for prop, propDef in propDefs.items():
pdg = propDef["getter"]
@@ -230,6 +232,9 @@
impt = self._import
else:
impt = ""
+ ct = self.classText
+ if isinstance(ct, unicode):
+ self.classText = ct.encode(self.Application.Encoding)
self.classText =
self.classText.replace("|classImportStatements|", impt)
# We're done!
@@ -635,6 +640,8 @@
def indentCode(self, cd, level):
"""Takes code and indents it to the desired level"""
+# if isinstance(cd, str):
+# cd = cd.decode(self.Application.Encoding)
lns = cd.splitlines()
indent = "\t" * level
# Compiled code needs newlines, no matter what platform.
Modified: trunk/dabo/lib/xmltodict.py
===================================================================
--- trunk/dabo/lib/xmltodict.py 2009-01-17 02:50:23 UTC (rev 4934)
+++ trunk/dabo/lib/xmltodict.py 2009-01-18 01:15:43 UTC (rev 4935)
@@ -7,6 +7,7 @@
import os
import string
import locale
+import codecs
from xml.parsers import expat
# If we're in Dabo, get the default encoding.
@@ -35,9 +36,9 @@
eol = os.linesep
-class Xml2Obj:
+class Xml2Obj(object):
"""XML to Object"""
- def __init__(self):
+ def __init__(self, encoding=None):
self.root = None
self.nodeStack = []
self.attsToSkip = []
@@ -51,6 +52,10 @@
self._propDict = None
self._currPropAtt = ""
self._currPropDict = None
+ if encoding is None:
+ self._encoding = dabo.defaultEncoding
+ else:
+ self._encoding = encoding
def StartElement(self, name, attributes):
@@ -154,7 +159,7 @@
def Parse(self, xml):
# Create a SAX parser
- Parser = expat.ParserCreate()
+ Parser = expat.ParserCreate(self._encoding)
# SAX event handlers
Parser.StartElementHandler = self.StartElement
Parser.EndElementHandler = self.EndElement
@@ -168,9 +173,11 @@
return self.Parse(open(filename,"r").read())
-def xmltodict(xml, attsToSkip=[], addCodeFile=False):
+def xmltodict(xml, attsToSkip=[], addCodeFile=False, encoding=None):
"""Given an xml string or file, return a Python dictionary."""
- parser = Xml2Obj()
+ if encoding is None:
+ encoding = dabo.defaultEncoding
+ parser = Xml2Obj(encoding=encoding)
parser.attsToSkip = attsToSkip
if eol in xml and "<?xml" in xml:
isPath = False
@@ -179,8 +186,11 @@
errmsg = ""
if eol not in xml and isPath:
# argument was a file
+ xmlContent = codecs.open(xml, "r", encoding).read()
+ if isinstance(xmlContent, unicode):
+ xmlContent = xmlContent.encode(encoding)
try:
- ret = parser.ParseFromFile(xml)
+ ret = parser.Parse(xmlContent)
except expat.ExpatError, e:
errmsg = _("The XML in '%s' is not well-formed and
cannot be parsed: %s") % (xml, e)
else:
@@ -196,7 +206,8 @@
codePth = "%s-code.py" % os.path.splitext(xml)[0]
if os.path.exists(codePth):
try:
- codeDict =
desUtil.parseCodeFile(open(codePth).read())
+ codeContent = codecs.open(codePth, "r",
encoding).read()
+ codeDict = desUtil.parseCodeFile(codeContent)
ret["importStatements"] =
codeDict.pop("importStatements", "")
desUtil.addCodeToClassDict(ret, codeDict)
except StandardError, e:
Modified: trunk/dabo/ui/uiwx/dEditor.py
===================================================================
--- trunk/dabo/ui/uiwx/dEditor.py 2009-01-17 02:50:23 UTC (rev 4934)
+++ trunk/dabo/ui/uiwx/dEditor.py 2009-01-18 01:15:43 UTC (rev 4935)
@@ -2392,6 +2392,8 @@
def _setValue(self, val):
if self._constructed():
+ if isinstance(val, str):
+ val = val.decode(self.Encoding)
if self.Text != val:
try:
self.Text = val
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]