[Mpuls-commits] r343 - in wasko/trunk: . waskaweb waskaweb/controllers waskaweb/converter waskaweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 9 12:17:33 CET 2009


Author: teichmann
Date: 2009-03-09 12:17:30 +0100 (Mon, 09 Mar 2009)
New Revision: 343

Added:
   wasko/trunk/waskaweb/converter/
   wasko/trunk/waskaweb/converter/__init__.py
   wasko/trunk/waskaweb/converter/wasko_v1v2.py
Modified:
   wasko/trunk/ChangeLog.txt
   wasko/trunk/waskaweb/controllers/case.py
   wasko/trunk/waskaweb/model/casexml.py
Log:
Added import converter v1 -> v2.


Modified: wasko/trunk/ChangeLog.txt
===================================================================
--- wasko/trunk/ChangeLog.txt	2009-03-09 09:37:11 UTC (rev 342)
+++ wasko/trunk/ChangeLog.txt	2009-03-09 11:17:30 UTC (rev 343)
@@ -1,3 +1,14 @@
+2009-03-05	Sascha L. Teichmann	<teichmann at intevation.de>
+
+	* waskaweb/model/casexml.py: Added converter plug-in logic
+	  from Offline Client.
+
+	* waskaweb/controllers/case.py: call XML import with v1 -> v2 
+	  converter.
+
+	* waskaweb/converter/wasko_v1v2.py: New. Converts v1 to v2
+	  XML documents during import.
+
 2009-03-09	Torsten Irlaender  <torsten.irlaender at intevation.de> 
 
 	* formed/privacy_statement.html: Replaced "Kompetenzagentur" with

Modified: wasko/trunk/waskaweb/controllers/case.py
===================================================================
--- wasko/trunk/waskaweb/controllers/case.py	2009-03-09 09:37:11 UTC (rev 342)
+++ wasko/trunk/waskaweb/controllers/case.py	2009-03-09 11:17:30 UTC (rev 343)
@@ -73,6 +73,8 @@
 from waskaweb.model.phases_factory   import RequiredFields
 from waskaweb.model.phase_transition import symbol_phase, phase_neighbors
 
+from waskaweb.converter.wasko_v1v2   import WaskoV1V2
+
 from waskaweb.lib.db        import db
 #from waskaweb.lib.xmlimport import importFromXML
 
@@ -454,7 +456,7 @@
             conn = None
             try:
                 conn = db.getConnection()
-                import_xml(xml.file, g.formedTree)
+                import_xml(xml.file, g.formedTree, WaskoV1V2)
             finally:
                 db.recycleConnection(conn)
 

Added: wasko/trunk/waskaweb/converter/__init__.py
===================================================================
--- wasko/trunk/waskaweb/converter/__init__.py	2009-03-09 09:37:11 UTC (rev 342)
+++ wasko/trunk/waskaweb/converter/__init__.py	2009-03-09 11:17:30 UTC (rev 343)
@@ -0,0 +1 @@
+#Do not remove me!

