Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Bfg Ploneconf Oct2008


Published on

Published in: Technology

Bfg Ploneconf Oct2008

  1. 1. the repoze.bfg web framework Presented By Chris McDonough Agendaless Consulting Plone Conference 2008
  2. 2. • About a year old. • Allow Zope developers to use WSGI technologies more easily. • Allow non-Zope developers to use Zope technologies without using all of Zope.
  3. 3. • repoze.bfg: July 2008 • Chris McDonough, Tres Seaver, Paul Everitt, Carlos de la Guardia, Malthe Borch, Stefan Eletzhofer, Fernando Neto-Correa. • Big f'ing gun -OR- big friendly giant: you choose.
  4. 4. WHY? • Zope2: still kicking, but only due to Plone • Zope3: big, many dependencies. App developers encouraged to use CA. • Grok: quot;making Z3 easier to usequot; (see Zope 3). • Django: nice, but Z2-like. • Pylons/TG2: nice, but geared towards RDB / URL dispatch apps. • Others: not Zope-like / no traction.
  5. 5. Goals • Familiarity: like Zope, but less • Simplicity: pay only for what you eat • Speed: go as fast as possible while still actually doing something • Documentation: the lack of formal documentation of a feature or API is a bug • Collaboration: culture of using and promoting non-Zope stuff
  6. 6. What BFG Does • Maps URLs to code • Provides mechanisms that allow developers to make declarative security assertions • Provides text and HTML templating facilities • Allows for use of existing Zope libs (via ZCML).
  7. 7. What BFG Doesn't Do • Database / persistence • Sessions • Indexing / searching • ZMI / TTW code • View code sharing with Z3 / Five • etc...
  8. 8. The Application is the Application • You don't write quot;Productsquot; to plug into BFG. Extensibility is via normal Python packages and ZCML as necessary. • An application is generated for you; you develop the application and run the application; you don't run the framework.
  9. 9. Graph Traversal and URL Dispatch • BFG supports both graph traversal (ala Zope) and quot;URL dispatchquot; (ala everything else) to map URLs to code. • URL dispatch is backed by Routes.
  10. 10. Zope CA • The Zope Component Architecture was used to construct BFG. • BFG application developers don't need to use the CA. It's a framework implementation detail.
  11. 11. Components • WSGI pipeline. • Router (aka Publisher). • quot;Application registryquot; (ZCML / decorators). • Views (aka quot;controllersquot; in Pylons/Rails). • Templates (chameleon/XSL/other).
  12. 12. Dependencies Paste-1.7.1-py2.4.egg zope.deferredimport-3.4.0-py2.4.egg PasteDeploy-1.3.2-py2.4.egg zope.deprecation-3.4.0-py2.4.egg PasteScript-1.6.3-py2.4.egg zope.event-3.4.0-py2.4.egg Routes-1.9.2-py2.4.egg zope.exceptions-3.5.2-py2.4.egg WebOb-0.9.2-py2.4.egg zope.hookable-3.4.0-py2.4-macosx-10.5-i386.egg zope.i18n-3.5.0-py2.4.egg zope.proxy-3.4.1-py2.4-macosx-10.5-i386.egg elementtree-1.2.6_20050316-py2.4.egg zope.i18nmessageid-3.4.3-py2.4-macosx-10.5-i386.egg lxml-2.1.1-py2.4-macosx-10.5-i386.egg zope.interface-3.4.1-py2.4-macosx-10.5-i386.egg pytz-2008c-py2.4.egg zope.location-3.4.0-py2.4.egg setuptools-0.6c8-py2.4.egg zope.publisher-3.5.3-py2.4.egg zope.schema-3.4.0-py2.4.egg zope.traversing-3.5.0a3-py2.4.egg zope.component-3.4.0-py2.4.egg zope.testing-3.5.1-py2.4.egg zope.configuration-3.4.0-py2.4.egg Many are due to inappropriate dependencies in Zope eggs.
  13. 13. How it Works
  14. 14. Models • quot;contentquot; objects • typically arranged in a graph (although not required) • For Zope people: think ZODB • ZODB not required for repoze.bfg applications: filesystem directories, XML documents, RDF graphs, SQL databases, etc. can be the source of model data
  15. 15. A Model from zope.interface import implements from zope.interface import Interface class IMyModel(Interface): pass class MyModel(object): implements(IMyModel) def __init__(self, name): = name
  16. 16. Views • Views are functions which accept a quot;contextquot; (a model object) and a quot;requestquot;. • Views must return a response. Unlike Zope. • The view may use a template to generate the response body, or not.
  17. 17. A View from webob import Response def hello_world(context, request): return Response('Hello world!')
  18. 18. A View That Renders a chameleon.zpt Template from repoze.bfg.chameleon_zpt import render_template_to_response def hello_world(context, request): render_template_to_response( 'templates/', context=context, myname='chris')
  19. 19. Using Model Data from a View from webob import Response def hello_world(context, request): name = return Response('Hello %s!' % name)
  20. 20. Templates • Default templating engine: chalmeleon by Malthe Borch. ZPT or Genshi syntax. ~ 10X - 15X faster than zope.pagetemplate. • Included: XSLT. • Add-on: jinja2 (Django-style, via repoze.bfg.jinja2) • Any other you'd like to use; bindings are simple to create (see the jinja2 bindings).
  21. 21. Traversal • The BFG router traverses the model graph based on the path segments in the URL (like Zope does). • If traversal quot;falls offquot; the end of the model graph, and there are path segments remaining, it looks for a view registered against the interface of the last model traversed using the next path segment as the view name. • If there are no segments remaining, the default view is used. • If no view is found, a 404 not found error is returned.
  22. 22. View Registrations <configure xmlns=quot;; xmlns:bfg=quot;; i18n_domain=quot;repoze.bfgquot;> <include package=quot;repoze.bfgquot; /> <— default view for .interfaces.IMyModel —> <bfg:view for=quot;.interfaces.IMyModelquot; view=quot;.views.my_viewquot; /> <— named view for .interfaces.IMyModel —> <bfg:view for=quot;.interfaces.IMyModelquot; view=quot;.views.other_viewquot; name=quot;otherquot; /> </configure>
  23. 23. View Registrations via Decorators from webob import Response from repoze.bfg.convention import bfg_view @bfg_view(name='hello.html') def hello_world(context, request): name = return Response('Hello %s!' % name)
  24. 24. Speed <html xmlns=quot;; xmlns:tal=quot;;> <head> <title tal:content=quot;context.titlequot;>The title</title> <meta http-equiv=quot;content-typequot; content=quot;text/html;charset=utf-8quot;/> </head> <body> <h2><span tal:replace=quot;context.title_or_id()quot;>content title or id</span> <span tal:condition=quot;context.titlequot; tal:replace=quot;context.titlequot;>optional template title</span></h2> This is Page Template <em tal:content=quot;request.view_namequot;>template id</em>. </body> </html> Zope 2.10 view page template: 309 req/sec vs. BFG view + chameleon.zpt template: 869 req/sec
  25. 25. Memory Usage and Process Sharing • Existing Zope 2 and Zope 3 apps cannot run side-by-side in the same process due to globals sharing (ZCML global registry, database references); BFG has been written to make such a configuration work by default. • Lower memory requirements per application instance (BFG minimum ~15 MB vs. Zope 3 minimum ~50MB).
  26. 26. Startup Speed • repoze.bfg caches the ZCML registry as a pickle for faster startup. • Startup time with a single view defined: 900ms.
  27. 27. Paste-Derived Reload Feature [chrism@vitaminf myproj]$ ../bin/paster serve myproj.ini —reload Starting subprocess with file monitor Starting server in PID 61405. serving on view at /Users/chrism/projects/repoze/bfgenv/myproj/myproj/ changed; reloading... —————————— Restarting —————————— Starting server in PID 61409. serving on view at
  28. 28. Template Auto-Reload Feature • Templates auto-reload by default. • This can be turned off for speed.
  29. 29. WSGI-Middleware- Derived Features • Profiler (repoze.profile) • Alternate exception handlers (paste.evalexception). • Caching (repoze.accelerator) • Theming (Deliverance). • Transaction management (repoze.tm2) • etc...
  30. 30. Project Setup Demo
  31. 31. Futures • Vudo CMS ( to be implemented using repoze.bfg, hopefully. Work towards this: repoze.bfg.skins, repoze.bfg.layout, repoze.bitblt, repoze.squeeze. • BFG will stay minimal. Add-ons and superframeworks like vudo and repoze.lemonade will provide functionality.
  32. 32. The End • For a more in-depth exploration of how a bfg app is written, see the screencast at