dabo Commit
Revision 5911
Date: 2010-07-10 06:26:08 -0700 (Sat, 10 Jul 2010)
Author: Ed
Trac: http://trac.dabodev.com/changeset/5911

Changed:
U   trunk/dabo/dPref.py
U   trunk/dabo/db/dBackend.py
U   trunk/dabo/db/dCursorMixin.py
U   trunk/dabo/db/dDataSet.py
U   trunk/dabo/db/dbFirebird.py
U   trunk/dabo/db/dbMsSQL.py
U   trunk/dabo/db/dbMySQL.py
U   trunk/dabo/db/dbOracle.py
U   trunk/dabo/db/dbPostgreSQL.py
U   trunk/dabo/db/dbSQLite.py
U   trunk/dabo/db/dbTemplate.py
U   trunk/dabo/lib/utils.py

Log:
Created the 'ustr()' method in dabo.lib.utils. This is designed to replace all 
of our calls to str() in order to eliminate the unicode encoding errors that 
pop up frequently when non-American developers use Dabo. I'm converting in 
blocks, so there will be more to follow over the next few days until I can 
convert all scripts.

To use, include the line:

from dabo.lib.utils import ustr

in your import statements, and then replace all instances of str(val) with 
ustr(val).


Diff:
Modified: trunk/dabo/dPref.py
===================================================================
--- trunk/dabo/dPref.py 2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/dPref.py 2010-07-10 13:26:08 UTC (rev 5911)
@@ -6,6 +6,7 @@
 import dabo
 from dabo.dLocalize import _
 import dabo.lib.utils as utils
+from dabo.lib.utils import ustr
 
 try:
        from pysqlite2 import dbapi2 as sqlite
@@ -26,6 +27,7 @@
                "_getAttributeNames", "_key", "_noneType", "_parent", 
"_persistAll", "_typeDict", "mro")
 
 
+
 class dPref(object):
        """dPref is a class that is used to automatically manage preferences. 
It requires
        SQLite in order to function; without that installed, you cannot use 
this class. It 
@@ -164,16 +166,12 @@
                """Takes various value types and converts them to a string 
formats that
                can be converted back when needed.
                """
-               if typ in ("str", "unicode"):
-                       ret = val
-               elif typ == "date":
-                       ret = str((val.year, val.month, val.day))
+               if typ == "date":
+                       ret = ustr((val.year, val.month, val.day))
                elif typ == "datetime":
-                       ret = str((val.year, val.month, val.day, val.hour, 
val.minute, val.second, val.microsecond))
-               elif typ == "decimal":
-                       ret = str(val)
+                       ret = ustr((val.year, val.month, val.day, val.hour, 
val.minute, val.second, val.microsecond))
                else:
-                       ret = unicode(val)
+                       ret = ustr(val)
                return ret
                
        

Modified: trunk/dabo/db/dBackend.py
===================================================================
--- trunk/dabo/db/dBackend.py   2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dBackend.py   2010-07-10 13:26:08 UTC (rev 5911)
@@ -4,13 +4,14 @@
 import re
 import datetime
 import threading
+import decimal
 import dabo
 from dabo.dLocalize import _
 import dabo.dException as dException
 from dabo.dObject import dObject
 from dabo.db import dTable
 from dNoEscQuoteStr import dNoEscQuoteStr
-import decimal
+from dabo.lib.utils import ustr
 
 
 
@@ -43,13 +44,6 @@
                self._cursor = None
 
 
-       def _stringify(self, val):
-               """Convert passed val to string; if unicode, leave as-is."""
-               if not isinstance(val, basestring):
-                       val = str(val)
-               return val
-
-
        def isValidModule(self):
                """ Test the dbapi to see if it is supported on this 
