[Checkins] SVN: z3ext.formatter/trunk/ - Added chameleon support

Nikolay Kim fafhrd91 at gmail.com
Sun Jul 5 03:51:41 EDT 2009


Log message for revision 101559:
  - Added chameleon support
  
  - Added 'formatter' chameleon expression
  
  
  

Changed:
  U   z3ext.formatter/trunk/CHANGES.txt
  U   z3ext.formatter/trunk/setup.py
  A   z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py
  U   z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml
  U   z3ext.formatter/trunk/src/z3ext/formatter/expression.py
  U   z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py
  U   z3ext.formatter/trunk/src/z3ext/formatter/humandatetime.py

-=-
Modified: z3ext.formatter/trunk/CHANGES.txt
===================================================================
--- z3ext.formatter/trunk/CHANGES.txt	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/CHANGES.txt	2009-07-05 07:51:41 UTC (rev 101559)
@@ -2,6 +2,14 @@
 CHANGES
 =======
 
+1.3.0 (2009-07-05)
+------------------
+
+- Added chameleon support
+
+- Added 'formatter' chameleon expression
+
+
 1.2.7 (2009-04-??)
 ------------------
 

Modified: z3ext.formatter/trunk/setup.py
===================================================================
--- z3ext.formatter/trunk/setup.py	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/setup.py	2009-07-05 07:51:41 UTC (rev 101559)
@@ -21,7 +21,7 @@
 def read(*rnames):
     return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
 
