I use json file to store all environment settings.
example of config.json:
{"db_echo": false, "database": "sqlite:///database.db"}
Then I load it like this:
import web, simplejson
config = web.storage(simplejson.load(file('config.json', 'r')))
db = web.database(**parse_rfc1738_args(config.database))
db.printing = config.db_echo
parse_rfc1738_args is a function borrowed from Django and probably
slightly modified by me,
so you can define database connection with one string: "mysql://
user:p...@localhost/database"
import sys, os, time, atexit, random
import urllib, re, cgi
def parse_rfc1738_args(name):
pattern = re.compile(r'''
(?P<dbn>\w+)://
(?:
(?P<user>[^:/]*)
(?::(?P<pw>[^/]*))?
@)?
(?:
(?P<host>[^/:]*)
(?::(?P<port>[^/]*))?
)?
(?:/(?P<db>.*))?
'''
, re.X)
m = pattern.match(name)
if m is not None:
components = m.groupdict()
if components['db'] is not None:
tokens = components['db'].split('?', 2)
components['db'] = tokens[0]
query = (len(tokens) > 1 and dict(cgi.parse_qsl(tokens
[1]))) or None
if query is not None:
query = dict([(k.encode('ascii'), query[k]) for k in
query])
components+= query
if components['pw'] is not None:
components['pw'] = urllib.unquote_plus(components['pw'])
args = dict()
for k, v in components.iteritems():
if v:
args[k] = v
return args
else:
raise Exception("Could not parse rfc1738 URL from string '%s'"
% name)
Then for every environment you can have separate settings
On Sep 19, 1:59 am, Sean <[email protected]> wrote:
> Hi,
>
> I'm trying to test my web.py app, including its database
> functionality. I would like to use a test database for this purpose,
> but it seems like the standard web.py way of doing things is to
> hardcode the database connection in something like a config.py file.
>
> What's the best way to test a web.py app, including its database? How
> do others do this?
>
> Thanks!
> Sean
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---