Top Banner
the repoze.bfg web framework Presented By Chris McDonough Agendaless Consulting Plone Conference 2008
33

Bfg Ploneconf Oct2008

Jan 17, 2015

Download

Technology

Alex Clark

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Bfg Ploneconf Oct2008

therepoze.bfg

web frameworkPresented By

Chris McDonoughAgendaless ConsultingPlone Conference 2008

Page 2: Bfg Ploneconf Oct2008

• 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.

Page 3: Bfg Ploneconf Oct2008

• 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.

Page 4: Bfg Ploneconf Oct2008
Page 5: Bfg Ploneconf Oct2008

WHY?• Zope2: still kicking, but only due to Plone

• Zope3: big, many dependencies. App developers encouraged to use CA.

• Grok: "making Z3 easier to use" (see Zope 3).

• Django: nice, but Z2-like.

• Pylons/TG2: nice, but geared towards RDB / URL dispatch apps.

• Others: not Zope-like / no traction.

Page 6: Bfg Ploneconf Oct2008

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

Page 7: Bfg Ploneconf Oct2008

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).

Page 8: Bfg Ploneconf Oct2008

What BFG Doesn't Do

• Database / persistence

• Sessions

• Indexing / searching

• ZMI / TTW code

• View code sharing with Z3 / Five

• etc...

Page 9: Bfg Ploneconf Oct2008

The Application is the Application

• You don't write "Products" 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.

Page 10: Bfg Ploneconf Oct2008

Graph Traversal and URL Dispatch

• BFG supports both graph traversal (ala Zope) and "URL dispatch" (ala everything else) to map URLs to code.

• URL dispatch is backed by Routes.

Page 11: Bfg Ploneconf Oct2008

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.

Page 12: Bfg Ploneconf Oct2008

Components

• WSGI pipeline.

• Router (aka Publisher).

• "Application registry" (ZCML / decorators).

• Views (aka "controllers" in Pylons/Rails).

• Templates (chameleon/XSL/other).

Page 13: Bfg Ploneconf Oct2008

DependenciesPaste-1.7.1-py2.4.egg zope.deferredimport-3.4.0-py2.4.eggPasteDeploy-1.3.2-py2.4.egg zope.deprecation-3.4.0-py2.4.eggPasteScript-1.6.3-py2.4.egg zope.event-3.4.0-py2.4.eggRoutes-1.9.2-py2.4.egg zope.exceptions-3.5.2-py2.4.eggWebOb-0.9.2-py2.4.egg zope.hookable-3.4.0-py2.4-macosx-10.5-i386.eggzope.i18n-3.5.0-py2.4.egg zope.proxy-3.4.1-py2.4-macosx-10.5-i386.eggelementtree-1.2.6_20050316-py2.4.egg zope.i18nmessageid-3.4.3-py2.4-macosx-10.5-i386.egglxml-2.1.1-py2.4-macosx-10.5-i386.egg zope.interface-3.4.1-py2.4-macosx-10.5-i386.eggpytz-2008c-py2.4.egg zope.location-3.4.0-py2.4.eggsetuptools-0.6c8-py2.4.egg zope.publisher-3.5.3-py2.4.eggzope.schema-3.4.0-py2.4.egg zope.traversing-3.5.0a3-py2.4.eggz3c.pt-1.0a1-py2.4.egg zope.security-3.5.1-py2.4-macosx-10.5-i386.eggzope.component-3.4.0-py2.4.egg zope.testing-3.5.1-py2.4.eggzope.configuration-3.4.0-py2.4.egg

Many are due to inappropriate dependencies in Zope eggs.

Page 14: Bfg Ploneconf Oct2008

How it Works

Page 15: Bfg Ploneconf Oct2008

Models• "content" 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

Page 16: Bfg Ploneconf Oct2008

A Modelfrom zope.interface import implementsfrom zope.interface import Interface

class IMyModel(Interface): pass

class MyModel(object): implements(IMyModel) def __init__(self, name): self.name = name

Page 17: Bfg Ploneconf Oct2008

Views

• Views are functions which accept a "context" (a model object) and a "request".

• Views must return a response. Unlike Zope.

• The view may use a template to generate the response body, or not.

Page 18: Bfg Ploneconf Oct2008

A View

from webob import Response

def hello_world(context, request): return Response('Hello world!')

Page 19: Bfg Ploneconf Oct2008

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/hello.pt', context=context, myname='chris')

Page 20: Bfg Ploneconf Oct2008

Using Model Data from a View

from webob import Response

def hello_world(context, request): name = context.name return Response('Hello %s!' % name)

Page 21: Bfg Ploneconf Oct2008

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).

Page 22: Bfg Ploneconf Oct2008

Traversal• The BFG router traverses the model graph based on

the path segments in the URL (like Zope does).

• If traversal "falls off" 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.

Page 23: Bfg Ploneconf Oct2008

View Registrations<configure xmlns="http://namespaces.zope.org/zope" xmlns:bfg="http://namespaces.repoze.org/bfg" i18n_domain="repoze.bfg">

<include package="repoze.bfg" />

<— default view for .interfaces.IMyModel —> <bfg:view for=".interfaces.IMyModel" view=".views.my_view" />

<— named view for .interfaces.IMyModel —> <bfg:view for=".interfaces.IMyModel" view=".views.other_view" name="other" /></configure>

Page 24: Bfg Ploneconf Oct2008

View Registrations via Decorators

from webob import Responsefrom repoze.bfg.convention import \ bfg_view

@bfg_view(name='hello.html')def hello_world(context, request): name = context.name return Response('Hello %s!' % name)

Page 25: Bfg Ploneconf Oct2008

Speed<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tal="http://xml.zope.org/namespaces/tal"> <head> <title tal:content="context.title">The title</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> </head> <body> <h2><span tal:replace="context.title_or_id()">content title or id</span> <span tal:condition="context.title" tal:replace="context.title">optional template title</span></h2>

This is Page Template <em tal:content="request.view_name">template id</em>. </body></html>

Zope 2.10 view page template: 309 req/secvs.

BFG view + chameleon.zpt template: 869 req/sec

Page 26: Bfg Ploneconf Oct2008

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).

Page 27: Bfg Ploneconf Oct2008

Startup Speed

• repoze.bfg caches the ZCML registry as a pickle for faster startup.

• Startup time with a single view defined: 900ms.

Page 28: Bfg Ploneconf Oct2008

Paste-Derived Reload Feature

[chrism@vitaminf myproj]$ ../bin/paster serve myproj.ini —reloadStarting subprocess with file monitorStarting server in PID 61405.serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

/Users/chrism/projects/repoze/bfgenv/myproj/myproj/run.py changed; reloading...—————————— Restarting ——————————Starting server in PID 61409.serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

Page 29: Bfg Ploneconf Oct2008

Template Auto-Reload Feature

• Templates auto-reload by default.

• This can be turned off for speed.

Page 30: Bfg Ploneconf Oct2008

WSGI-Middleware-Derived Features

• Profiler (repoze.profile)

• Alternate exception handlers (paste.evalexception).

• Caching (repoze.accelerator)

• Theming (Deliverance).

• Transaction management (repoze.tm2)

• etc...

Page 31: Bfg Ploneconf Oct2008

Project Setup Demo

Page 32: Bfg Ploneconf Oct2008

Futures

• Vudo CMS (http://docs.vudo.me) 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.

Page 33: Bfg Ploneconf Oct2008

The End

• For a more in-depth exploration of how a bfg app is written, see the screencast at

http://static.repoze.org/presentations/repozebfg-wiki.mov