From 1f0f25a26f0a6cd89fe69c27337a7a14671635e0 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Mon, 4 Apr 2011 11:50:18 +0100 Subject: [PATCH] move plugin to own module --- ckanext/googleanalytics/__init__.py | 69 +---------------------------- ckanext/googleanalytics/plugin.py | 62 ++++++++++++++++++++++++++ setup.py | 2 +- 3 files changed, 64 insertions(+), 69 deletions(-) create mode 100644 ckanext/googleanalytics/plugin.py diff --git a/ckanext/googleanalytics/__init__.py b/ckanext/googleanalytics/__init__.py index 39f1299..792d600 100644 --- a/ckanext/googleanalytics/__init__.py +++ b/ckanext/googleanalytics/__init__.py @@ -1,68 +1 @@ -import logging -import urllib - -log = logging.getLogger(__name__) -import os -from genshi.filters import Transformer -from genshi import HTML -from ckan.plugins import implements, SingletonPlugin -from ckan.plugins import IGenshiStreamFilter, IConfigurable, IRoutes -from ckan.plugins import IConfigurer -from gasnippet import gacode - - -class GoogleAnalyticsException(Exception): - pass - - -class GoogleAnalyticsPlugin(SingletonPlugin): - implements(IConfigurable, inherit=True) - implements(IGenshiStreamFilter, inherit=True) - implements(IRoutes, inherit=True) - implements(IConfigurer, inherit=True) - - def configure(self, config): - self.config = config - log.info("Loading Google Analytics plugin...") - if (not 'googleanalytics.id' in config): - msg = "Missing googleanalytics.id in config" - raise GoogleAnalyticsException(msg) - - def filter(self, stream): - """ - Required to implement IGenshiStreamFilter; will apply some HTML - transformations to the page currently rendered. Depends on Pylons - global objects, how can this be fixed without obscuring the - inteface? - """ - - log.info("Inserting GA code into template") - ga_id = self.config['googleanalytics.id'] - code = HTML(gacode % ga_id) - stream = stream | Transformer('head').append(code) - - # add download tracking link - def js_attr(name, event): - attrs = event[1][1] - link = '/downloads/%s' % urllib.quote(attrs.get('href')) - js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link - return js - stream = stream | Transformer( - '//div[@id="package"]//td/a')\ - .attr('onclick', js_attr) - - return stream - - def after_map(self, map): - map.connect('analytics', '/analytics/package/top', - controller='ckanext.googleanalytics.controller:GAController', - action='view') - return map - - def update_config(self, config): - here = os.path.dirname(__file__) - rootdir = os.path.dirname(os.path.dirname(here)) - template_dir = os.path.join(rootdir, 'ckanext', - 'googleanalytics', 'templates') - config['extra_template_paths'] = ','.join([template_dir, - config.get('extra_template_paths', '')]) +# diff --git a/ckanext/googleanalytics/plugin.py b/ckanext/googleanalytics/plugin.py new file mode 100644 index 0000000..e6b957d --- /dev/null +++ b/ckanext/googleanalytics/plugin.py @@ -0,0 +1,62 @@ +import logging +import urllib + +log = logging.getLogger(__name__) +import os +from genshi.filters import Transformer +from genshi import HTML +from ckan.plugins import implements, SingletonPlugin +from ckan.plugins import IGenshiStreamFilter, IConfigurable, IRoutes +from ckan.plugins import IConfigurer +from ckan import model +from gasnippet import gacode + + +class GoogleAnalyticsException(Exception): + pass + + +class GoogleAnalyticsPlugin(SingletonPlugin): + implements(IConfigurable, inherit=True) + implements(IGenshiStreamFilter, inherit=True) + implements(IRoutes, inherit=True) + implements(IConfigurer, inherit=True) + + def configure(self, config): + self.config = config + log.info("Loading Google Analytics plugin...") + if (not 'googleanalytics.id' in config): + msg = "Missing googleanalytics.id in config" + raise GoogleAnalyticsException(msg) + + def filter(self, stream): + log.info("Inserting GA code into template") + ga_id = self.config['googleanalytics.id'] + code = HTML(gacode % ga_id) + stream = stream | Transformer('head').append(code) + + # add download tracking link + def js_attr(name, event): + attrs = event[1][1] + link = '/downloads/%s' % urllib.quote(attrs.get('href')) + js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link + return js + stream = stream | Transformer( + '//div[@id="package"]//td/a')\ + .attr('onclick', js_attr) + + return stream + + def after_map(self, map): + map.connect('analytics', '/analytics/package/top', + controller='ckanext.googleanalytics.controller:GAController', + action='view') + return map + + def update_config(self, config): + here = os.path.dirname(__file__) + rootdir = os.path.dirname(os.path.dirname(here)) + template_dir = os.path.join(rootdir, 'ckanext', + 'googleanalytics', 'templates') + config['extra_template_paths'] = ','.join([template_dir, + config.get('extra_template_paths', '')]) diff --git a/setup.py b/setup.py index 8419bd2..972060c 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,6 @@ setup( """ [ckan.plugins] # Add plugins here, eg - googleanalytics=ckanext.googleanalytics:GoogleAnalyticsPlugin + googleanalytics=ckanext.googleanalytics.plugin:GoogleAnalyticsPlugin """, )