[Checkins] SVN: hurry.custom/trunk/s * Add ZCML directives for setting up template languages and so on.

Martijn Faassen faassen at startifact.com
Fri May 22 11:45:38 EDT 2009


Log message for revision 100238:
  * Add ZCML directives for setting up template languages and so on.
  
  * Add jsontemplate support as a template language (extension .jsont).
  
  

Changed:
  U   hurry.custom/trunk/setup.py
  U   hurry.custom/trunk/src/hurry/custom/__init__.py
  A   hurry.custom/trunk/src/hurry/custom/configure.zcml
  A   hurry.custom/trunk/src/hurry/custom/jsont.py
  A   hurry.custom/trunk/src/hurry/custom/jsontemplate.txt
  A   hurry.custom/trunk/src/hurry/custom/meta.zcml
  U   hurry.custom/trunk/src/hurry/custom/tests.py
  A   hurry.custom/trunk/src/hurry/custom/zcml.py

-=-
Modified: hurry.custom/trunk/setup.py
===================================================================
--- hurry.custom/trunk/setup.py	2009-05-22 15:20:26 UTC (rev 100237)
+++ hurry.custom/trunk/setup.py	2009-05-22 15:45:38 UTC (rev 100238)
@@ -37,5 +37,7 @@
        'zope.component',
        'zope.interface',
        'zope.hookable',
+       'jsontemplate',
+       'zope.configuration',
        ],
     )

Modified: hurry.custom/trunk/src/hurry/custom/__init__.py
===================================================================
--- hurry.custom/trunk/src/hurry/custom/__init__.py	2009-05-22 15:20:26 UTC (rev 100237)
+++ hurry.custom/trunk/src/hurry/custom/__init__.py	2009-05-22 15:45:38 UTC (rev 100238)
@@ -12,5 +12,7 @@
 
 from hurry.custom.interfaces import IHurryCustomAPI
 
+from hurry.custom.jsont import JsonTemplate
+
 moduleProvides(IHurryCustomAPI)
 __all__ = list(IHurryCustomAPI)

Added: hurry.custom/trunk/src/hurry/custom/configure.zcml
===================================================================
--- hurry.custom/trunk/src/hurry/custom/configure.zcml	                        (rev 0)
+++ hurry.custom/trunk/src/hurry/custom/configure.zcml	2009-05-22 15:45:38 UTC (rev 100238)
@@ -0,0 +1,11 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           xmlns:custom="http://namespaces.zope.org/custom">
+
+  <include package="hurry.custom" file="meta.zcml" />
+
+  <custom:templateLanguage 
+        template_class=".JsonTemplate"
+        extension=".jsont"
+        sample_extension=".json" />
+
+</configure>

Added: hurry.custom/trunk/src/hurry/custom/jsont.py
===================================================================
--- hurry.custom/trunk/src/hurry/custom/jsont.py	                        (rev 0)
+++ hurry.custom/trunk/src/hurry/custom/jsont.py	2009-05-22 15:45:38 UTC (rev 100238)
@@ -0,0 +1,15 @@
+import jsontemplate
+from zope.interface import implements
+from hurry.custom.interfaces import ITemplate
+
+class JsonTemplate(object):
+    implements(ITemplate)
+
+    def __init__(self, source):
+        self.json_template = jsontemplate.Template(source)
+        self.source = source
+        
+    def __call__(self, input):
+        return self.json_template.expand(input)
+
+        

Added: hurry.custom/trunk/src/hurry/custom/jsontemplate.txt
===================================================================
--- hurry.custom/trunk/src/hurry/custom/jsontemplate.txt	                        (rev 0)
+++ hurry.custom/trunk/src/hurry/custom/jsontemplate.txt	2009-05-22 15:45:38 UTC (rev 100238)
@@ -0,0 +1,45 @@
+Integration of JSON Template
+============================
+
+Introduction
+------------
+
+We've provided an integration of JSON Template as one template
+language into hurry.custom. 
+
+More about JSON template can be found here:
+
+http://json-template.googlecode.com
+
+Using the template language
+---------------------------
+
+Before we can use the template language we need to make sure it is 
+registered with the system::
+
+  >>> from hurry.custom.jsont import JsonTemplate
+  >>> from hurry import custom
+  >>> custom.register_language(JsonTemplate, extension='.jsont')
+
+The system can now recognize ``.jsont`` templates. We demonstrate this
+by creating one on the filesystem and registering the directory it is
+in as a collection::
+
+  >>> import tempfile, os
+  >>> templates_path = tempfile.mkdtemp(prefix='hurry.custom.jsont')
+  >>> test1_path = os.path.join(templates_path, 'test1.jsont')
+  >>> f = open(test1_path, 'w')
+  >>> f.write("Hello {target}!")
+  >>> f.close()
+  >>> custom.register_collection(id='templates', path=templates_path)
+
+We can now look up the template::
+
+  >>> template = custom.lookup('templates', 'test1.jsont')
+  >>> template.source
+  u'Hello {target}!'
+
+Let's try using it, passing in JSON::
+
+  >>> template({'target': 'universe'})
+  u'Hello universe!'

