dabo Commit
Revision 3887
Date: 2008-01-18 12:02:55 -0800 (Fri, 18 Jan 2008)
Author: Paul
Trac: http://svn.dabodev.com/trac/dabo/changeset/3887
Changed:
U trunk/dabo/dApp.py
U trunk/dabo/lib/dates.py
U trunk/dabo/lib/reportWriter.py
U trunk/dabo/settings.py
U trunk/dabo/ui/uiwx/dGrid.py
U trunk/dabo/ui/uiwx/dTextBoxMixin.py
Log:
Merged my branch back into trunk.
Added dateFormat, dateTimeFormat, and timeFormat to dabo.settings.
This specifies the format for each type of data, or if None uses
the format from the user's OS settings.
Whatever date/datetime format is in play, we can convert from date
to string and back again, allowing both display and editing.
dTextBox and dGrid are now date-format savvy. dDateTextBox still
needs to be addressed.
dabo.settings.timeFormat currently does nothing.
Diff:
Modified: trunk/dabo/dApp.py
===================================================================
--- trunk/dabo/dApp.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/dApp.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -157,6 +157,9 @@
def __init__(self, selfStart=False, properties=None, *args, **kwargs):
+ if dabo.settings.loadUserLocale:
+ locale.setlocale(locale.LC_ALL, '')
+
self._uiAlreadySet = False
dabo.dAppRef = self
self._beforeInit()
Modified: trunk/dabo/lib/dates.py
===================================================================
--- trunk/dabo/lib/dates.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/lib/dates.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -6,6 +6,7 @@
import datetime
import re
import time
+import dabo
_dregex = {}
_dtregex = {}
@@ -28,7 +29,18 @@
elif format == "MMDD":
exp = "^%(month)s%(day)s$"
else:
- return None
+ conv = {"%d": "%(day)s",
+ "%m": "%(month)s",
+ "%y": "%(shortyear)s",
+ "%Y": "%(year)s"}
+ if "%d" in format and "%m" in format and ("%y" in format or
"%Y" in format):
+ for k in conv.keys():
+ format = format.replace(k, conv[k])
+ format.replace(".", "\.")
+ exp = "^%s$" % format
+ else:
+ return None
+
return re.compile(exp % elements)
@@ -74,14 +86,31 @@
return re.compile(exp % elements)
+def getStringFromDate(dateVal):
+ """Given a datetime.date, convert to string in dabo.settings.dateFormat
style."""
+ dateFormat = dabo.settings.dateFormat
+ if dateFormat is None:
+ # Delegate formatting to the time module, which will take the
+ # user's locale into account.
+ dateFormat = "%x"
+ return dateVal.strftime(dateFormat)
+
+
def getDateFromString(strVal, formats=None):
"""Given a string in a defined format, return a date object or None."""
global _dregex
+ dateFormat = dabo.settings.dateFormat
ret = None
+
if formats is None:
formats = ["ISO8601"]
-
+
+ if dateFormat is not None:
+ # Take the date format as set in dabo into account, when trying
+ # to make a date out of the string.
+ formats.append(dateFormat)
+
# Try each format in order:
for format in formats:
try:
@@ -111,16 +140,38 @@
pass
if ret is not None:
break
+ if ret is None:
+ if dateFormat is None:
+ # Fall back to the current locale setting in user's os
account:
+ try:
+ ret = datetime.date(*time.strptime(strVal,
"%x")[:3])
+ except:
+ pass
return ret
+def getStringFromDateTime(dateTimeVal):
+ """Given a datetime.datetime, convert to string in
dabo.settings.dateTimeFormat style."""
+ dateTimeFormat = dabo.settings.dateTimeFormat
+ if dateTimeFormat is None:
+ # Delegate formatting to the time module, which will take the
+ # user's locale into account.
+ dateTimeFormat = "%x %X"
+ return dateTimeVal.strftime(dateTimeFormat)
+
+
def getDateTimeFromString(strVal, formats=None):
"""Given a string in a defined format, return a datetime object or
None."""
global _dtregex
+ dtFormat = dabo.settings.dateTimeFormat
ret = None
+
if formats is None:
formats = ["ISO8601"]
+
+ if dtFormat is not None:
+ formats.append(dtFormat)
for format in formats:
regex = _dtregex.get(format, None)
@@ -158,7 +209,14 @@
# (Sept. only has 30 days but the regex will
allow 31, etc.)
pass
if ret is not None:
- break
+ break
+ if ret is None:
+ if dtFormat is None:
+ # Fall back to the current locale setting in user's os
account:
+ try:
+ ret = datetime.datetime(*time.strptime(strVal,
"%x %X"))
+ except:
+ pass
return ret
Modified: trunk/dabo/lib/reportWriter.py
===================================================================
--- trunk/dabo/lib/reportWriter.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/lib/reportWriter.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -72,9 +72,6 @@
else:
ParaClass = platypus.Paragraph
-# Pretty sure we want to do this way earlier in Dabo,
-# but this gets it working for me:
-locale.setlocale(locale.LC_ALL, '')
def toPropDict(dataType, default, doc):
return {"dataType": dataType, "default": default, "doc": doc}
Modified: trunk/dabo/settings.py
===================================================================
--- trunk/dabo/settings.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/settings.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+
# Dabo Global Settings
# Do not modify this file directly. Instead, create a file called
@@ -110,6 +111,18 @@
# Check for web updates?
checkForWebUpdates = True
+# Date and Time formats. None will use the os user's settings, but
+# your code can easily override these. Example:
+# dabo.settings.dateFormat = "%d.%m.%Y" -> "31.12.2008".
+dateFormat = None
+dateTimeFormat = None
+timeFormat = None
+
+# Do we load the os user's locale settings automatically?
+# Pythonista note: this executes:
+# locale.setlocale(locale.LC_ALL, '')
+loadUserLocale = True
+
### Settings - end
# Do not copy/paste anything below this line into settings_override.py.
Modified: trunk/dabo/ui/uiwx/dGrid.py
===================================================================
--- trunk/dabo/ui/uiwx/dGrid.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/ui/uiwx/dGrid.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -2,7 +2,7 @@
import copy
import sys
import datetime
-import locale
+import time
import operator
import re
import wx
@@ -21,6 +21,7 @@
import dabo.dColors as dColors
from dabo.dObject import dObject
from dabo.ui import makeDynamicProperty
+import dabo.lib.dates
# from dabo.lib.profilehooks import profile
# from dabo.dBug import loggit
@@ -333,7 +334,7 @@
dabo.ui.callAfterInterval(200, col_obj._updateCellDynamicProps,
row)
if bizobj:
if field and (row < bizobj.RowCount):
- ret = bizobj.getFieldVal(field, row)
+ ret =
self.getStringValue(bizobj.getFieldVal(field, row))
else:
ret = ""
else:
@@ -345,6 +346,13 @@
ret = self.grid.NoneDisplay
return ret
+ def getStringValue(self, val):
+ """Get the string value to display in the grid."""
+ if isinstance(val, datetime.datetime):
+ return dabo.lib.dates.getStringFromDateTime(val)
+ elif isinstance(val, datetime.date):
+ return dabo.lib.dates.getStringFromDate(val)
+ return val
def SetValue(self, row, col, value):
field = self.grid.Columns[col].DataField
Modified: trunk/dabo/ui/uiwx/dTextBoxMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dTextBoxMixin.py 2008-01-18 18:09:02 UTC (rev 3886)
+++ trunk/dabo/ui/uiwx/dTextBoxMixin.py 2008-01-18 20:02:55 UTC (rev 3887)
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import re
import datetime
+import time
import locale
import wx
import wx.lib.masked as masked
@@ -15,6 +16,7 @@
decimal = None
numericTypes = (int, long, float)
valueErrors = (ValueError, )
+
# Make this locale-independent
decimalPoint = locale.localeconv()["decimal_point"]
@@ -507,13 +509,9 @@
# keep it unicode instead of converting to str
strVal = value
elif isinstance(value, datetime.datetime):
- # Use the ISO 8601 datetime string format (with a " "
separator
- # instead of "T")
- strVal = value.isoformat(" ")
+ strVal = dabo.lib.dates.getStringFromDateTime(value)
elif isinstance(value, datetime.date):
- # Use the ISO 8601 date string format so we can convert
- # back from a known quantity later...
- strVal = value.isoformat()
+ strVal = dabo.lib.dates.getStringFromDate(value)
elif isinstance(value, datetime.time):
# Use the ISO 8601 time string format
strVal = value.isoformat()
@@ -637,7 +635,7 @@
try:
convertedVal =
self.convertStringValueToDataType(strVal, dataType)
if self.getStringValue(convertedVal) !=
self.GetValue():
- dabo.ui.callAfterInterval(50,
self._updateStringDisplay)
+ self._updateStringDisplay
except ValueError:
# It couldn't convert; return the previous
value.
convertedVal = self._value
_______________________________________________
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/dabo-dev/[EMAIL PROTECTED]