How to use spatialite with pyramid

10 views
Skip to first unread message

Michael Lane

unread,
Jul 9, 2019, 5:01:32 PM7/9/19
to pylons-discuss

How to add spatialite to the engine of my pyramid project?


my environment:

Windows 10:
Python version: 3.6.8
Local database: sqlite (for spatialite)
Remote database: postgresql

I find a lot of exemples where spatialite is set in the module parameter for the engine in create_engine using lib like libsqlite3, mod_spatialite, pysqlite3 in my case I use spatialiteinstall with pip (package define in setup.py)

My problem is then I don't have the control of create_engine, in the engine comming from settings in ini so I think that I have to pass the setting by the ini file but I did'nt get it. I didn't find any good example of code using same combination environment as mine (windows, pyramid, spatialite).

There are the settings of ini file:

pyramid.reload_templates = true
pyramid.reload_assets = true
pyramid.debug_authorization = true
pyramid.debug_notfound = false
pyramid.debug_routematch = true
pyramid.default_locale_name = en
pyramid.includes =
    pyramid_debugtoolbar
    pyramid_tm

sqlalchemy.url = sqlite:///%(here)s/risc.sqlite

Code where the engine is create:

from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
import zope.sqlalchemy


# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()


def get_engine(settings, prefix='sqlalchemy.'):
    return engine_from_config(settings, prefix)


def get_session_factory(engine):
    factory = sessionmaker()
    factory.configure(bind=engine)
    return factory


def get_tm_session(session_factory, transaction_manager):
    dbsession = session_factory()
    zope.sqlalchemy.register(dbsession, transaction_manager=transaction_manager)
    return dbsession


def includeme(config):
    settings = config.get_settings()
    settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'

    # use pyramid_tm to hook the transaction lifecycle to the request
    config.include('pyramid_tm')

    # use pyramid_retry to retry a request when transient exceptions occur
    config.include('pyramid_retry')

    session_factory = get_session_factory(get_engine(settings))
    config.registry['dbsession_factory'] = session_factory

    # make request.dbsession available for use in Pyramid
    config.add_request_method(
        # r.tm is the transaction manager used by pyramid_tm
    lambda r: get_tm_session(session_factory, r.tm),
    'dbsession',
    reify=True
)

And the settings module of setup.py

requires = [
    'bcrypt',
    'docutils',
    'plaster_pastedeploy',
    'pyramid',
    'pyramid_debugtoolbar',
    'pyramid_retry',
    'pyramid_tm',
    'sqlalchemy',
    'transaction',
    'zope.sqlalchemy',
    'waitress',
    'sphinx',
    'flake8',
    'pyLint',
    'pg8000',
    'geoalchemy2',
    'spatialite'
]

I'm pretty sure than I miss a little something, but I'm not able to find it my way. I little hint will be appreciated on this one.

Thanks

Reply all
Reply to author
Forward
0 new messages