computer."""
                try:
@@ -79,9 +73,9 @@
                        # Some databases have specific rules for formatting 
date values.
                        return self.formatDateTime(val)
                elif isinstance(val, (int, long, float)):
-                       return str(val)
+                       return ustr(val)
                elif isinstance(val, decimal.Decimal):
-                       return str(val)
+                       return ustr(val)
                elif isinstance(val, dNoEscQuoteStr):
                        return val
                elif fieldType == "L":

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dCursorMixin.py       2010-07-10 13:26:08 UTC (rev 5911)
@@ -14,6 +14,7 @@
 from dabo.db.dDataSet import dDataSet
 from dabo.lib import dates
 from dabo.lib.utils import noneSortKey, caseInsensitiveSortKey
+from dabo.lib.utils import ustr
 
 
 class dCursorMixin(dObject):
@@ -239,7 +240,7 @@
                                _field_val = field_val
                                if type(field_val) in (float,):
                                        # Can't convert to decimal directly 
from float
-                                       _field_val = str(_field_val)
+                                       _field_val = ustr(_field_val)
                                # Need to convert to the correct scale:
                                scale = None
                                for s in ds:
@@ -338,7 +339,7 @@
                                                
sql.decode(self.Encoding).replace("\n", " "),))
                        # Database errors need to be decoded from database 
encoding.
                        try:
-                               errMsg = str(e).decode(self.Encoding)
+                               errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
                                errMsg = unicode(e)
                        # If this is due to a broken connection, let the user 
know.
@@ -371,7 +372,7 @@
                        _records = dabo.db.dDataSet()
                        # Database errors need to be decoded from database 
encoding.
                        try:
-                               errMsg = str(e).decode(self.Encoding)
+                               errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
                                errMsg = unicode(e)
                        dabo.errorLog.write("Error fetching records: %s" % 
errMsg)
@@ -681,7 +682,7 @@
 
        def getType(self, val):
                try:
-                       ret = re.search("type '([^']+)'", 
str(type(val))).groups()[0]
+                       ret = re.search("type '([^']+)'", 
ustr(type(val))).groups()[0]
                except IndexError:
                        ret = "-unknown-"
                return ret
@@ -958,7 +959,7 @@
                                convTypes = (str, unicode, int, float, long, 
complex)
                                if isinstance(val, convTypes) and 
isinstance(rec[fld], basestring):
                                        if isinstance(fldType, str):
-                                               val = str(val)
+                                               val = ustr(val)
                                        else:
                                                val = unicode(val)
                                elif isinstance(rec[fld], int) and 
isinstance(val, bool):
@@ -981,7 +982,7 @@
                                # string representation of these classes, as 
there is no primitive for either
                                # 'DateTime' or 'Date'.
                                dtStrings = ("<type 'DateTime'>", "<type 
'Date'>", "<type 'datetime.datetime'>")
-                               if str(fldType) in dtStrings and 
isinstance(val, basestring):
+                               if ustr(fldType) in dtStrings and 
isinstance(val, basestring):
                                        ignore = True
                                elif issubclass(fldType, basestring) and 
isinstance(val, basestring):
                                        ignore = True
@@ -1000,7 +1001,7 @@
                                        ignore = (rec.get(keyField, None) in 
self._newRecords)
 
                                if not ignore:
-                                       sft, stv = str(fldType), str(type(val))
+                                       sft, stv = ustr(fldType), 
ustr(type(val))
                                        msg = _("!!! Data Type Mismatch: 
field=%(fld)s. Expecting: %(sft)s; got: %(stv)s") % locals()
                                        dabo.errorLog.write(msg)
 
@@ -1320,14 +1321,14 @@
                                # Error was encountered. Raise an exception so 
that the
                                # calling bizobj can rollback the transaction 
if necessary
                                try:
-                                       errMsg = str(e).decode(self.Encoding)
+                                       errMsg = ustr(e).decode(self.Encoding)
                                except UnicodeError:
                                        errMsg = unicode(e)
                                dabo.dbActivityLog.write(
                                                _("DBQueryException encountered 
in save(): %s") % errMsg)
                                raise e
                        except StandardError, e:
-                               errMsg = str(e)
+                               errMsg = ustr(e)
                                if "connect" in errMsg.lower():
                                        dabo.dbActivityLog.write(
                                                        _("Connection Lost 
exception encountered in saverow(): %s") % errMsg)
@@ -1446,7 +1447,7 @@
                                        elif isinstance(val, (datetime.date, 
datetime.datetime)):
                                                val = self.formatDateTime(val)
                                        else:
-                                               val = str(val)
+                                               val = ustr(val)
                                        wheres.append("%s = %s" % (fld, val))
                                where = " and ".join(wheres)
                                aux.execute("select * from %s where %s" % 
(self.Table, where))
@@ -1760,7 +1761,7 @@
                                        newval = typ()
                                except Exception, e:
                                        dabo.errorLog.write(_("Failed to create 
newval for field '%s'") % field_alias)
-                                       dabo.errorLog.write(str(e))
+                                       dabo.errorLog.write(ustr(e))
                                        newval = u""
 
                        self._blank[field_alias] = newval
@@ -1984,19 +1985,19 @@
                        except KeyError:
                                return rec[fld]
 
-               ret = ""
+               ret = []
                for fld in keyFields:
                        fldSafe = bo.encloseNames(fld, self.AutoQuoteNames)
                        if ret:
-                               ret += " AND "
+                               ret.append(" AND ")
                        pkVal = getPkVal(fld)
                        if isinstance(pkVal, basestring):
-                               ret += tblPrefix + fldSafe + "='" + 
pkVal.encode(self.Encoding) + "' "
+                               ret.extend([tblPrefix, fldSafe, "='", 
pkVal.encode(self.Encoding), "' "])
                        elif isinstance(pkVal, (datetime.date, 
datetime.datetime)):
-                               ret += tblPrefix + fldSafe + "=" + 
self.formatDateTime(pkVal) + " "
+                               ret.extend([tblPrefix, fldSafe, "=", 
self.formatDateTime(pkVal), " "])
                        else:
-                               ret += tblPrefix + fldSafe + "=" + str(pkVal) + 
" "
-               return ret
+                               ret.extend(tblPrefix, fldSafe, "=", 
ustr(pkVal), " "])
+               return "".join(ret)
 
 
        def makeUpdClause(self, diff):
@@ -2603,7 +2604,7 @@
                        self._keyField = tuple(flds)
                        self._compoundKey = True
                else:
-                       self._keyField = str(kf)
+                       self._keyField = ustr(kf)
                        self._compoundKey = False
                self.AuxCursor._keyField = self._keyField
                self.AuxCursor._compoundKey = self._compoundKey

Modified: trunk/dabo/db/dDataSet.py
===================================================================
--- trunk/dabo/db/dDataSet.py   2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dDataSet.py   2010-07-10 13:26:08 UTC (rev 5911)
@@ -25,6 +25,7 @@
 
 import dabo
 from dabo.dLocalize import _
+from dabo.lib.utils import ustr
 
 
 
@@ -83,7 +84,7 @@
 
        def _adapt_decimal(self, decVal):
                """Converts the decimal value to a string for storage"""
-               return str(decVal)
+               return ustr(decVal)
 
 
        def _convert_decimal(self, strval):
@@ -296,7 +297,7 @@
                        dabo.errorLog.write(_("Cannot populate without data for 
alias '%s'")
                                        % alias)
                        return None
-               hs = hashlib.md5(str(ds)).hexdigest()
+               hs = hashlib.md5(ustr(ds)).hexdigest()
                if hs == ds._dataHash:
                        # Data's already there and hasn't changed; no need to 
re-load it
                        return

Modified: trunk/dabo/db/dbFirebird.py
===================================================================
--- trunk/dabo/db/dbFirebird.py 2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbFirebird.py 2010-07-10 13:26:08 UTC (rev 5911)
@@ -5,8 +5,10 @@
 from dabo.dLocalize import _
 from dBackend import dBackend
 from dCursorMixin import dCursorMixin
+from dabo.lib.utils import ustr
 
 
+
 class Firebird(dBackend):
        """Class providing Firebird connectivity. Uses kinterbasdb."""
 
@@ -50,10 +52,10 @@
                if not port:
                        port = 3050
                # kinterbasdb will barf with unicode strings:
-               host = str(connectInfo.Host)
-               user = str(connectInfo.User)
-               password = str(connectInfo.revealPW())
-               database = str(connectInfo.Database)
+               host = ustr(connectInfo.Host)
+               user = ustr(connectInfo.User)
+               password = ustr(connectInfo.revealPW())
+               database = ustr(connectInfo.Database)
                
                self._connection = self.dbapi.connect(host=host, user=user, 
                                password=password, database=database, **kwargs)
@@ -98,7 +100,7 @@
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
 
 

Modified: trunk/dabo/db/dbMsSQL.py
===================================================================
--- trunk/dabo/db/dbMsSQL.py    2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbMsSQL.py    2010-07-10 13:26:08 UTC (rev 5911)
@@ -2,7 +2,10 @@
 import datetime
 from dabo.dLocalize import _
 from dBackend import dBackend
+from dabo.lib.utils import ustr
 
+
+
 class MSSQL(dBackend):
        """Class providing Microsoft SQL Server connectivity. Uses pymssql."""
        def __init__(self):
@@ -21,7 +24,7 @@
                  __init__(self, dsn, user, passwd, database = None, strip = 
0)"""
                import pymssql 
                
