[uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 18 Jul 2011 11:33:44 +0200
changeset 7661 e157174c595c
parent 7660 a1506b5306cc
child 7662 3217298823c1
[uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
uilib.py
--- a/uilib.py	Tue Jul 12 17:50:31 2011 +0200
+++ b/uilib.py	Mon Jul 18 11:33:44 2011 +0200
@@ -51,37 +51,65 @@
     assert eid is not None
     return '%s:%s' % (name, eid)
 
+def print_bytes(value, req, props, displaytime=True):
+    return u''
+
+def print_string(value, req, props, displaytime=True):
+    # don't translate empty value if you don't want strange results
+    if props is not None and value and props.get('internationalizable'):
+        return req._(value)
+    return value
+
+def print_date(value, req, props, displaytime=True):
+    return ustrftime(value, req.property_value('ui.date-format'))
+
+def print_time(value, req, props, displaytime=True):
+    return ustrftime(value, req.property_value('ui.time-format'))
+
+def print_tztime(value, req, props, displaytime=True):
+    return ustrftime(value, req.property_value('ui.time-format')) + u' UTC'
+
+def print_datetime(value, req, props, displaytime=True):
+    if displaytime:
+        return ustrftime(value, req.property_value('ui.datetime-format'))
+    return ustrftime(value, req.property_value('ui.date-format'))
+
+def print_tzdatetime(value, req, props, displaytime=True):
+    if displaytime:
+        return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
+    return ustrftime(value, req.property_value('ui.date-format'))
+
+def print_boolean(value, req, props, displaytime=True):
+    if value:
+        return req._('yes')
+    return req._('no')
+
+def print_float(value, req, props, displaytime=True):
+    return unicode(req.property_value('ui.float-format') % value)
+
+PRINTERS = {
+    'Bytes': print_bytes,
+    'String': print_string,
+    'Date': print_date,
+    'Time': print_time,
+    'TZTime': print_tztime,
+    'Datetime': print_datetime,
+    'TZDatetime': print_tzdatetime,
+    'Boolean': print_boolean,
+    'Float': print_float,
+    'Decimal': print_float,
+    # XXX Interval
+    }
+
 def printable_value(req, attrtype, value, props=None, displaytime=True):
     """return a displayable value (i.e. unicode string)"""
-    if value is None or attrtype == 'Bytes':
+    if value is None:
         return u''
-    if attrtype == 'String':
-        # don't translate empty value if you don't want strange results
-        if props is not None and value and props.get('internationalizable'):
-            return req._(value)
-        return value
-    if attrtype == 'Date':
-        return ustrftime(value, req.property_value('ui.date-format'))
-    if attrtype == 'Time':
-        return ustrftime(value, req.property_value('ui.time-format'))
-    if attrtype == 'TZTime':
-        return ustrftime(value, req.property_value('ui.time-format')) + u' UTC'
-    if attrtype == 'Datetime':
-        if displaytime:
-            return ustrftime(value, req.property_value('ui.datetime-format'))
-        return ustrftime(value, req.property_value('ui.date-format'))
-    if attrtype == 'TZDatetime':
-        if displaytime:
-            return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
-        return ustrftime(value, req.property_value('ui.date-format'))
-    if attrtype == 'Boolean':
-        if value:
-            return req._('yes')
-        return req._('no')
-    if attrtype in ('Float', 'Decimal'):
-        value = req.property_value('ui.float-format') % value
-    # XXX Interval
-    return unicode(value)
+    try:
+        printer = PRINTERS[attrtype]
+    except KeyError:
+        return unicode(value)
+    return printer(req, value, props, displaytime)
 
 
 # text publishing #############################################################