Personal tools
You are here: Home Members rea SLUG Talks Genshi Talk
Document Actions

Genshi Talk

by Robert Anderson last modified 2008-05-12 19:42

XML processing using Genshi templates in Python to output GoogleEarth KML files from live APRS data feeds.

Lets say you want to programatically output something in XML.

Here are three example approaches to generating XHTML in Python

All of the following assume these variable settings:

        title='My Example'
        text='<B>This</B> example is boring.'

  1. Directly printout the desired XHTML :
            print '<XHTML><BODY><H1>', title, '</H1>', text ,'</BODY></XHTML>'
    
  2. Using an XML library like ElementTree :
            from elementtree.SimpleXMLWriter import XMLWriter
            import sys
            w = XMLWriter(sys.stdout)
            html = w.start("html")
            w.start("body")
            w.element("h1", title)
            w.data(text)
            w.close(html)
    
  3. With a templating language like Genshi :
            <xhtml><body><h1 py:content="title">Ex Title</h1>${text}</body></xhtml>
    

Which one is better?

Some goals (our goals?):

  1. Easy to read
  2. Re-useable
  3. Easy to maintain
  4. Be able to use syntax checking tools

My Example

For awhile now I've taken motorcycle trips, then downloaded my gps data and translated the "track" files into KML for overlaying onto GoogleMaps. A few times I've even gone through the KML adding extra information. I've changed multi day trips so that each days route appears in different colors. I've even created little photo icons that when clicked pop up thumbnail photos of the location and contain links to the full digital photo. It seemed like the next step was to automatically process my track files and photos into KML script that could do it all automatically.

Well that isn't where I ended up starting. After learning about APRS, Tucker went out and got his Ham Radio license. APRS is an amature radio service that accepts packets containing GPS position information and some small amount of data.k These packets are stored for a few days before being flushed. Before being flushed it's possible to retrieve the packets over the Internet. That gave me the idea to write a python script to pull that data and write out my own KML. After some web searches I stumbled upon a best practices example using a templating prodcut called Genshi.

Programmers always like to make tools. By making my Genshi template, very flexible, I hope to reuse much of it on other projects. Eventually the scripts evolved into a small script, a library and the Genshi template. This early example shows the power of these techniques. We may also be able to extend this simple start to do more.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: