mike watkins dot ca : May 2007 Archives

May 2007 Archives

5 entries filed this month:

May 31 2007

Python Web Application Diary

May 31, 2007

I have a number of tutorial projects in various stages of completion, and I'm itching to put some of this stuff out there in the hope that its useful to someone.

It occurred to me that I could do a project or two and document them along the way, tutorial style, in order to present one or more tutorials on writing web applications with QP, a stable but relatively unknown web application framework from the same folks who brought Quixote to Python.

I'm going to try something a little different than the typical blog in 20 minutes style tutorial and instead try to take readers along a real-world path as I document what I do.

This tutorial isn't intended to be the last word on web development or Python or even on using QP and related bits, but hopefully it will be accessible enough for new Python users, while remaining informative and brief enough for those with more experience.

I'm not even going to delve into the question "Why QP and not TurboGears or Django or Zope?", except to simply say that QP, like its older cousin Quixote, provides an easy to use and understand framework which makes publishing your Python objects to the web simple. Quixote and QP have been around a long time, QP somewhat less so but acquires its maturity by sharing the principal publish/request and resource representation architecture of its older cousin, Quixote. QP also shares a philosophical constraint: no magic.

If you are looking for a tool set that has every single size of screwdriver ever produced, then a tool like QP might not appeal to you. But if you are looking for an easy to understand and work with web application development framework that you can more or less keep in your head, QP is worth a close look. If you are also a ZODB or object database aficionado, or would like to see what the object database world is like, then QP and Durus are a compelling duo.

The Project

The project I have in mind is to redevelop the blog software that powers my own personal site, http://mikewatkins.ca/. You are soaking in it right now, if you are reading this in early June 2007.

Why bother? While there are many blog applications out there, what I want at the end of this are a set of objects and UI components that can be integrated into other QP applications.

I've wanted to move off of PyBlosxom for some time now, but with something else always on my plate, doing so has never been a priority for me. Its the prospect of killing two birds with one stone -- wiring up a simple web log or journal, and documenting it tutorial style -- that is pushing me forward now.

Before Going Further

QP's developers rather unabashedly adopted some preferences which are built into the system and its design. It was designed to run on Unix-like systems. While some enterprising people have looked at running it on Windows, if you are a Windows-only or Windows-sometime developer, you might want to search and read the QP mailing list archives first before venturing forth.

First Steps

Today's installment for readers is to install QP and related components. But before we get there, first a word on my environment so that subsequent notes make sense.

My development, test and production environments have similar file system layouts and installed software. Of note to us in this tutorial you'll want:

  • Python 2.5.x installed
  • Python Imaging Library (PIL)
  • HTMLTidy (will be used eventually)
  • Perhaps a component or two more as we go through this.

I install all Python related tools into the default locations; tools and libraries and non-QP applications that I write are located in:

/www/lib - added to PYTHONPATH (in your .profile or .cshrc)

QP by default looks along certain paths for applications to manage (start, stop, restart, interact with, view log). This allows you to have user-space apps which QP can see with no re-installation of the base software, as well as system or privileged user applications. First lets look at the command line help:

%qp -h
Usage: qp [options] [start|stop|restart] [site]*

Control the servers for the sites in your qp installation.
No options is the same as "--status".
"stop" is the same as "--stop".
"start" is the same as "--start".
"restart" is the same as "--stop --start".

If "--stop" and "--start" are both present and the durus server
is already running, only the web server is restarted.

The script searches for sites in the qp.sites package, which
in turn searches through the following list, by default.
    ${QP_SITES}
    ~/qp_sites
    ~/.qp_sites
    /var/qp_sites
    /usr/local/lib/python2.5/site-packages/qp/sites

Options:
  -h, --help           show this help message and exit
  -u, --start          Start the durus and web servers.
  -d, --stop           Stop the durus and web servers.
  -q, --quickrestart   Quick restart of the web server(s) of the named sites.
  -v, --status         Show the current status of the durus and web servers.
  -c, --configuration  Show the site configuration.
  -l, --log            tail -f the log for the last named site.
  -i, --interact       Open an interactive session with the last named site.

