[Mpuls-commits] r2659 - in wasko/branches/2.0: . mpulsweb/lib
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed May 5 15:08:54 CEST 2010
Author: torsten
Date: 2010-05-05 15:08:53 +0200 (Wed, 05 May 2010)
New Revision: 2659
Modified:
wasko/branches/2.0/ChangeLog
wasko/branches/2.0/development.ini
wasko/branches/2.0/mpulsweb/lib/app_globals.py
wasko/branches/2.0/mpulsweb/lib/base.py
wasko/branches/2.0/mpulsweb/lib/config.py
Log:
Added config based dbmapping
Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog 2010-05-05 13:07:06 UTC (rev 2658)
+++ wasko/branches/2.0/ChangeLog 2010-05-05 13:08:53 UTC (rev 2659)
@@ -1,3 +1,14 @@
+2010-05-05 Torsten Irländer <torsten.irlaender at intevation.de>
+
+ Added config based db-mapping:
+
+ * default_mapping.json,
+ development.ini,
+ mpulsweb/lib/base.py,
+ mpulsweb/lib/base.py,
+ mpulsweb/lib/app_globals.py: DB-Mapping is now loaded from a json
+ based config file. Config file can be configured in ini file.
+
2010-05-05 Roland Geider <roland.geider at intevation.de>
* mpulsweb/i18n/mpulsweb.pot:
Modified: wasko/branches/2.0/development.ini
===================================================================
--- wasko/branches/2.0/development.ini 2010-05-05 13:07:06 UTC (rev 2658)
+++ wasko/branches/2.0/development.ini 2010-05-05 13:08:53 UTC (rev 2659)
@@ -23,7 +23,8 @@
# Where is the config file for this application. Config is used to define
# which moduls are enabled or which phases are present.
-mpuls.app.config=%(here)s/default.json
+mpuls.app.config=%(here)s/default_config.json
+mpuls.app.dbmapping=%(here)s/default_dbmapping.json
# What is the name of the application instance. This is the name of the
# directory containing the instance specific controller, templates and model.
mpuls.app.instance=mpulsweb
Modified: wasko/branches/2.0/mpulsweb/lib/app_globals.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/app_globals.py 2010-05-05 13:07:06 UTC (rev 2658)
+++ wasko/branches/2.0/mpulsweb/lib/app_globals.py 2010-05-05 13:08:53 UTC (rev 2659)
@@ -34,7 +34,7 @@
from formed.meta.io.document import openDocument
from mpulsweb.lib.timelog import setupTimeLogging
-from mpulsweb.lib.config import MpulsConfig
+from mpulsweb.lib.config import MpulsAppConfig, MpulsDBMapping
from mpulsweb.lib.security import load_db_mapping_from_file
from mpulsweb.model.annotations import AnnotationsProvider
@@ -54,13 +54,18 @@
variable
"""
# Load mpuls configuration
- config_file = config.get('mpuls.app.config', 'default.json')
+ config_file = config.get('mpuls.app.config', 'default_config.json')
+ mapping_file = config.get('mpuls.app.dbmapping', 'default_dbmapping.json')
if not os.path.isabs(config_file):
root = config.get('pylons.paths').get('root')
config_file = os.path.join(root, '..', config_file)
+ if not os.path.isabs(mapping_file):
+ root = config.get('pylons.paths').get('root')
+ mapping_file = os.path.join(root, '..', mapping_file)
self.config_path = os.path.dirname(config_file)
- self.mpuls_config = MpulsConfig(config_file)
+ self.mpuls_config = MpulsAppConfig(config_file)
+ self.mpuls_dbmapping = MpulsDBMapping(mapping_file)
# Load formed from defined location. If no location is defined. Take
# the default which is the formed dir under public.
Modified: wasko/branches/2.0/mpulsweb/lib/base.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/base.py 2010-05-05 13:07:06 UTC (rev 2658)
+++ wasko/branches/2.0/mpulsweb/lib/base.py 2010-05-05 13:08:53 UTC (rev 2659)
@@ -137,13 +137,29 @@
# If not then redirect the user to the login page
try:
user = session['USER_AUTHORIZED']
- dbname = getDbName()
+ origdbname = getDbName()
- self.db = db.DB(dbname=config.get('mpuls.db.database') % dbname,
- host=config.get('mpuls.db.host'),
- port=config.get('mpuls.db.port'),
- user=config.get('mpuls.db.user') % (dbname,
- user.login),
+ # Load dbmapping
+ mapping = g.mpuls_dbmapping.get_mapping()
+ if mapping.has_key(origdbname):
+ log.debug('mapping: %s' % mapping.get(origdbname))
+ dbname = mapping.get(origdbname).get('name')
+ dbhost = mapping.get(origdbname).get('host')
+ dbport = mapping.get(origdbname).get('port')
+ userschema = mapping.get(origdbname).get('userschema')
+ dbschema = mapping.get(origdbname).get('dbschema')
+ else:
+ log.debug('not found %s in mapping' % origdbname)
+ dbname = origdbname
+ dbhost = config.get('mpuls.db.host')
+ dbport = config.get('mpuls.db.port')
+ userschema = config.get('mpuls.db.user')
+ dbschema = config.get('mpuls.db.database')
+
+ self.db = db.DB(dbname=dbschema% dbname,
+ host=dbhost,
+ port=dbport,
+ user=userschema % (dbname, user.login),
password=user.password)
db.enter(self.db)
Modified: wasko/branches/2.0/mpulsweb/lib/config.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/config.py 2010-05-05 13:07:06 UTC (rev 2658)
+++ wasko/branches/2.0/mpulsweb/lib/config.py 2010-05-05 13:08:53 UTC (rev 2659)
@@ -69,12 +69,10 @@
"""Dict-equivalent for default values that should not be merged recursively.
"""
-
class MpulsConfig(object):
def __init__(self, filename=None):
self.config = self.build_defaults()
-
if filename and os.path.isfile(filename):
f = open(filename, "rb")
try:
@@ -101,7 +99,31 @@
section = {}
sections[section_key] = section
section[item_key] = item
+ return sections
+ def get(self, section, name):
+ try:
+ return self.config[section][name]
+ except KeyError:
+ log.exception('Error on checking config (%s, %s)' % (section, name))
+ return ''
+
+class MpulsAppConfig(MpulsConfig):
+
+ def build_defaults(self):
+ '''Set default configuration. This is function should be calles if
+ there is no config file'''
+
+ sections = {}
+
+ def set(section_key, item_key, item):
+ try:
+ section = sections[section_key]
+ except KeyError:
+ section = {}
+ sections[section_key] = section
+ section[item_key] = item
+
set('common', 'name', 'mpuls')
set('common', 'version', 'rev. %s' % get_revision())
set('common', 'releasedate', '07.04.2010')
@@ -291,13 +313,6 @@
log.exception('Error on checking config (%s, %s)' % (section, name))
return False
- def get(self, section, name):
- try:
- return self.config[section][name]
- except KeyError:
- log.exception('Error on checking config (%s, %s)' % (section, name))
- return ''
-
def get_app_name(self):
return self.get('common', 'name')
@@ -326,4 +341,29 @@
except KeyError:
return key
+class MpulsDBMapping(MpulsConfig):
+
+ def build_defaults(self):
+ '''Set default configuration. This is function should be calles if
+ there is no config file'''
+
+ sections = {}
+
+ def set(section_key, item_key, item):
+ try:
+ section = sections[section_key]
+ except KeyError:
+ section = {}
+ sections[section_key] = section
+ section[item_key] = item
+ mapping = {}
+ set('db', 'mapping', [mapping])
+ return sections
+
+ def get_mapping(self):
+ mapping = self.get('db', 'mapping')
+ if mapping != '':
+ return mapping[0]
+ else: return {}
+
# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
More information about the Mpuls-commits
mailing list