-               port = str(connectInfo.Port)
+               port = ustr(connectInfo.Port)
                #if not port or port == "None":
                        #port = 1433
                #host = "%s:%s" % (connectInfo.Host, port)
@@ -64,7 +67,7 @@
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
        
 

Modified: trunk/dabo/db/dbMySQL.py
===================================================================
--- trunk/dabo/db/dbMySQL.py    2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbMySQL.py    2010-07-10 13:26:08 UTC (rev 5911)
@@ -6,7 +6,10 @@
 from dBackend import dBackend
 import dabo.dException as dException
 from dNoEscQuoteStr import dNoEscQuoteStr as dNoEQ
+from dabo.lib.utils import ustr
 
+
+
 class MySQL(dBackend):
        """Class providing MySQL connectivity. Uses MySQLdb."""
 
@@ -68,7 +71,7 @@
                                        db=connectInfo.Database, port=port, 
charset=charset, **kwargs)
                except Exception, e:
                        try:
-                               errMsg = str(e).decode(self.Encoding)
+                               errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
                                errMsg = unicode(e)
                        if "access denied" in errMsg.lower():
@@ -116,7 +119,7 @@
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
        
        
@@ -271,16 +274,16 @@
                                                                                
        
                                elif fld.DataType == "Float":
                                        if fld.Size in (0,1,2,3,4):
