[Mpuls-commits] r345 - in wasko/trunk: . waskaweb/converter waskaweb/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Mar 9 13:30:26 CET 2009
Author: teichmann
Date: 2009-03-09 13:30:20 +0100 (Mon, 09 Mar 2009)
New Revision: 345
Modified:
wasko/trunk/ChangeLog.txt
wasko/trunk/waskaweb/converter/wasko_v1v2.py
wasko/trunk/waskaweb/model/casexml.py
Log:
Handle XML import of multiple cases more gently.
Modified: wasko/trunk/ChangeLog.txt
===================================================================
--- wasko/trunk/ChangeLog.txt 2009-03-09 11:42:25 UTC (rev 344)
+++ wasko/trunk/ChangeLog.txt 2009-03-09 12:30:20 UTC (rev 345)
@@ -1,5 +1,10 @@
2009-03-05 Sascha L. Teichmann <teichmann at intevation.de>
+ * waskaweb/model/casexml.py, waskaweb/converter/wasko_v1v2.py:
+ Handle import of multiple cases more gently.
+
+2009-03-05 Sascha L. Teichmann <teichmann at intevation.de>
+
* waskaweb/model/casexml.py: Added converter plug-in logic
from Offline Client.
Modified: wasko/trunk/waskaweb/converter/wasko_v1v2.py
===================================================================
--- wasko/trunk/waskaweb/converter/wasko_v1v2.py 2009-03-09 11:42:25 UTC (rev 344)
+++ wasko/trunk/waskaweb/converter/wasko_v1v2.py 2009-03-09 12:30:20 UTC (rev 345)
@@ -71,6 +71,9 @@
def __init__(self):
self.data = {}
+ def reset(self):
+ self.data.clear()
+
def eat(self, name, attrs, value):
if name in OLD_ITEMS:
value = decode_int(value)
Modified: wasko/trunk/waskaweb/model/casexml.py
===================================================================
--- wasko/trunk/waskaweb/model/casexml.py 2009-03-09 11:42:25 UTC (rev 344)
+++ wasko/trunk/waskaweb/model/casexml.py 2009-03-09 12:30:20 UTC (rev 345)
@@ -245,12 +245,19 @@
def add_entry(self, entry):
self.entries.append(entry)
+ def reset(self):
+ self.entries = []
+
class LogbookLoader(object):
def __init__(self):
self.logbook = Logbook()
self.entry = None
+ def to_database(self, cur, ds_id):
+ self.logbook.to_database(cur, ds_id)
+ self.logbook.reset()
+
def register_loader(self, loader):
factories = {
@@ -320,13 +327,21 @@
class MasterHandler(object):
- def __init__(self, formed, converter = None):
- self.formed = formed
- self.version = 1
- self.uuid = None
- self.data = []
- self.converter = converter
+ def __init__(self, formed, logbook_loader, cur, converter = None):
+ self.formed = formed
+ self.version = 1
+ self.uuid = None
+ self.data = []
+ self.logbook_loader = logbook_loader
+ self.cur = cur
+ self.converter = converter
+ def register_loader(self, loader):
+ loader.add_factories(
+ 'master',
+ {},
+ (self.element_start, self.element_end))
+
def element_start(self, name, attrs):
if name == u"cases":
@@ -340,8 +355,20 @@
u"Dateien der Version %d können nicht importiert werden." % \
self.version)
+ elif name == u"case":
+ self.version = 1
+ self.uuid = None
+ self.data = []
+ if self.converter:
+ self.converter.reset()
+
def element_end(self, name, attrs, value):
+ if name == u"case":
+ if self.converter: self.converter.finish(self)
+ self.finish()
+ return
+
if self.converter and self.converter.eat(name, attrs, value):
return
@@ -357,6 +384,83 @@
self.data.append((name, DECODERS.get(widget.__class__, lambda s: s)(value)))
+ def finish(self):
+ create_case, data = True, None
+
+ if self.uuid:
+ #print >> sys.stderr, "found uuid '%s'" % repr(master_handler.uuid)
+ ds_id = get_id_for_uuid(self.cur, 'master', self.uuid)
+ if not ds_id is None:
+ #print >> sys.stderr, "case %d already exists" % ds_id
+ cur.execute(SQL_SELECT_ALL_FROM_MASTER % { 'ds_id': ds_id })
+ row = self.cur.fetchone()
+ if not row:
+ raise XMLImportException(u"Daten zum Fall '%d' können nicht geladen werden." % ds_id)
+ data = dict(zip([n[0] for n in cur.description], map(convert_from_db, row)))
+ create_case = False
+ #else:
+ # print >> sys.stderr, "case with uuid '%s' does not exists" % repr(
+ # master_handler.uuid)
+ #else:
+ # print >> sys.stderr, "case does not have an uuid"
+
+ if data is None:
+ # print >> sys.stderr, "preset case with defaults from formed"
+ data = self.formed.name2default
+
+ changed = {}
+
+ def get_data(var):
+ try:
+ return changed[var]
+ except KeyError:
+ return data.get(var)
+
+ # apply the rules
+
+ errors = []
+
+ for var, value in self.data:
+ old_value = get_data(var)
+ if old_value == value: continue
+
+ rules = self.formed.getRulesForVariable(var)
+ okay = True
+ if rules:
+ for rule in rules:
+ mark = rule.getMark()
+ if mark and mark.find("warning:") >= 0: continue
+ #print rule.getAttribute('expr')
+ expr = rule.getExpr()
+
+ isNull, params = False, {}
+ n_okay = True
+ for dep in expr.getDependencies():
+ dep_v = get_data(dep)
+ if dep != var and dep_v is None: isNull = True; break
+ params[dep] = dep_v
+ if not isNull:
+ params[var] = value
+ try: n_okay = expr.evaluate(params)
+ except: n_okay = False
+ if not n_okay:
+ errors.append(rule.getValue())
+ okay = False
+ if okay:
+ changed[var] = value
+
+ if errors:
+ raise XMLImportException(
+ u"Es wurden Inkonsistenzen festgestellt.", errors)
+
+ if create_case:
+ ds_id = create_master(self.cur, self.uuid)
+
+ store_changes(self.cur, changed, ds_id)
+
+ # store the logbook too
+ self.logbook_loader.to_database(self.cur, ds_id)
+
def get_id_for_uuid(cur, relation_name, uuid):
cur.execute(SQL_GET_MASTER_ID, {
'relation_name': relation_name, 'uuid': uuid })
@@ -380,108 +484,27 @@
converter = converter_factory and converter_factory() or None
- master_handler = MasterHandler(formed, converter)
-
loader = StackedLoader()
- loader.add_factories(
- 'master',
- {},
- (master_handler.element_start, master_handler.element_end))
-
- loader.activate_factories('master') # for backwards compat
-
logbook_loader = LogbookLoader()
- logbook_loader.register_loader(loader)
-
- parser = make_parser()
- parser.setContentHandler(loader)
- parser.parse(f)
-
- if converter: converter.finish(master_handler)
-
con, cur = None, None
try:
con = db.getConnection()
cur = con.cursor()
try:
- create_case, data = True, None
+ master_handler = MasterHandler(formed, logbook_loader, cur, converter)
- if master_handler.uuid:
- #print >> sys.stderr, "found uuid '%s'" % repr(master_handler.uuid)
- ds_id = get_id_for_uuid(cur, 'master', master_handler.uuid)
- if not ds_id is None:
- #print >> sys.stderr, "case %d already exists" % ds_id
- cur.execute(SQL_SELECT_ALL_FROM_MASTER % { 'ds_id': ds_id })
- row = cur.fetchone()
- if not row:
- raise XMLImportException(u"Daten zum Fall '%d' können nicht geladen werden." % ds_id)
- data = dict(zip([n[0] for n in cur.description], map(convert_from_db, row)))
- create_case = False
- #else:
- # print >> sys.stderr, "case with uuid '%s' does not exists" % repr(
- # master_handler.uuid)
- #else:
- # print >> sys.stderr, "case does not have an uuid"
+ master_handler.register_loader(loader)
- if data is None:
- # print >> sys.stderr, "preset case with defaults from formed"
- data = formed.name2default
+ loader.activate_factories('master') # for backwards compat
- changed = {}
+ logbook_loader.register_loader(loader)
- def get_data(var):
- try:
- return changed[var]
- except KeyError:
- return data.get(var)
+ parser = make_parser()
+ parser.setContentHandler(loader)
+ parser.parse(f)
- # apply the rules
-
- errors = []
-
- for var, value in master_handler.data:
- old_value = get_data(var)
- if old_value == value: continue
-
- rules = formed.getRulesForVariable(var)
- okay = True
- if rules:
- for rule in rules:
- mark = rule.getMark()
- if mark and mark.find("warning:") >= 0: continue
- #print rule.getAttribute('expr')
- expr = rule.getExpr()
-
- isNull, params = False, {}
- n_okay = True
- for dep in expr.getDependencies():
- dep_v = get_data(dep)
- if dep != var and dep_v is None: isNull = True; break
- params[dep] = dep_v
- if not isNull:
- params[var] = value
- try: n_okay = expr.evaluate(params)
- except: n_okay = False
- if not n_okay:
- errors.append(rule.getValue())
- okay = False
- if okay:
- changed[var] = value
-
- if errors:
- raise XMLImportException(
- u"Es wurden Inkonsistenzen festgestellt.", errors)
-
- if create_case:
- ds_id = create_master(cur, master_handler.uuid)
-
- store_changes(cur, changed, ds_id)
-
- # store the logbook too
- logbook_loader.logbook.to_database(cur, ds_id)
-
con.commit()
except:
try: con.rollback()
More information about the Mpuls-commits
mailing list