dabo Commit
Revision 3879
Date: 2008-01-17 13:05:11 -0800 (Thu, 17 Jan 2008)
Author: Paul
Trac: http://svn.dabodev.com/trac/dabo/changeset/3879
Changed:
U branches/paul/dabo/__init__.py
U branches/paul/dabo/lib/dates.py
U branches/paul/dabo/settings.py
U branches/paul/dabo/ui/uiwx/dGrid.py
U branches/paul/dabo/ui/uiwx/dTextBoxMixin.py
Log:
Added settings.loadUserLocale, default True. This makes the
locale.setlocale(locale.LC_ALL, '') call, which I think is a nice
thing to do. However, if it causes problems people can turn it
off and make the call themselves when needed.
Added settings for dateFormat, dateTimeFormat, and timeFormat. At
this time, the only one implemented is dateFormat. A value of None
means that Dabo will delegate to time.strftime("%x").
Diff:
Modified: branches/paul/dabo/__init__.py
===================================================================
--- branches/paul/dabo/__init__.py 2008-01-17 07:19:02 UTC (rev 3878)
+++ branches/paul/dabo/__init__.py 2008-01-17 21:05:11 UTC (rev 3879)
@@ -102,6 +102,7 @@
import os
import sys
+import locale
try:
import pysqlite2
except ImportError:
@@ -150,6 +151,9 @@
# Import global settings (do this first, as other imports may rely on it):
from settings import *
+if settings.loadUserLocale:
+ locale.setlocale(locale.LC_ALL, '')
+
from __version__ import version
import dColors
import dEvents
Modified: branches/paul/dabo/lib/dates.py
===================================================================
--- branches/paul/dabo/lib/dates.py 2008-01-17 07:19:02 UTC (rev 3878)
+++ branches/paul/dabo/lib/dates.py 2008-01-17 21:05:11 UTC (rev 3879)
@@ -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:
@@ -112,11 +141,12 @@
if ret is not None:
break
if ret is None:
- ## Fall back to the current locale setting:
- try:
- ret = datetime.date(*time.strptime(strVal, "%x")[:3])
- except:
- ret = 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
Modified: branches/paul/dabo/settings.py
===================================================================
--- branches/paul/dabo/settings.py 2008-01-17 07:19:02 UTC (rev 3878)
+++ branches/paul/dabo/settings.py 2008-01-17 21:05:11 UTC (rev 3879)
@@ -111,7 +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: branches/paul/dabo/ui/uiwx/dGrid.py
===================================================================
--- branches/paul/dabo/ui/uiwx/dGrid.py 2008-01-17 07:19:02 UTC (rev 3878)
+++ branches/paul/dabo/ui/uiwx/dGrid.py 2008-01-17 21:05:11 UTC (rev 3879)
@@ -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
@@ -348,7 +349,7 @@
def getStringValue(self, val):
"""Get the string value to display in the grid."""
if isinstance(val, datetime.date):
- return time.strftime("%x", (val.year, val.month,
val.day, 0, 0, 0, 0, 0, 0))
+ return dabo.lib.dates.getStringFromDate(val)
return val
def SetValue(self, row, col, value):
Modified: branches/paul/dabo/ui/uiwx/dTextBoxMixin.py
===================================================================
--- branches/paul/dabo/ui/uiwx/dTextBoxMixin.py 2008-01-17 07:19:02 UTC (rev
3878)
+++ branches/paul/dabo/ui/uiwx/dTextBoxMixin.py 2008-01-17 21:05:11 UTC (rev
3879)
@@ -513,7 +513,7 @@
# instead of "T")
strVal = value.isoformat(" ")
elif isinstance(value, datetime.date):
- strVal = time.strftime("%x", (value.year, value.month,
value.day, 0, 0, 0, 0, 0, 0))
+ strVal = dabo.lib.dates.getStringFromDate(value)
elif isinstance(value, datetime.time):
# Use the ISO 8601 time string format
strVal = value.isoformat()
@@ -532,7 +532,7 @@
"""Given a string in an accepted date format, return a
datetime.date object, or None.
"""
- formats = [dabo.settings.dateFormat, "ISO8601"]
+ formats = ["ISO8601"]
if not self.StrictDateEntry:
# Add some less strict date-entry formats:
formats.append("YYYYMMDD")
_______________________________________________
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]