Commits

Mike Bayer  committed 7d76529

initial render setup.

  • Participants

Comments (0)

Files changed (15)

+.venv
+*.egg-info
+*.pyc
+0.0
+---
+
+-  Initial version
+include *.txt *.ini *.cfg *.rst
+recursive-include template_demo *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml
+template_demo README

File development.ini

+[app:main]
+use = egg:template_demo
+
+pyramid.reload_templates = true
+pyramid.debug_authorization = false
+pyramid.debug_notfound = false
+pyramid.debug_routematch = false
+pyramid.default_locale_name = en
+pyramid.includes = 
+
+[server:main]
+use = egg:waitress#main
+host = 0.0.0.0
+port = 6543
+
+# Begin logging configuration
+
+[loggers]
+keys = root, template_demo
+
+[handlers]
+keys = console
+
+[formatters]
+keys = generic
+
+[logger_root]
+level = INFO
+handlers = console
+
+[logger_template_demo]
+level = DEBUG
+handlers =
+qualname = template_demo
+
+[handler_console]
+class = StreamHandler
+args = (sys.stderr,)
+level = NOTSET
+formatter = generic
+
+[formatter_generic]
+format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
+
+# End logging configuration
+[nosetests]
+match = ^test
+nocapture = 1
+cover-package = template_demo
+with-coverage = 1
+cover-erase = 1
+
+[compile_catalog]
+directory = template_demo/locale
+domain = template_demo
+statistics = true
+
+[extract_messages]
+add_comments = TRANSLATORS:
+output_file = template_demo/locale/template_demo.pot
+width = 80
+
+[init_catalog]
+domain = template_demo
+input_file = template_demo/locale/template_demo.pot
+output_dir = template_demo/locale
+
+[update_catalog]
+domain = template_demo
+input_file = template_demo/locale/template_demo.pot
+output_dir = template_demo/locale
+previous = true
+import os
+
+from setuptools import setup, find_packages
+
+here = os.path.abspath(os.path.dirname(__file__))
+README = open(os.path.join(here, 'README.txt')).read()
+CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
+
+requires = [
+    'pyramid',
+    'pyramid_debugtoolbar',
+    'waitress',
+    ]
+
+setup(name='template_demo',
+      version='1.0',
+      description='illustrate APIs + client/server side templating.',
+      long_description=README + '\n\n' +  CHANGES,
+      classifiers=[
+        "Programming Language :: Python",
+        "Framework :: Pylons",
+        "Topic :: Internet :: WWW/HTTP",
+        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
+        ],
+      author='mike bayer',
+      author_email='mike_mp@zzzcomputing.com',
+      url='',
+      keywords='web pyramid pylons',
+      packages=find_packages(),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=requires,
+      tests_require=requires,
+      test_suite="template_demo",
+      entry_points = """\
+      [paste.app_factory]
+      main = template_demo.application:main
+      """,
+      )
+

File template_demo/__init__.py

Empty file added.

File template_demo/application.py

+from pyramid.config import Configurator
+from .renderers import MakoRenderer
+from .renderers import HandlebarsRenderer
+
+def main(global_config, **settings):
+    """ This function returns a Pyramid WSGI application.
+    """
+    config = Configurator(settings=settings)
+    config.add_static_view('static', 'static', cache_max_age=3600)
+    config.add_renderer('.mako', MakoRenderer)
+    config.add_renderer('.handlebars', HandlebarsRenderer)
+
+    config.add_route('home', '/')
+    config.add_route('client_data', '/client_view')
+    config.add_route('server_data', '/server_view')
+    config.add_route('api_data', '/api_view')
+
+    config.scan()
+    return config.make_wsgi_app()
+

File template_demo/renderers.py

+from pyramid.asset import abspath_from_asset_spec
+from mako.lookup import TemplateLookup
+import re
+
+class MakoRenderer(object):
+    def __init__(self, info):
+        package_name = info.package.__name__
+        self.lookup = TemplateLookup(
+                        directories=abspath_from_asset_spec(
+                                "%s:templates" % package_name
+                        ))
+        self.template_reg = re.compile(
+                    r'(?P<fname>[\w_]+)'
+                    r'(?:\|(?P<defname>[\w_]+))?'
+                    r'(\.(?P<extension>.*))')
+
+    def __call__(self, value, system):
+        return self._render(system['renderer_name'], data=value)
+
+    def _render(self, tname, **kw):
+        fname, defname, ext = self.template_reg.match(
+                                        tname
+                                    ).group(
+                                        "fname", "defname", "extension"
+                                    )
+        template = self.lookup.get_template("%s.%s" % (fname, ext))
+        if defname:
+            template = template.get_def(defname)
+        return template.render_unicode(**kw)
+
+class HandlebarsRenderer(MakoRenderer):
+    def __call__(self, value, system):
+        return super(HandlebarsRenderer, self)._render(
+                "handlebars_base.mako", 
+                data=value, 
+                handlebars=system['renderer_name'])
+
+#class UIRenderer(object):
+#
+#    def __init__(self, info):
+#        self.renderer = zpt_renderer_factory(RendererHelper(
+#            'ui.pt', brac2.views, info.registry))
+#
+#    def __call__(self, value, system):
+#        request = system['request']
+#        admin_user = request.admin_user
+#        resource_types = [
+#            {'title': rt.name,
+#             'href': request.route_url('resource_type_list', name=rt.name),
+#             'api': request.route_url('resource_type_api', name=rt.name),
+#             'template': 'resource_type'}
+#            for rt in request.dbsession.query(
+#                ResourceType).order_by(ResourceType.name)]
+#        return self.renderer(
+#            {
+#             'api': json.dumps(api_links(request)),
+#             'data': json.dumps(value, sort_keys=True, indent=4),
+#             'template': request.matched_route.name,
+#             'resource_types': resource_types,
+#             'login': authenticated_userid(request),
+#             'adminuser': admin_user.is_admin if admin_user else False,
+#            },
+#            system)

File template_demo/static/favicon.ico

Added
New image

File template_demo/templates/handlebars_base.mako

+handlebars w00p

File template_demo/templates/home.mako

+hello world
+
+<%def name="foob()">
+    foob
+</%def>

File template_demo/tests.py

+import unittest
+
+from pyramid import testing
+
+class ViewTests(unittest.TestCase):
+    def setUp(self):
+        self.config = testing.setUp()
+
+    def tearDown(self):
+        testing.tearDown()
+
+    def test_my_view(self):
+        from .views import my_view
+        request = testing.DummyRequest()
+        info = my_view(request)
+        self.assertEqual(info['project'], 'template_demo')

File template_demo/views.py

+from pyramid.view import view_config
+
+@view_config(route_name='home', renderer='home.mako')
+def home(request):
+    return {}
+
+@view_config(route_name='client_data', renderer='snippet.handlebars')
+@view_config(route_name='server_data', renderer='home|snippet.mako')
+@view_config(route_name='api_data', renderer='json')
+def api(request):
+    return {'project':'template_demo'}