(Harmonious is a web framework I’m working on for my degree, see previous blog entries for more info)
So after mulling over various problems (I’m in no hurry so I’m not forcing myself to make decisions about design issues on the spot. Instead I’m letting them sit in my head for however long it takes for the best solution to make itself known) related to implementing Aspect-Oriented Programming techniques elegantly, I’ve finally come to a conclusion and laid out the basic code needed.
So, wtf? It goes like this…
URLs are mapped to methods (see previous blog entries) and these methods can be decorated. The new ‘aspect’ decorator lets you decorate your method with, umm.. aspects. I like to call them Operational Aspects, as they relate to the various aspects of operation required to process a request.
e.g:
@public
@aspect(MyAspect)
def mypage(self):
return “hi!”
The aspects to which you decorate your method with are grouped, this is so that the framework knows what to expect from the aspect and what the aspect is expecting from the framework. There are also 3 execution modes: before your method is executed, after and in parallel with your method.
The reasons for implementing this all stem from my ponderings about how best to implement CRUD (Create Read Update Delete) support. My main qualm was that your method may only want the framework to handle some of these operations for you, the others you may want to handle yourself. So instead of an all or nothing situation you can now decorate your method with aspects (operations) of the CRUD model 🙂
Creating custom aspects is pretty easy too, here is a simple aspect:
class MyAspect:
class aspectmeta:
execution = AspectExecution.AFTER
keep_instance = True # Cache an instance of this aspect as we’re going to use it for more than one execution mode.
returns = AspectKeyword.RESPONSE
group = AspectGroup.OUTPUT
def __init__(self, harm, response):
self.harm = harm
self.response = response
def run(self, event):
notice(“MyAspect got event %s:” % event)
self.response.set_header(‘Status’, ‘groovy’)
return response
As you can probably tell, this aspect is executed after the requested method is executed and it returns a Response object. The framework determines what arguments __init__ accepts using introspection and supplies them from the current scope, most things will be imported though.
There are a number of things these aspects are going to make implementing really easy, input/output filters are pretty much a done job already 🙂
By the by, I’m looking for a new name. The reason being that harmonious.(org,net,com) are all taken 🙁
b2evo’s code tag sucks!
Ciao

