Message from discussion
running scripts using pylons models
Received: by 10.36.23.7 with SMTP id 7mr333207nzw.1183194533885;
Sat, 30 Jun 2007 02:08:53 -0700 (PDT)
Received: by n2g2000hse.googlegroups.com with HTTP;
Sat, 30 Jun 2007 09:08:52 +0000 (UTC)
X-IP: 212.2.99.83
From: WSobczuk <WSobc...@gmail.com>
To: pylons-discuss <pylons-discuss@googlegroups.com>
Subject: Re: running scripts using pylons models
Date: Sat, 30 Jun 2007 09:08:52 -0000
Message-ID: <1183194532.748236.319920@n2g2000hse.googlegroups.com>
In-Reply-To: <4685C56E.3000205@gmail.com>
References: <1183152734.161830.274250@k79g2000hse.googlegroups.com>
<4685C56E.3000205@gmail.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4,gzip(gfe),gzip(gfe)
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-2"
Well, for one thing you didn't do CONFIG.push_process_config(...), so
not everything would work.
For another thing - if you change the pylons DB config then you have
to edit your script and change it there too - so that's evil.
And the third thing is that it took quiet some time to figure this out
for me so I think that if there was a script like this in the project
out of the box - then it could save some people a lot of time and
stress with pylons.
Regards,
On 30 Cze, 04:52, Contact 42 <contactm...@gmail.com> wrote:
> Maybe I'm just not getting it, but this looks really complicated for
> what it's trying to achieve (not the fault of the OP but more so because
> of the paste config stuff).
>
> serioiusly, why not just have
>
> appconfig.py
> dburi = 'postgres://user:password@localhost/db'
>
> database.py
> import appconfig.py
> engine = create_engine(appconfig.dburi)
> # tables defined here
>
> model.py
> import database
> #model classes defined here
> model.meta.connect(database.engine)
>
> Then if you need to use any of this in a script, it's as simple as importing any of these modules into your script. Just plain python. Nothing special.
>
> Huy
>
> > Hello,
>
> > I just wasted around 5 hours trying to figure out how to run a job
> > that would use pylons models/config. I finally did it so I'm
> > submitting this script here and hoping that it will be somehow
> > integrated into pylons and the documentation. I hope that as a result
> > it will save other peoples time and make pylons a better experience.
>
> > Regards,
>
> > from sqlalchemy import *
> > import leisurenow.models as model
> > from paste.deploy import appconfig, config, CONFIG
> > import sys
>
> > config_file = sys.argv[1]
> > package = sys.argv[2]
> > func = sys.argv[3]
>
> > conf = appconfig('config:'+config_file)
> > conf.update(dict(app_conf=conf.local_conf,
> > global_conf=conf.global_conf))
> > CONFIG.push_process_config(conf)
> > if not conf.has_key('sqlalchemy.dburi'):
> > raise KeyError("No sqlalchemy database config found!")
> > print "Connecting to database %s..."%repr(conf['sqlalchemy.dburi'])
> > engine = create_engine(conf['sqlalchemy.dburi'])
> > model.meta.connect(engine)
>
> > pkg = __import__(package, globals(), locals(), [func])
> > getattr(pkg, func)()