QP applications other than demo apps or quick one-offs or test apps go:

/www/qp_sites

And I've created a symlink in /var that points to qp_sites:

cd /var
ln -s /www/qp_sites qp_sites

I put demo apps / quick one-offs / test apps in:

~/qp_sites

Also under /www for convenience:

/www/docs - a collection of documentation that I refer to
/www/python - symlink to /usr/local/lib/python2.5/

All the library work I do is contained in a Python package called "parlez". The development files live in /www/lib/Parlez, and a symlink to its lib subdir is created in /www/lib. Parlez contains:

/www/lib/Parlez         - including CHANGES, LICENSE, README
/www/lib/Parlez/bin     - related scripts
/www/lib/Parlez/doc     - documentation
/www/lib/Parlez/lib     - the Parlez package itself
/www/lib/Parlez/site    - a demo application

In /www/lib I create a symlink:

@ln -s /www/lib/Parlez/lib parlez@

Install QP and related packages

We want to install Durus, an object database closely related to QP but which can be used quite independently of QP, as well as QPY, QP, and Dulcinea. More on each of these later. Install them now, in the following order:

  • Durus - a Python object database similar to ZODB
  • QPY - Python-centric "templating" or if you prefer, untemplating
  • QP - A web framework for easy publishing of "objects" to the web
  • Dulcinea - pre-made components that work with QP and Durus
  • Sancho - A unit testing suite

All of the software components listed above are available at http://www.mems-exchange.org/software/ under an open source license.

Once installed, run and play with one or more of the three QP demo applications provided:

qp proto start

or start proto like this:

qp -u proto

See what's running by issuing qp with no parameters:

%qp

proto  durus[61859]:localhost:7002 web[61860] http::8002 as_https:localhost:9002 https:localhost:10002
sr     durus:down web:down
recipe durus[60590]:localhost:7005 web[60591] http::8005
contact web[54418] http::8080
test   web:down
hello  web:down

And then visit http://localhost:8002/ to explore the prototype application.

Once you've looked at the basic QP demos and peeked at some of the underlying code to grasp how simple this all is going to be, then see what Dulcinea brings to the table by way of pre-made components and user interface bits. In your ~/qp_sites subdirectory, create a link to the Dulcinea demo application and run that.

In our next installment, we'll create some basic objects, introduce Sancho unit testing, and write a bloxsom to "Parlez" conversion script so we have some real data to work with going forward.

May 18 2007

Guess who?

Try and guess which political ideology the speaker of the following words belongs to:

It makes no sense to push ahead and spend all of this money on a port that makes the communities adjacent to it little more than doormats on which passing freight wipes its feet and leaves nothing but misery and disruption behind. [When] our concern today is so rightly focused on reducing pollution, alleviating traffic congestion, protecting the environment and other quality-of-life concerns, we shouldn’t be spending money on massive projects that ignore these concerns.

Totally agree with these words. Who said it?

Surprise: Delta-Richmond East Conservative MP John Cummings.

Its too bad that the only Conservative MP’s who are willing to talk common sense are those who are clearly out of favour with Stephen Harper and his political hit men.

The vast majority of Conservative MP’s toe the party line, whether it makes any sense or not. The vast majority of party members seem to believe that is a sensible thing to do. Wrong headed in the extreme. Policy is being driven by a very few in the PMO, not by the membership and certainly not by the average Canadian voter.

Gateway is one idea that makes no sense whatsoever. Gateway, David Emerson’s pet project, only serves to prove that the Conservative party has no intention of actually addressing greenhouse gas emission reduction and climate change.

I’m glad to see Cummings continue to speak out. I won’t always agree with him, but I want to hear what MPs really think, not what they were ordered to think and say by Harper.