Added: wasko/trunk/waskaweb/converter/wasko_v1v2.py
===================================================================
--- wasko/trunk/waskaweb/converter/wasko_v1v2.py	2009-03-09 09:37:11 UTC (rev 342)
+++ wasko/trunk/waskaweb/converter/wasko_v1v2.py	2009-03-09 11:17:30 UTC (rev 343)
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+#
+# (c) 2008 by Intevation GmbH
+# This is Free software under the GPLv3. 
+# See LICENSE comming with the source of 'mpuls offline'
+# for details.
+#
+# author: Sascha L. Teichmann <sascha.teichmann at intevation.de>
+#
+# Converts WASKO v1 to v2 documents
+#  old:
+#      geschw_w_a
+#      geschw_m_a
+#      geschw_hh_a
+#      geschw_w_b
+#      geschw_m_b
+#      geschw_hh_b
+#      geschw_w_c
+#      geschw_m_c
+#      geschw_hh_c
+#      geschw_w_d
+#      geschw_m_d
+#      geschw_hh_d
+#  
+#  new:
+#      geschw_w
+#      geschw_m
+#      geschw_hh
+#      geschw_alt_a
+#      geschw_alt_b
+#      geschw_alt_c
+#      geschw_alt_d
+#  
+#  old -> new:
+#      geschw_w     = geschw_w_a  + geschw_w_b  + geschw_w_c  + geschw_w_d
+#      geschw_m     = geschw_m_a  + geschw_m_b  + geschw_w_c  + geschw_m_d
+#      geschw_hh_a  = geschw_hh_a + geschw_hh_b + geschw_hh_c + geschw_hh_d
+#      geschw_alt_a = geschw_w_a  + geschw_m_a
+#      geschw_alt_b = geschw_w_b  + geschw_m_b
+#      geschw_alt_c = geschw_w_c  + geschw_m_c
+#      geschw_alt_d = geschw_w_d  + geschw_m_d
+
+from waskaweb.lib.xmlhelper import decode_int
+
+
+def getConverterFactory():
+    return WaskoV1V2
+
+OLD_ITEMS = frozenset([
+    'geschw_w_a', 'geschw_m_a', 'geschw_hh_a',
+    'geschw_w_b', 'geschw_m_b', 'geschw_hh_b',
+    'geschw_w_c', 'geschw_m_c', 'geschw_hh_c',
+    'geschw_w_d', 'geschw_m_d', 'geschw_hh_d'])
+
+ADDS = (
+    ('geschw_w',     ('geschw_w_a',  'geschw_w_b',  'geschw_w_c',  'geschw_w_d')),
+    ('geschw_m',     ('geschw_m_a',  'geschw_m_b',  'geschw_w_c',  'geschw_m_d')),
+    ('geschw_hh',    ('geschw_hh_a', 'geschw_hh_b', 'geschw_hh_c', 'geschw_hh_d')),
+    ('geschw_alt_a', ('geschw_w_a',  'geschw_m_a')),
+    ('geschw_alt_b', ('geschw_w_b',  'geschw_m_b')),
+    ('geschw_alt_c', ('geschw_w_c',  'geschw_m_c')),
+    ('geschw_alt_d', ('geschw_w_d',  'geschw_m_d')))
+
+def add(a, b):
+    if a is None: return b
+    if b is None: return a
+    return a + b
+
+class WaskoV1V2(object):
+
+    def __init__(self):
+        self.data = {}
+
+    def eat(self, name, attrs, value):
+        if name in OLD_ITEMS:
+            value = decode_int(value)
+            if not value is None:
+                self.data[name] = value
+            return True
+        return False
+
+    def finish(self, master_loader):
+        if self.data:
+            for key, values in ADDS:
+                s = reduce(add, map(lambda v: self.data.get(v), values), None)
+                if not s is None: 
+                    master_loader.data.append((key, s))
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

Modified: wasko/trunk/waskaweb/model/casexml.py
===================================================================
--- wasko/trunk/waskaweb/model/casexml.py	2009-03-09 09:37:11 UTC (rev 342)
+++ wasko/trunk/waskaweb/model/casexml.py	2009-03-09 11:17:30 UTC (rev 343)
@@ -320,13 +320,15 @@
 
 class MasterHandler(object):
 
-    def __init__(self, formed):
-        self.formed  = formed
-        self.version = 1
-        self.uuid    = None
-        self.data    = []
+    def __init__(self, formed, converter = None):
+        self.formed    = formed
+        self.version   = 1
+        self.uuid      = None
+        self.data      = []
+        self.converter = converter
 
     def element_start(self, name, attrs):
+
         if name == u"cases":
             try:
                 self.version = int(attrs[u'version'])
@@ -334,11 +336,15 @@
                 pass
 
             if self.version > VERSION:
-                raise Exception(
-                    "Cannot load files of version %d." % version)
+                raise XMLImportException(
+                    u"Dateien der Version %d können nicht importiert werden." % \
+                        self.version)
 
     def element_end(self, name, attrs, value):
 
+        if self.converter and self.converter.eat(name, attrs, value):
+            return
+
         try:
             widget = self.formed.widgets[name]
         except KeyError:
@@ -370,10 +376,12 @@
     update = SQL_UPDATE_MASTER % (",".join(fields), ds_id)
     cur.execute(update, changes)
 
-def import_xml(f, formed):
+def import_xml(f, formed, converter_factory):
 
-    master_handler = MasterHandler(formed)
+    converter = converter_factory and converter_factory() or None
 
+    master_handler = MasterHandler(formed, converter)
+
     loader = StackedLoader()
 
     loader.add_factories(
@@ -391,6 +399,8 @@
     parser.setContentHandler(loader)
     parser.parse(f)
 
+    if converter: converter.finish(master_handler)
+
     con, cur = None, None
     try:
         con = db.getConnection()
@@ -441,7 +451,7 @@
                     for rule in rules:
                         mark = rule.getMark()
                         if mark and mark.find("warning:") >= 0: continue
-                        print rule.getAttribute('expr')
+                        #print rule.getAttribute('expr')
                         expr = rule.getExpr()
 
                         isNull, params = False, {}



More information about the Mpuls-commits mailing list