Added: hurry.custom/trunk/src/hurry/custom/meta.zcml
===================================================================
--- hurry.custom/trunk/src/hurry/custom/meta.zcml	                        (rev 0)
+++ hurry.custom/trunk/src/hurry/custom/meta.zcml	2009-05-22 15:45:38 UTC (rev 100238)
@@ -0,0 +1,22 @@
+<configure xmlns:meta="http://namespaces.zope.org/meta">
+
+  <meta:directives namespace="http://namespaces.zope.org/custom">
+
+    <meta:directive
+        name="templateLanguage"
+        schema=".zcml.ITemplateLanguage"
+        handler=".zcml.action_template_language" />
+
+    <meta:directive
+        name="dataLanguage"
+        schema=".zcml.IDataLanguage"
+        handler=".zcml.action_data_language" />
+
+    <meta:directive
+        name="collection"
+        schema=".zcml.ICollection"
+        handler=".zcml.action_collection" />
+
+  </meta:directives>
+
+</configure>

Modified: hurry.custom/trunk/src/hurry/custom/tests.py
===================================================================
--- hurry.custom/trunk/src/hurry/custom/tests.py	2009-05-22 15:20:26 UTC (rev 100237)
+++ hurry.custom/trunk/src/hurry/custom/tests.py	2009-05-22 15:45:38 UTC (rev 100238)
@@ -15,6 +15,7 @@
 def tearDownReadMe(test):
     # clean up Zope
     cleanup.cleanUp()
+    setSite(None)
 
 def test_suite():
     optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
@@ -27,7 +28,7 @@
     suite = unittest.TestSuite()
     
     suite.addTest(doctest.DocFileSuite(
-        'README.txt',
+        'README.txt', 'jsontemplate.txt',
         optionflags=optionflags,
         setUp=setUpReadMe,
         tearDown=tearDownReadMe,

Added: hurry.custom/trunk/src/hurry/custom/zcml.py
===================================================================
--- hurry.custom/trunk/src/hurry/custom/zcml.py	                        (rev 0)
+++ hurry.custom/trunk/src/hurry/custom/zcml.py	2009-05-22 15:45:38 UTC (rev 100238)
@@ -0,0 +1,67 @@
+from zope.interface import Interface
+from schema import TextLine
+from zope.configuration.fields import GlobalObject, Path
+
+from hurry import custom
+
+class TemplateLanguage(Interface):
+    template_class = GlobalObject(
+        title=u'Template Class',
+        description=u'The class that implements the template language')
+
+    extension = TextLine(
+        title=u'Extension',
+        description=u'The filesystem extension (with period, '
+                    u'for example .jsont) '
+                    u'recognized for this template language')
+
+    sample_extension = TextLine(
+        title=u'Sample Extension',
+        description=u'The filesystem extension used for sample input data.',
+        required=False)
+
+class DataLanguage(Interface):
+    parse_func = GlobalObject(
+        title=u'Parse function',
+        description=u'A function that can parse text and deliver input data.')
+
+    extension = TextLine(
+        title=u'Extension',
+        description=u'The filesystem extension (with period, '
+                    u'for example .json) '
+                    u'recognzied for this input language')
+
+class Collection(Interface):
+    id = TextLine(
+        title=u'Collection ID',
+        description=u"Globally unique collection id")
+
+    path = Path(
+        title=u"Path",
+        description=u'Filesystem path to collection')
+
+    title = TextLine(
+        title=u"Title",
+        description=u'Human-readable title for collection',
+        required=False)
+
+def action_template_language(_context,
+                             template_class, extension, sample_extension=None):
+    _context.action(
+        discriminator = ('action_template_language',
+                         template_class, extension, sample_extension),
+        callable = custom.register_language,
+        args = (template_class, extension, sample_extension))
+    
+def action_data_language(_context, parser_func, extension):
+    _context.action(
+        discriminator = ('action_data_language',
+                         parser_func, extension),
+        callable = custom.register_data_language,
+        args = (parser_func, extension))
+    
+def action_collection(_context, id, path, title=None):
+    _context.action(
+        discriminator = ('action_collection', id),
+        callable = custom.register_collection,
+        args = (id, path, title))



More information about the Checkins mailing list