[Zope-CVS] SVN: book/trunk/formatns/ Code that goes with the TALES Namespace chapter.

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Aug 24 09:39:56 EDT 2004


Log message for revision 27241:
  Code that goes with the TALES Namespace chapter.
  


Changed:
  A   book/trunk/formatns/
  A   book/trunk/formatns/__init__.py
  A   book/trunk/formatns/configure.zcml
  A   book/trunk/formatns/interfaces.py
  A   book/trunk/formatns/tests.py


-=-
Added: book/trunk/formatns/__init__.py
===================================================================
--- book/trunk/formatns/__init__.py	2004-08-24 13:39:36 UTC (rev 27240)
+++ book/trunk/formatns/__init__.py	2004-08-24 13:39:56 UTC (rev 27241)
@@ -0,0 +1,113 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""'format' TALES Namespace Adapter implementation
+
+$Id: __init__.py,v 1.1 2003/09/16 22:18:56 srichter Exp $
+"""
+from zope.interface import implements
+from zope.tales.interfaces import ITALESFunctionNamespace
+from zope.app import zapi
+from interfaces import IFormatTalesAPI
+
+
+class FormatTalesAPI(object):
+
+    implements(IFormatTalesAPI, ITALESFunctionNamespace)
+
+    def __init__(self, context):
+        self.context = context
+
+    def setEngine(self, engine):
+        """See zope.tales.interfaces.ITALESFunctionNamespace"""
+        self.locale = zapi.removeSecurityProxy(engine.vars['request']).locale
+
+    def shortDate(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'date', 'short').format(self.context)
+        
+    def mediumDate(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'date', 'medium').format(self.context)
+        
+    def longDate(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'date', 'long').format(self.context)
+
+    def fullDate(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'date', 'full').format(self.context)
+
+    def shortTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'time', 'short').format(self.context)
+
+    def mediumTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'time', 'medium').format(self.context)
+
+    def longTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'time', 'long').format(self.context)
+
+    def fullTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'time', 'full').format(self.context)
+
+    def shortDateTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'dateTime', 'short').format(self.context)
+
+    def mediumDateTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'dateTime', 'medium').format(self.context)
+
+    def longDateTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'dateTime', 'long').format(self.context)
+
+    def fullDateTime(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.dates.getFormatter(
+            'dateTime', 'full').format(self.context)
+
+    def decimal(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.numbers.getFormatter(
+            'decimal').format(self.context)
+
+    def percent(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.numbers.getFormatter(
+            'percent').format(self.context)
+
+    def scientific(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.numbers.getFormatter(
+            'scientific').format(self.context)
+
+    def currency(self):
+        """See book.formatns.interfaces.IFormatTalesAPI"""
+        return self.locale.numbers.getFormatter(
+            'currency').format(self.context)

Added: book/trunk/formatns/configure.zcml
===================================================================
--- book/trunk/formatns/configure.zcml	2004-08-24 13:39:36 UTC (rev 27240)
+++ book/trunk/formatns/configure.zcml	2004-08-24 13:39:56 UTC (rev 27241)
@@ -0,0 +1,10 @@
+<configure 
+    xmlns="http://namespaces.zope.org/zope">
+
+  <adapter
+      for="*"
+      provides="zope.app.traversing.interfaces.IPathAdapter"
+      factory=".FormatTalesAPI"
+      name="format" />
+
+</configure>

Added: book/trunk/formatns/interfaces.py
===================================================================
--- book/trunk/formatns/interfaces.py	2004-08-24 13:39:36 UTC (rev 27240)
+++ book/trunk/formatns/interfaces.py	2004-08-24 13:39:56 UTC (rev 27241)
@@ -0,0 +1,136 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""'format' TALES Namespace Interfaces
+
+$Id: interfaces.py,v 1.1 2003/09/16 22:18:56 srichter Exp $
+"""
+from zope.interface import Interface
+
+
+class IFormatTalesAPI(Interface):
+
+    def shortDate(self):
+        """Returns the short date using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def mediumDate(self):
+        """Returns the medium date using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+        
+    def longDate(self):
+        """Returns the long date using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def fullDate(self):
+        """Returns the full date using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def shortTime(self):
+        """Returns the short time using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def mediumTime(self):
+        """Returns the medium time using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def longTime(self):
+        """Returns the long time using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def fullTime(self):
+        """Returns the full time using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def shortDateTime(self):
+        """Returns the short datetime using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def mediumDateTime(self):
+        """Returns the  datetime using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def longDateTime(self):
+        """Returns the long datetime using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def fullDateTime(self):
+        """Returns the full datetime using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def decimal(self):
+        """Returns the full datetime using the user's locale.
+
+        The context of this namespace must be a datetime object, otherwise an
+        exception is raised.
+        """
+
+    def percent(self):
+        """Returns the floating point as percentage using the user's locale.
+
+        The context of this namespace must be a floating point object,
+        otherwise an exception is raised.
+        """
+
+    def scientific(self):
+        """Returns the floating point in scientific notation using the user's
+        locale.
+
+        The context of this namespace must be a floating point object,
+        otherwise an exception is raised.
+        """
+
+    def currency(self):
+        """Returns the floating point as currency using the user's locale.
+
+        The context of this namespace must be a floating point object,
+        otherwise an exception is raised.
+        """
+
+    