-                                               sql = sql + "FLOAT(" + 
str(fld.TotalDP) + "," + str(fld.RightDP) + ") "
+                                               sql = sql + "FLOAT(" + 
ustr(fld.TotalDP) + "," + ustr(fld.RightDP) + ") "
                                        elif fld.Size in (5,6,7,8):
-                                               sql = sql + "DOUBLE(" + 
str(fld.TotalDP) + "," + str(fld.RightDP) + ") "
+                                               sql = sql + "DOUBLE(" + 
ustr(fld.TotalDP) + "," + ustr(fld.RightDP) + ") "
                                        else:
                                                raise #what should happen?
                                elif fld.DataType == "Decimal":
-                                       sql = sql + "DECIMAL(" + 
str(fld.TotalDP) + "," + str(fld.RightDP) + ") "
+                                       sql = sql + "DECIMAL(" + 
ustr(fld.TotalDP) + "," + ustr(fld.RightDP) + ") "
                                elif fld.DataType == "String":
                                        if fld.Size <= 255:
-                                               sql = sql + "VARCHAR(" + 
str(fld.Size) + ") "
+                                               sql = sql + "VARCHAR(" + 
ustr(fld.Size) + ") "
                                        elif fld.Size <= 65535:
                                                sql = sql + "TEXT "
                                        elif fld.Size <= 16777215:

Modified: trunk/dabo/db/dbOracle.py
===================================================================
--- trunk/dabo/db/dbOracle.py   2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbOracle.py   2010-07-10 13:26:08 UTC (rev 5911)
@@ -8,6 +8,7 @@
 import datetime
 from dabo.dLocalize import _
 from dBackend import dBackend
+from dabo.lib.utils import ustr
 
 
 class Oracle(dBackend):
