[ckan-dev] How to load Ckan configuration inside extension?
Eduardo Santos
eduardo.edusantos at gmail.com
Wed Jul 11 13:18:03 UTC 2012
Hi Sean,
Thank you for these info. I'll try to adapt it to run as a paster command.
This tutorial is exactly what I nedded.
2012/7/11 Sean Hammond <sean.hammond at okfn.org>
> > Hi Sean,
> >
> > Thank you for the answer. Yes, I've seen this extension, but I had very
> > specific requirements and only a few time, and it would take me more
> > time to know this extension than to develop a new one.
> >
> > I'm going to share my code, but I don't know if it's going to be useful,
> > because there's a lot of other extensions to achieve the same thing. I
> > just can't release it now because there's a lot of client-specific
> > things that would not only be broken, but it would also not fit any
> > other environment. I'll move this code to another extension and release
> > the other work. It basically does this:
> >
> > 1 - Monitor one specific directory in the computer for changes using
> > pyinotify;
> > 2 - When there's a new file detected, send it to be processed;
> > 3 - The file is a zip data collection with self contained data records,
> > also zipped. A model very similar to ODF, but much more simple;
> > 4 - Parse one RDF record inside the zip record and insert it into Ckan
> > as a resource;
> > 5 - Store resource metadata on Elastic Search;
> > 6 - Store binary files in OFS;
> > 7 - keep a log of all transactions, even parsing errors.
> >
> > As you can see, it's really a lot of operations, and the code is still a
> > mess, so I don't know if it's going to be useful.
> >
> > Anyway, the problem persists. As the loading script run as a daemon, the
> > only way I found to parse the .ini file is with the following code:
> >
> > import ConfigParser
> > config = ConfigParser.ConfigParser()
> >
> > # Find config file
> > config_file = os.environ.get('CKAN_CONFIG')
> > if not config_file:
> > config_file = os.path.join(
> > os.path.dirname(os.path.abspath(__file__)),
> > '../../../ckan/development.ini')
> >
> > config.read(config_file)
> >
> > # Load DB connection
> > from sqlalchemy import create_engine
> > engine = create_engine(config.get('app:main','sqlalchemy.url'))
> > model.metadata.bind = engine
> >
> > I'm sure it's not the best implementation, and it would help me very
> > much if you could point me a better way to do it.
>
> Hi Eduardo, CKAN plugins can read and edit CKAN's configuration by
> implementing the IConfigurer and/or IConfigurable plugin interfaces:
>
> http://docs.ckan.org/en/ckan-1.7.1/writing-extensions.html
>
> But that only helps if you're writing an extension, won't work for
> standalone scripts.
>
> You can implement your script as a CKAN extension (and run it from the
> paster command) using CkanCommand, and this way you can import
> pylons.config and access the config that way. That's slightly better as
> you don't have to write your own code to find and parse the config file.
>
> For an example of implementing a CKAN paster command see:
>
> https://github.com/seanh/ckanext-examplevocabs
>
> The new command is registered in the extension's setup.py:
>
> https://github.com/seanh/ckanext-examplevocabs/blob/master/setup.py#L29
>
> and then is implemented in the VocabsCommand class:
>
>
> https://github.com/seanh/ckanext-examplevocabs/blob/master/ckanext/examplevocabs/plugin.py#L11
>
> This example doesn't actually read the CKAN config, but you could read
> it by adding these lines to the command() method:
>
> import pylons.config
> self._load_config()
> site_title = pylons.config['ckan.site_title']
>
> If your script really is a standalone script and not a CKAN extension at
> all, then I'm not sure if anything would work other than finding and
> parsing the ini file yourself with ConfigParser.
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
>
--
Eduardo Santos
Analista de Sistemas
http://www.eduardosan.com
http://twitter.com/eduardosan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20120711/47310739/attachment-0001.html>
More information about the ckan-dev
mailing list