I had an article published on Kuro5hin recently: Switching from PHP to Zope/Python. Read, enjoy and learn from my mighty wisdom.
Occasionally, when working with computers, you get to experience a completely new way of thinking about problems long thought solved. Rarer still does one find a new set of solutions that are so well thought out and integrated that older approaches seem clumsy by comparison.
Recently I discovered Zope, and having understood its principles I decided to abandon my years of PHP experience in pursuit of a better way.
In my former incarnation as a PHP programmer, I re-invented the wheel most working days. Is there a scripter who has not built classes for basic tasks like database connections or representing content? When I analyzed the time I spent coding, I realized that much of it was spent solving routine problems. I needed was a way to take the routine out of my work.
A wise PHP coder worth his or her wage will make use of standard classes: Pear, Smarty and ADODB provide bigger building blocks with which to construct a project. These do not fundamentally change the way we program - but they do save programmers from some of the excruciating detail. Once you are familiar with these classes it’s not so hard to work out a routine way of joining them together, and perhaps more than a routine ? you build a standard set of libraries for dealing with your day to day work.
What’s so bad about having your own Application server or Content Management System libraries? With them you can construct a number of similar projects. But what about the client who wants something completely different? Do you find a way to extend your system? Do you write something new from scratch or do you find a way to shoe-horn the project’s brief into the capabilities your older code?
The Zen of Zope
Wouldn’t it be nice if there was a sufficiently flexible framework built and maintained by somebody else ? You might use a package like Zope for the same reason that most people use a Linux distribution instead of manually assembling packages from the author’s TGZ files.
For example, nearly every system on the web needs a templating system: Zope provides two contrasting approaches to templates (DTML, ZPT) - despite their differences they both have similar APIs and can be programmed in much the same way. As a result I can use them interchangeably.
Zope also provides a number of data source objects - A standard object called a ‘ZSQL Method‘ can be used to query any database connection and can be combined with either form of template to build a simple database querying and presenting application with almost no programming.
Since the objects are all work together in a standard way, I need not concern myself with how I am going to make everything work together but what exactly I want to do with it. Instead of re-inventing the wheel I get to drive the car.
What is Zope?
Zope is an Object Publishing Environment, a kind of application server that allows you to easily re-combine objects in handy ways to achieve your goal. Everything in Zope is an object, from a document to an error message. At the core of Zope is a simple object request broker - a program that knows how to match objects to user requests and deliver results over an Internet protocol.
Every object in Zope inherits from a few ancestor classes that define the most basic aspects of an object’s API. You know for example that every container object behaves much like any other kind of ‘folderish‘ object. Having understood the general interface of containers the chances are you will be able to use all containers.
These are some of the components that come in the basic Zope distribution:
- ZODB - An object oriented database to store your code and content. Superficially it is like a filesystem - but thanks to a feature called acquisition it can allow objects to appear in more than one location. This bizarre feature allows programmers to quickly call up the most appropriate object based on the ‘context’.
- ZMI - The Zope management interface, a Zope based web application that lets you build more Zope web applications. Developers can manipulate the ZODB by placing and editing Zope objects.
- WebDav / FTP interface - An alternative to the ZMI interface that allows you to continue using your favourite integrated developers environment to program Zope. The ZODB pretends to be a traditional filesystem that allows developers to quickly upload and change content.
- Document and Template Classes - These provide standard ways to represent documents and present data. In Zope 2.x there are two main kinds of template, however in future there are plans to unify both models.
- Data Access Classes - You can access a data-connection class using a ZSQL method. These are sources of data that can be catalogued, filtered and presented by other classes.
- ‘Utility’ and ‘Service’ Classes - These provide a wide range of helper functions, such as error logging, indexing, sending and receiving email.
- Products, Scripts and External Methods - These provide ways to extend Zope using the python programming language. A Product is usually a set of objects with a management interface. Scripts and external methods are more like functions in a procedural language.
Most development does not require access to the host operating system. A Windows Zope server looks identical to a Linux Zope server. Zope is a good option for developers wishing to opt-out of the platform wars.
Zope is more than a replacement for your favorite scripting language, its a completely object oriented environment geared to building better applications.
Persistence
Zope users take for granted things that are harder in scripting languages. For example, persistence of objects and variables.
The usual method of making things persistent is to use a session class, or perhaps a class of your own concoction that writes things that need to persist into a relational database. Once again, Zope does not prevent you from this kind of traditional solution, but as usual it provides a much easier way.
Zope is built around the ZODB, an object oriented database that transparently saves and loads Zope objects. As a result, allowing some data to persist is simple as setting a property or writing an object. No need to worry about SQL, extra classes or database-commits, it just works ‘out of the box’.
Your code can be more reusable
Experienced scriptwriters will usually write their classes in a generic way that can then be sub-classed with configuration information. In this way the class and its configuration can be kept in separate files. This is an effective kludge, but does mean that anybody wishing to use that class needs intimate knowledge of what variables to set.
Once again, Zope does not forbid this traditional scripting technique, but it does allow a more powerful means to define generic components: A Zope Product is the standard way of extending Zope’s behavior.
A product is a self-contained bundle of code that can includes administrative interfaces, user interfaces and the classes that make it work. From the Zope programmers’ point of view, once built, a product is merely something that can be dropped into a container and used much like any other object.
The beauty of this system is that what the Product does is kept in a completely different domain to how a particular instance of the object is used. Re-using code in Zope is usually as simple as placing an object and setting some configuration options on it.
Zope is secure by design
Web applications are infamous for their vulnerabilities; writing good security is hard, and when deadlines are tight, security will often be left until the last minute. As a result, most web applications have poor security.
Zope lets you turn the situation around. It gives you a declarative security model, which makes security an intrinsic feature of anything you build:
As with any object oriented environment you can inherit features of older objects into new objects. Having built an excellent security model, all of the Zope objects can automatically take advantage of it.
You protect Zope objects with permissions, and you grant permissions to users or groups of users; All this can be defined separately from your descriptive object code.
Of course, if you want to invent new types of permission or a new security model, there is nobody to stop you.
Zope makes data easy
I mentioned before that Zope is built around something called the ZODB, an object oriented database. This is a great start for most web applications, which tend to be organized hierarchically. OODBs tend to be more suited to applications where rules governing data frequently have exceptions.
Of course nobody is forcing you store everything as objects. Zope comes bundled with a basic relational database called Gadfly, and with the addition of a Data Adapter (e.g MySQL, Postgres) you can use pretty much any relational database on the market.
People who use scripting offer database abstraction layers to make talking to a database less of a nightmare. PHP has at least two excellent abstraction layers (ADODB and Pear DB). Zope gives you abstraction, plus the ability to define of connection objects and query templates visually.
ZSQL combined with Connection objects allow you to define SQL templates that can be run as a query. Any ZSQL method becomes a source of data. Consequently it can be chained to other objects that know how to process, catalog or display that data.
Zope plays nicely with your existing tools
A basic of Zope’s design is to offer an alternative without forcing you to abandon standard tools and techniques. Contrary to popular belief, you do not have to give up Apache or your favorite editor. You do not have to do everything through a web interface.
Zope’s FTP and WebDav interfaces allow most development environments to connect directly to a Zope-server. This bypasses the web interface, which is not appropriate for large text processing tasks. Macromedia Dreamweaver, Front-Page and KDE Kate can deal with objects in Zope as if they were normal files in a file system.
The Virtual Host Monster provides a safe way for Apache and Zope to co-operate. This utility class allows seamless remapping of Apache virtual hosts to points in the ZODB. It allows you to turn a single Zope-instance into a number of virtual Zope instances accessible via Apache’s mod_proxy.
Summary
Zope is a beautifully integrated set of solutions to common web development problems. It works in a substantially different way to traditionally scripted web-applications. Think of it as a collection of objects help with web publishing rather than a set of scripts to do a job.
Zope objects are designed to work well together. Instead of wondering how best to make one class talk to another you can go ahead and build your application.
By making use of a well developed, stable platform in which the common problems have already been solved, you are free to concentrate on a problem’s unique aspects.
Getting Started
- Downloading Zope - Or use your Linux package manager to download it for you. e.g. Gentoo: just type ‘emerge -uDvp zope’, check your use flags and if everything is good to go ‘emerge -uD zope’ will give you what you need. Debian: ‘apt-get install zope’
- Zope Tutorial (Link is on your Zope Installation welcome screen) - There is a built-in interactive Zope Tutorial which gets you started with some simple tasks using the Zope management interface. To use the tutorial, go to any Folder and select Zope Tutorial from the add list and click the Add button. Provide a name for the tutorial and click Add to begin working with the tutorial.
Getting Help
- #Zope on Freenode (IRC) - A friendly forum that frequently visited by Zope developers.
- #Python on Freenode (IRC) - A group of people who can help you with your python scripting problems.
Other Zope Resources
- The Zope Book - Do not buy any Zope books until you have read THE Zope book. By far the best introduction to Zope development.
- Zope Newbies - A blog to help you through your first few days of Zoping.
- Python Tutorial - Since Zope is a pure Python application, the more you know about it the more you will understand.
- Nobody Expects the Spanish Inquisition - A compact intro to the Python scripting language, courtesy of K5.
Interesting Zope Apps & Products
- Plone - A skinnable content management system built in with Zope and the Content Management Framework.
- Silva - A rival Zope based content management system that makes novel use of XML.
- DocFinder - An automatic documentation system that allows Zope to look within itself to show you how the basic objects and products work.
- Product database - A collection of products that extend Zope’s capabilities.
Praise be unto Altair from #zope for assisting with the proofreading of this document.
0 Response to “Switching from PHP to Zope”