I don't need all of the bells and whistles of pylons, just the SQLAlchemy stuff. Luckily, there are some convenience functions that seem to have been made to deal with a situation just like this one. Here's the code:
from paste.deploy import appconfig
from sqlalchemy import engine_from_config
from myapp.model import init_model
if len(sys.argv) != 2:
print 'Usage: Need to specify config file.'
config_filename = sys.argv
config = appconfig('config:%s' % config_filename, relative_to='.')
engine = engine_from_config(config)
Of course, replace "myapp" with the name of your application.
Getting the configuration filename from the command-line arguments could of course be put somewhere else, so I'll just skip talking about that. The two main functions are appconfig and engine_from_config.
appconfig is from paste.deploy, which reads the configuration file and returns a dictionary-like object. engine_from_config is from SQLAlchemy and takes a dictionary-like object, retrieves SQLAlchemy-specific configurations, and uses the configurations to create an engine object.
Post a Comment