-version = '1.2.7dev'
+version = '1.3.0dev'
 
 
 setup(name='z3ext.formatter',
@@ -65,6 +65,7 @@
                           'zope.app.appsetup',
                           'zope.app.pagetemplate',
                           'zope.app.publisher',
+			  'z3c.pt',
                           ],
       extras_require = dict(test=['zope.app.testing',
                                   'zope.testing',

Added: z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py	                        (rev 0)
+++ z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py	2009-07-05 07:51:41 UTC (rev 101559)
@@ -0,0 +1,65 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+""" 
+
+$Id:  2007-12-12 12:27:02Z fafhrd $
+"""
+from zope.component import queryAdapter
+
+from chameleon.core import types
+from chameleon.zpt import expressions
+from z3c.pt.expressions import path_translator
+
+from interfaces import \
+    IFormatterFactory, FormatterNotDefined, FormatterExpressionError
+
+
+class FormatterExpression(object):
+
+    def __call__(self, request, formatterName, formatterArgs, value):
+        factory = queryAdapter(
+            request, IFormatterFactory, name=formatterName)
+
+        if factory is None:
+            raise FormatterNotDefined(self.formatterName)
+
+        return factory(*formatterArgs).format(value)
+
+
+class FormatterTranslator(expressions.ExpressionTranslator):
+
+    symbol = '_get_z3ext_formatter'
+    formatter_traverse = FormatterExpression()
+
+    def translate(self, string, escape=None):
+        parts = string.strip().split(':', 1)
+
+        try:
+            formatterName, string = parts[0], parts[1]
+        except IndexError:
+            raise SyntaxError(string)
+
+        string = path_translator.translate(string, escape)
+
+        args = formatterName.split(',')
+        formatterName = args[0]
+        formatterArgs = args[1:]
+
+        value = types.value(
+            "%s(request, '%s', %s, %s)" % \
+            (self.symbol, formatterName, formatterArgs, string))
+
+        value.symbol_mapping[self.symbol] = self.formatter_traverse
+
+        return value

Modified: z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml	2009-07-05 07:51:41 UTC (rev 101559)
@@ -9,6 +9,11 @@
   <!-- zope.conf configuration -->
   <subscriber handler=".config.initFormatter" />
 
+  <!-- formatter: chameleon expression -->
+  <utility
+     name="formatter"
+     factory=".chameleonexpr.FormatterTranslator" />
+
   <!-- formatter: tales expresison -->
   <tales:expressiontype
      name="formatter"

Modified: z3ext.formatter/trunk/src/z3ext/formatter/expression.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/expression.py	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/src/z3ext/formatter/expression.py	2009-07-05 07:51:41 UTC (rev 101559)
@@ -16,7 +16,6 @@
 
 $Id$
 """
-
 from zope.component import queryAdapter
 from zope.proxy import removeAllProxies
 from zope.tales.expressions import PathExpr, simpleTraverse

Modified: z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py	2009-07-05 07:51:41 UTC (rev 101559)
@@ -19,6 +19,7 @@
 from pytz import utc, timezone
 from datetime import datetime
 from zope import interface, component
+from zope.i18n import translate
 from zope.component import getUtility
 from zope.interface.common.idatetime import ITZInfo
 from zope.publisher.interfaces.http import IHTTPRequest
@@ -73,8 +74,9 @@
                 pos = pattern.find('H')
 
             formatter.setPattern(pattern[pos:])
-            value = _(u'Today at ${value}',
-                      mapping={'value': formatter.format(value)})
+            value = translate(
+                u'Today at ${value}', 'z3ext.formatter',
+                mapping={'value': formatter.format(value)})
             formatter.setPattern(oldpattern)
             return value
 
@@ -85,8 +87,9 @@
                 pos = pattern.find('H')
 
             formatter.setPattern(pattern[pos:])
-            value = _(u'Yesterday at ${value}',
-                      mapping={'value': formatter.format(value)})
+            value = translate(
+                u'Yesterday at ${value}', 'z3ext.formatter',
+                mapping={'value': formatter.format(value)})
             formatter.setPattern(oldpattern)
             return value
 

Modified: z3ext.formatter/trunk/src/z3ext/formatter/humandatetime.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/humandatetime.py	2009-07-05 02:47:34 UTC (rev 101558)
+++ z3ext.formatter/trunk/src/z3ext/formatter/humandatetime.py	2009-07-05 07:51:41 UTC (rev 101559)
@@ -18,6 +18,7 @@
 from pytz import utc, timezone
 from datetime import datetime
 from zope import interface, component
+from zope.i18n import translate
 from zope.component import getUtility
 from zope.interface.common.idatetime import ITZInfo
 from zope.publisher.interfaces.http import IHTTPRequest
@@ -59,31 +60,32 @@
             delta.seconds/3600, delta.seconds/60)
 
         if years > 0:
-            return _(u'${value} year(s) ago',
-                     mapping={'value': years})
+            return translate(
+                u'${value} year(s) ago', 'z3ext.formatter',
+                mapping={'value': years})
 
         if months > 0:
-            return _(u'${value} month(s) ago',
-                     mapping={'value': months})
+            return translate(u'${value} month(s) ago', 'z3ext.formatter',
+                             mapping={'value': months})
 
         if weeks > 0:
-            return _(u'${value} week(s) ago',
-                     mapping={'value': weeks})
+            return translate(u'${value} week(s) ago', 'z3ext.formatter',
+                             mapping={'value': weeks})
 
         if delta.days > 0:
-            return _(u'${value} day(s) ago',
-                     mapping={'value': delta.days})
+            return translate(u'${value} day(s) ago', 'z3ext.formatter',
+                             mapping={'value': delta.days})
 
         if hours > 0:
-            return _(u'${value} hour(s) ago',
-                     mapping={'value': hours})
+            return translate(u'${value} hour(s) ago', 'z3ext.formatter',
+                             mapping={'value': hours})
 
         if minutes > 0:
-            return _(u'${value} minute(s) ago',
-                     mapping={'value': minutes})
+            return translate(u'${value} minute(s) ago', 'z3ext.formatter',
+                             mapping={'value': minutes})
 
-        return _(u'${value} second(s) ago',
-                 mapping={'value': delta.seconds})
+        return translate(u'${value} second(s) ago', 'z3ext.formatter',
+                         mapping={'value': delta.seconds})
 
 
 class HumanDatetimeFormatterFactory(object):



More information about the Checkins mailing list