[ckan-dev] IGenshiStreamFilter example

Seb Bacon seb.bacon at okfn.org
Mon Apr 11 12:54:41 UTC 2011


Hi,

In the OKFN CKAN team catchup today, I mentioned that the
ckanext-googleanalytics extension has a good example of using the
IGenshiStreamFilter extension point.  There was a discussion around
the fact it's been done elsewhere.  I just reviewed the other examples
and thought it was worth highlighting how the ckanext-googleanalytics
example is different from the other examples.  Here's some pasted code
so you don't need to refer elsewhere :)

The simple use case [1] is "add a snippet of HTML at a particular place", e.g.

  stream = stream | Transformer('body')\
                .append(HTML('<blink>under construction</blink>'))

That's the one documented in our documentation and what I've seen
implemented elsewhere.

The more complex use case of which the googleanalytics plugin is an
example [2] stores the value of a variable taken from the stream, and
then later inserts something with that variable value:

        def download_adder(stream):
            download_html = ''' <span>
            (downloaded %s times)</span>'''
            count = None
            for mark, (kind, data, pos) in stream:
                if mark and kind == START:
                    href = data[1].get('href')
                    if href:
                        count = dbutil.get_resource_visits_for_url(href)
                if count and mark is EXIT:
                    # emit count
                    yield INSIDE, (TEXT,
                                   HTML(download_html % count),
                                   pos)
                yield mark, (kind, data, pos)

        stream = stream | Transformer(
            '//div[@id="package"]//td/a')\
             .apply(download_adder)

There's also an example of altering an attribute of a tag [2]:

        def js_attr(name, event):
            attrs = event[1][1]
            href = attrs.get('href').encode('utf-8')
            link = '%s%s' % (resource_url,
                             urllib.quote(href))
            js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link
            return js

        stream = stream | Transformer(
            '//div[@id="package"]//td/a')\
            .attr('onclick', js_attr)

I think these examples are worth knowing about as they took me a while
to get right :)

Seb



[1] https://bitbucket.org/okfn/ckanext-datapreview/src/ba7794a1ac37/ckanext/datapreview/__init__.py
[2] https://bitbucket.org/okfn/ckanext-googleanalytics/src/a26e5038fde3/ckanext/googleanalytics/plugin.py




More information about the ckan-dev mailing list