Cork

Cork is a tool for quickly building simple applications. It defines a free-form object model that can be serialized as YAML documents with embedded Python code, and has built-in support for dispatching WSGI requests.

Simple applications

I find myself wanting to build quick applications to solve a specific problem, for example generating a monthly, printed, calendar-style timesheet based on entries in a text file. I don't want to worry about defining a schema for my data, I don't want to think about Cocoa or GTK widgets, the only thing I should worry about is generating the timesheet report as HTML. Oh, and I want the timesheet displayed in my web browser, so I can quickly review and print it.

So what I want is a tool that helpes solve two problems – data persistence and user interface – and make it painless to integrate the application logic.

Data model

I love text files: you don't need special tools to work with them (well, unless you're not familiar with source code, but then you wouldn't go about writing your own applications, would you?) and, if used cleverly, the contents is self-documenting. It's no coincidence that we use text files for a lot of different things: data exchange (XML), presentation (HTML), configuration files (look inside /etc on any unix system), program source code.

I'm also a big fan of free-form data structures, like the ZODB database, or a (conformant) HTML document. You don't need a rigid pre-defined schema; as long as your names don't collide with someone else's, everything just works. So Cork's data model is open and flexible: there's an object namespace, much like a filesystem, addressed by path (and implemented as files in folders on disk); objects are YAML documents, parsed with an off-the-shelf library (pyyaml), with a handful of custom tags for things like object methods and inter-object links. And that's it.

User interface

Practically every computer has a web browser, and writing web pages is really easy, so it makes sense to use this as UI. Python has this insanely simple protocol for plugging web server pieces, WSGI, and Cork makes it easy to provide a WSGI service. It dispatches requests based on the URL path, so all the application has to do is generate response documents from Python code.

Plug this into a WSGI-compliant HTTP server, like wsgiref.simple_server (part of the Python standard library), and presto, you have a working web application. I use a simple script, inetd_wsgi, that invokes simple_server from launchd, so I don't have to worry about the housekeeping of a persistent server process.

The vision

Right now Cork only does basic stuff. It can read data (no write support yet), there are no friendly request/response objects (you have to speak WSGI if you want to generate web pages – no big deal but you shouldn't have to), and hopefully there will be support for jQuery-style selectors, to easily get a hold of data. Some javascript support, in the form a jQuery plugin, might come in handy too. The idea is to implement as much of the generic-use plumbing as possible in Cork, to let application writers focus on their application.

The project is hosted on Github (github.com/alex-morega/cork) and there are some doctests to show how it works. I'd love to hear from you if you find the idea worthwile, or if you think I'm doing something wrong. Or if, worse, you use it yourself. :)

Created:
26 Mar 2009, 19:40
« previous
(inetd + wsgi)
next »
(Before PyCon 2009)