Subsections

11. How to use AOP (Aspect Oriented Programing) with CherryPy

11.1 Introduction

This HowTo is intended to people who are already familiar with OOP, but who are not necessarily familiar with AOP. If this is your case, check out http://aosd.net for an introduction to AOP.

CherryPy only implements a few of AOP concepts. I don't have a PHD in AOP and it is not my goal to make CherryPy an AOP reference :-)

Instead, I implemented a few features that I thought might be useful (and they already are, since both HttpAuthenticate and CookieAuthenticate modules use AOP features).

11.2 Basic example

On most websites a common header and footer is used for all (or most) pages of the site. In CherryPy, you can easily implement that using a "classic" object oriented approach: first you define a class that contains the header and the footer that you want to use for all your pages.

CherryClass NormalHeaderAndFooter:
mask:
    def header(self):
        <html><body>I'm the header<br><br>
    def footer(self):
        <br><br>I'm the footer</body></html>

Then, all you have to do is derive your CherryClasses from NormalHeaderAndFooter and call header and footer in each of your masks or views:

CherryClass Root(NormalHeaderAndFooter):
mask:
    def index(self):
        <py-eval="self.header()">
            Hello, world !
        <py-eval="self.footer()">
    def otherPage(self):
        <py-eval="self.header()">
            I love cherry pie !
        <py-eval="self.footer()">

That's the "classic" object oriented approach. It's pretty good, but if we look at it, we'll realize that we have to repeat <py-eval="self.header()"> and <py-eval="self.footer()"> for each mask and view, and this has several drawbacks:

That's where AOP comes in ... Instead of just declaring "normal" header and footer methods, we'll declare "aspect" methods. This basically adds another information which is: "include my code at the beginning or at the end of each of the methods of all derived classes".

Therefore, the implementation of our simple example using AOP would be:

CherryClass NormalHeaderAndFooter:
aspect:
    (1) start:
        _page.append("<html><body>I'm the header<br><br>")
    (1) end:
        _page.append("<br><br>I'm the footer</body></html>")

CherryClass Root(NormalHeaderAndFooter):
mask:
    def index(self):
            Hello, world !
    def otherPage(self):
            I love cherry pie !
11.1

See ... we got rid of the <py-eval="self.header()"> and <py-eval="self.footer()"> lines ...

Let's see what we can notice from looking at the code:

Here is how it works:

The header for an aspect method has two parts:

(condition) startOrEnd:
startOrEnd can be either the "start" or the "end" keyword. This indicates whether the code should be appended at the start or at the end of the methods.

condition is a python expression used to indicated which method the aspect should apply to. If the condition is always true (as in "1"), then it means that the aspect will apply to all methods of the derived CherryClasses. The python expression can use a special variable called method that contains the following member variables:

Let's go back to our example: let's imagine that we want to use the regular header and footer for both pages index and otherPage, but we want a third page called yetAnotherPage that has its own header and footer. Here is what the code would look like:

CherryClass NormalHeaderAndFooter:
aspect:
    (method.type=='mask' and method.name!='yetAnotherPage') start:
        _page.append("<html><body>I'm the header<br><br>")
    (method.type=='mask' and method.name!='yetAnotherPage') end:
        _page.append("<br><br>I'm the footer</body></html>")

CherryClass Root(NormalHeaderAndFooter):
mask:
    def index(self):
            Hello, world !
    def otherPage(self):
            I love cherry pie !
    def yetAnotherPage(self):
        <html><body bgcolor=red>
            I love cherry pie !
        </body></html>
As we can see, we use the aspect condition to indicate which method the aspect will apply to. Note that the "method.type='mask'" condition is useless, since all methods of Root are masks. So it is only there to show you an example of an aspect condition.

11.3 How is it used in CookieAuthenticate and HttpAuthenticate

Now that you know how it works, you can look at the source code for these two modules and you should be able to understand it.

Basically, when you declare a CherryClass that inherits from CookieAuthenticate or HttpAuthenticate, some code is automatically added at the beginning of each of your masks and views. This code checks if the user is authenticated or not. If not, it returns the login page instead of the regular page ...



Footnotes

... 11.1
This sample code requires CherryPy-0.7 or later to work
See About this document... for information on suggesting changes.