May 08 2007

Emergency Preparedness Week - May 6-12

Its EP Week, don’t ya know. What’s that? Its an annual reminder that we all should be personally prepared – at home, at work, and in-between, to survive a significant regional disaster on our own without the help of emergency services for at least a week.

It takes very little planning and forethought to make sure you have enough water and food on hand, and other basic essentials, to survive a week without power or additional groceries or running tap water. Yet despite how easy it is to prepare, few are prepared.

Think back to the residents of New Orleans two summers ago and then consider what Vancouver would be like after a 7.0 or 8+ magnitude earthquake, and you would more or less correctly envision the situation here in Vancouver – access to the city cut off, supplies limited and scarce within hours.

A big quake (or an industrial spill or power outage or…) Could happen 10 minutes from now. So start preparing.

Live in Vancouver? Attend one of these free emergency preparedness “how-to” sessions (to register, call the community centre):

  • Tuesday, May 8 at 7 pm, West End Community Centre (604) 257–8333
  • Wednesday, May 9 at 7 pm, Renfrew Community Centre (604) 257–8388
  • Saturday May 12 at 10 am, Trout Lake Community Centre (604) 257–6955
  • Saturday May 19 at 2 pm, West End Community Centre (604) 257–8333
  • Wednesday May 16 at 7 pm, Dunbar Community Center (604) 222–6060
  • Wednesday June 6 at 7 pm, Kits Community Centre (604) 257–6976

Resources:

May 02 2007

QP goes 2.0

QP, a close-cousin to the Quixote web framework, is now at 2.0 release:

The MEMS Exchange released updates of QP and Qpy today. We also released updates of the Durus, Dulcinea, and Sancho packages. More detail about all of these updates can be found at http://www.mems-exchange.org/software/ (via QP mailing list)

Misdirection 101

When trying to pull off a trick its often helpful to distract the audience and lead their eye away from where the action is really happening.

Harper ‘fanning flames’ of controversy, says May

May has been in the headlines for comments she made on the weekend that compared the government’s approach to climate change to former British prime minister Neville Chamberlain’s appeasement of the Nazis.

Speaking on CBC Newsworld, May said the comparison was not her own, but came from British journalist and author George Monbiot.

May said she included it in her address in order to illustrate international opinion of Canada.

“In citing that, what I was saying was, ‘Look how far Canada’s reputation has fallen, look at how the world is now looking at us for violating our international commitments on Kyoto,’” she said.

May said she left out a portion of Monbiot’s article, in which he says U.S. President George W. Bush, Australian Prime Minister John Howard and Harper will be remembered as the new “axis of evil” for failing to meet the environmental challenges of the day.

I don’t disagree with Monbiot at all there, but Paul Martin, Jean Chretien, and Brian Mulroney ought not to be left out of the reckoning. Each has had their own hand in shaping Canadian policy on climate change since the late 1980’s, and each has failed to measure up. Mulroney allowed his environment minister Lucien Bouchard to renege on earlier promises to seek reductions. Bouchard of course went on to form a separatist party whose goal is to break up Canada. Chretien signed treaties and promised lots but never empowered any of his environment and industry ministers, nor allowed parliament, to make the necessary policy changes. Martin diddled on every file.

Stephen Harper, now boxed in by the current climate of awareness, is trying to appear to be doing all that he can without actually trying to do all that is required. Its a sales strategy more than a campaign to save the planet. And all these “leaders” have continued to plug globalism (one of the root causes of the acceleration of greenhouse gas production) as they only way to conduct the business of the world, as if there is no other alternative.

Harper isn’t the only environmental obstructionist, he’s just the latest to hold power. Add to him the former Canadian Alliance and Reform party leaders Stockwell Day and Preston Manning and you have a triumvirate of Alberta-centric leaders who have consistently demonstrated in Parliament and out that their only interest over two decades has been to obstruct any move to reduce carbon dioxide and other greenhouse gas emissions.