[ckan-dev] How to load Ckan configuration inside extension?

Eduardo Santos eduardo.edusantos at gmail.com
Thu Jul 5 22:01:01 UTC 2012


Hi everybody,

I'm sure this is a slilly doubt, but I'm working on it for weeks and still
don't know the answer. Maybe the problem is that I'm not the best Python
programmer myself. :)

The problem is: I'm creating a Ckan extension that will run as daemon to
import data from external sources and load it into Ckan as resources. I
know there are a lot of extensions to do it, but the fastest way was to
build my own. I have the following code:


<code>
from logging import getLogger
from sqlalchemy import event
from sqlalchemy import distinct
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.orm import backref, relation

from ckan.lib.base import config
from ckan import model
from ckan.model.meta import *
from ckan.model.domain_object import DomainObject

def setup():

    if datadaemon_table is None or error_table is None:
        define_tables()
        log.debug('Spatial tables defined in memory')

    if model.repo.are_tables_created():

        if not datadaemon_table.exists():
            try:
                datadaemon_table.create()
            except Exception,e:
                # Make sure the table does not remain incorrectly created
                # (eg without geom column or constraints)
                if datadaemon_table.exists():
                    Session.execute('DROP TABLE dt_files')
                    Session.commit()

                raise e

            log.debug('Datadaemon tables created')
        else:
            log.debug('Datadaemon tables already exist')
            # Future migrations go here

    def __repr__(self):
        return "<DataRepository('%s','%s', '%s')>" % (self.hash,
self.original_file, self.creation_date)


class DataRepository(DomainObject):
    """
    The repository will hold information about every uploaded files
    """
    __tablename__ = 'dt_files'

    hash = Column(UnicodeText, primary_key=True,nullable=False)
    original_file = Column(UnicodeText,nullable=False)
    creation_date = Column(DateTime(timezone=True),server_default='now()')

    def __init__(self, hash, original_file, creation_date):
        self.hash = hash
        self.original_file = original_file
        self.creation_date = creation_date
</code>


I'm creating a class that will add some tables to the system. In another
file there's the following code:


<code>
from ckan import plugins
from ckanext.datadaemon.model import setup as setup_model
from ckan.logic import get_action
from ckan import model

 <goog_989364852>
from ckanext.datadaemon.model import setup as setup_model
from ckanext.datadaemon.model import DataRepository

<goog_989364840>
setup_model()
</code>

At that point, when I call setup_model(), I get the following error:

UnboundExecutionError: The MetaData is not bound to an Engine or
Connection.  Execution can not proceed without a database to execute
against.  Either execute with an explicit connection or assign the
MetaData's .bind to enable implicit execution.

It seems like the script is not loading pylons configuration containing
database connections.

Can somebody point me how I can load configuration parameters from ckan
development.ini file?

-- 
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/20120705/6c9b958d/attachment.html>


More information about the ckan-dev mailing list