@@ -49,14 +50,14 @@
        def processFields(self, txt):
                # this was used for testing only
                if isinstance(txt, unicode):
-                       txt = str(txt)
+                       txt = ustr(txt)
                return txt
 
 
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
 
                

Modified: trunk/dabo/db/dbPostgreSQL.py
===================================================================
--- trunk/dabo/db/dbPostgreSQL.py       2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbPostgreSQL.py       2010-07-10 13:26:08 UTC (rev 5911)
@@ -5,8 +5,10 @@
 import dabo
 from dabo.dLocalize import _
 from dBackend import dBackend
+from dabo.lib.utils import ustr
 
 
+
 class Postgres(dBackend):
        """Class providing PostgreSQL connectivity. Uses psycopg."""
 
@@ -112,7 +114,7 @@
 
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
-               return "'%s'" % self._stringify(val)
+               return "'%s'" % ustr(val)
 
 
        def getTables(self, cursor, includeSystemTables=False):

Modified: trunk/dabo/db/dbSQLite.py
===================================================================
--- trunk/dabo/db/dbSQLite.py   2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbSQLite.py   2010-07-10 13:26:08 UTC (rev 5911)
@@ -8,6 +8,7 @@
 from dBackend import dBackend
 from dNoEscQuoteStr import dNoEscQuoteStr as dNoEQ
 from dCursorMixin import dCursorMixin
+from dabo.lib.utils import ustr
 
 
 class SQLite(dBackend):
@@ -78,7 +79,7 @@
 
        def formatForQuery(self, val, fieldType=None):
                if isinstance(val, bool):
-                       return str(int(val))
+                       return ustr(int(val))
                else:
                        return super(SQLite, self).formatForQuery(val, 
fieldType)
 
@@ -86,7 +87,7 @@
        def escQuote(self, val):                        
                sl = "\\"
                qt = "\'"
-               val = self._stringify(val)
+               val = ustr(val)
                return qt + val.replace(sl, sl+sl).replace(qt, qt+qt) + qt
        
        
@@ -111,7 +112,7 @@
                        pass
                except Exception, e:
                        try:
-                               errMsg = str(e).decode(self.Encoding)
+                               errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
                                errMsg = unicode(e)
                        dabo.dbActivityLog.write("SQL: commit failed: %s" % 
errMsg)
@@ -133,7 +134,7 @@
        def formatDateTime(self, val):
                """ We need to wrap the value in quotes. """
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
                
        

Modified: trunk/dabo/db/dbTemplate.py
===================================================================
--- trunk/dabo/db/dbTemplate.py 2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/db/dbTemplate.py 2010-07-10 13:26:08 UTC (rev 5911)
@@ -26,8 +26,10 @@
 import datetime
 from dabo.dLocalize import _
 from dBackend import dBackend
+from dabo.lib.utils import ustr
 
 
+
 class NEWDATABASE(dBackend):
        def __init__(self):
                dBackend.__init__(self)
@@ -72,7 +74,7 @@
                #### TODO: Make sure that the format for DateTime 
                ####    values is returned correctly 
                sqt = "'"               # single quote
-               val = self._stringify(val)
+               val = ustr(val)
                return "%s%s%s" % (sqt, val, sqt)
                
 

Modified: trunk/dabo/lib/utils.py
===================================================================
--- trunk/dabo/lib/utils.py     2010-07-07 13:45:33 UTC (rev 5910)
+++ trunk/dabo/lib/utils.py     2010-07-10 13:26:08 UTC (rev 5911)
@@ -139,8 +139,19 @@
                else:
                        ret[kk] = vv
        return ret
-               
 
+
+def ustr(val):
+       """When converting to a string, do not use the str() function, which
+       can create encoding errors with non-ASCII text.
+       """
+       try:
+               return "%s" % val
+       except TypeError:
+               # tuples
+               return val.__repr__()
+
+
 def relativePathList(toLoc, fromLoc=None):
        """Given two paths, returns a list that, when joined with 
        os.path.sep, gives the relative path from 'fromLoc' to



_______________________________________________
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]

Reply via email to