Added: book/trunk/formatns/tests.py
===================================================================
--- book/trunk/formatns/tests.py	2004-08-24 13:39:36 UTC (rev 27240)
+++ book/trunk/formatns/tests.py	2004-08-24 13:39:56 UTC (rev 27241)
@@ -0,0 +1,149 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""Test TALES 'format' namespace.
+
+$Id: test_formatns.py,v 1.1 2003/09/16 22:18:56 srichter Exp $
+"""
+import unittest
+from datetime import datetime
+from zope.publisher.browser import TestRequest
+from zope.testing.doctestunit import DocTestSuite
+from book.formatns import FormatTalesAPI
+
+class Engine:
+    vars = {'request': TestRequest(environ={'HTTP_ACCEPT_LANGUAGE': 'en'})}
+
+def getFormatNamespace(context):
+    ns = FormatTalesAPI(context)
+    ns.setEngine(Engine())
+    return ns
+
+
+def shortDate():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortDate()
+    u'9/16/03'
+    """
+    
+def mediumDate():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.mediumDate()
+    u'Sep 16, 2003'
+    """
+    
+def longDate():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.longDate()
+    u'September 16, 2003'
+    """
+
+def fullDate():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.fullDate()
+    u'Tuesday, September 16, 2003'
+    """
+
+def shortTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortTime()
+    u'4:51 PM'
+    """
+
+def mediumTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortTime()
+    u'4:51 PM'
+    """
+
+def longTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortTime()
+    u'4:51 PM'
+    """
+
+def fullTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortTime()
+    u'4:51 PM'
+    """
+
+def shortDateTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.shortDateTime()
+    u'9/16/03 4:51 PM'
+    """
+
+def mediumDateTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.mediumDateTime()
+    u'Sep 16, 2003 4:51:01 PM'
+    """
+
+def longDateTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.longDateTime()
+    u'September 16, 2003 4:51:01 PM +000'
+    """
+
+def fullDateTime():
+    """
+    >>> ns = getFormatNamespace(datetime(2003, 9, 16, 16, 51, 01))
+    >>> ns.fullDateTime()
+    u'Tuesday, September 16, 2003 4:51:01 PM +000'
+    """
+
+def decimal():
+    """
+    >>> ns = getFormatNamespace(4.205)
+    >>> ns.decimal()
+    u'4.205'
+    """
+
+def percent():
+    """
+    >>> ns = getFormatNamespace(4412)
+    >>> ns.percent()
+    u'4,412%'
+    """
+
+def scientific():
+    """
+    >>> ns = getFormatNamespace(4421)
+    >>> ns.scientific()
+    u'4E3'
+    """
+
+def currency():
+    """
+    >>> ns = getFormatNamespace(4.205)
+    >>> ns.currency()
+    u'\\xa44.21'
+    """
+
+def test_suite():
+    return DocTestSuite()
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')



More information about the Zope-CVS mailing list