[Formed-commits] r390 - trunk/formed/formed/plugins/export

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Aug 10 11:20:49 CEST 2010


Author: torsten
Date: 2010-08-10 11:20:49 +0200 (Tue, 10 Aug 2010)
New Revision: 390

Modified:
   trunk/formed/formed/plugins/export/rules_sql.py
Log:
Modified export. To generate an analyse shell-script.


Modified: trunk/formed/formed/plugins/export/rules_sql.py
===================================================================
--- trunk/formed/formed/plugins/export/rules_sql.py	2010-08-09 15:36:53 UTC (rev 389)
+++ trunk/formed/formed/plugins/export/rules_sql.py	2010-08-10 09:20:49 UTC (rev 390)
@@ -7,7 +7,7 @@
 # LICENSE coming with the source of FormEd for details.
 #
 # Authors:
-# Sascha L. Teichmann <teichmann at intevation.de>
+# Torsten Irländer <torsten.irlaender at intevation.de>
 #
 
 from formed.plugins.ui.controls import FileDialogFilter
@@ -115,25 +115,48 @@
         raise Exception('Can not parse %s' % node) 
     return " ".join(out)
 
-class ExportRulesAsSQL(FileDialogFilter):
+def generate_analyse(rules, document):
+    out = []
+    # 1. Header
+    out.append('#!/bin/bash')
+    out.append("""DATABASES=`psql -t -c "SELECT datname FROM pg_database WHERE datname LIKE 'ka_%';" postgres`""")
+    out.append('echo "DB,%s"' % ",".join("q%s" % [str(i) for i in range(0,len(rules))]))
+    out.append("for DB in ${DATABASES}")
+    out.append("do")
 
+    # 2. Querys 
+    command = 'q%s=`psql -t -c "%s" ${DB}`'
+    for num, rule in enumerate(rules):
+        out.append('# Eval %s' % num)
+        out.append('# %s' % rule.getDescription())
+        out.append(command % 
+                   (num, 'select count(id) from master_tbl where not (%s)' %  
+                    recursive_parse(rule.getExpr().prog, document)))
+        out.append('\n')
+    # 3. Footer 
+    out.append('echo "${DB},%s"' % ",".join(["${q%s}" % str(i) for i in range(0,len(rules))]))
+    out.append("done")
+    return "\n".join(out)
+
+class ExportRulesAsSH(FileDialogFilter):
+
     def __init__(self):
         FileDialogFilter.__init__(self)
 
     def getMenuName(self):
-        return _("Export R&ules As SQL Query...")
+        return _("Export R&ules As SH Query...")
 
     def getDescription(self):
-        return _("Exports rules as a SQL")
+        return _("Exports rules as a SH")
 
     def wildcards(self):
-        return _("SQL files (*.sql)|*.sql")
+        return _("SH files (*.sh)|*.sh")
 
     def dialogMessage(self):
-        return _("Export rules as SQL document")
+        return _("Export rules as SH document")
 
     def errorTitle(self):
-        return _("Error occured while exporting rules to SQL")
+        return _("Error occured while exporting rules to SH")
 
     def doExport(self, path, main, dummy):
         document = main.getDocument()
@@ -141,25 +164,16 @@
         mode     = main.getSelectedMode()
         allModes = main.getAllModes()
         out      = None
-        sql      = []
+        sql      = None
         try:
-            sql.append("select count(id) from master_tbl where (")
             out = codecs.open(path, "wb", "UTF-8")
             all_rules = list(document.findAllByClass(data.RuleLeaf, mode, allModes))
-            where = []
+            error_rules = []
             for idx, rule in enumerate(all_rules):
-                tmp_sql = []
                 if rule.getMark().find('error') < 0: continue
-                desc = rule.getDescription()
-                if not desc: desc = rule.getValue()
-                tmp_sql.append("\n")
-                tmp_sql.append("--")
-                tmp_sql.append("-- %s\n" % desc)
-                tmp_sql.append("%s" % recursive_parse(rule.getExpr().prog, document))
-                where.append("\n".join(tmp_sql))
-            sql.append("\nAND ".join(where))
-            sql.append(")")
-            print >> out, "\n".join(sql)
+                error_rules.append(rule)
+            sql = generate_analyse(error_rules, document)
+            print >> out, sql
         finally:
             if out:
                 try: out.close()



More information about the Formed-commits mailing list