[Formed-commits] r391 - trunk/formed/formed/plugins/export
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Tue Aug 10 11:22:04 CEST 2010
Author: torsten
Date: 2010-08-10 11:22:04 +0200 (Tue, 10 Aug 2010)
New Revision: 391
Added:
trunk/formed/formed/plugins/export/rules_sh.py
Removed:
trunk/formed/formed/plugins/export/rules_sql.py
Log:
Renamed file
Copied: trunk/formed/formed/plugins/export/rules_sh.py (from rev 390, trunk/formed/formed/plugins/export/rules_sql.py)
Property changes on: trunk/formed/formed/plugins/export/rules_sh.py
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/formed/formed/plugins/export/rules_sql.py
===================================================================
--- trunk/formed/formed/plugins/export/rules_sql.py 2010-08-10 09:20:49 UTC (rev 390)
+++ trunk/formed/formed/plugins/export/rules_sql.py 2010-08-10 09:22:04 UTC (rev 391)
@@ -1,182 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright (c) 2007 by Intevation GmbH
-#
-# FormEd is free software under the terms of the
-# GNU General Public License Version 3. See file
-# LICENSE coming with the source of FormEd for details.
-#
-# Authors:
-# Torsten Irländer <torsten.irlaender at intevation.de>
-#
-
-from formed.plugins.ui.controls import FileDialogFilter
-from formed.model.exprtree import *
-
-import formed.model.data as data
-
-#from formed.model.misc import ModeChecker, checkMode
-
-import codecs
-from cgi import escape
-
-def sql_today(node, document):
- out = []
- out.append("now()")
- return " ".join(out)
-
-def sql_var(node, document):
- out = []
- out.append(node.var_name)
- return " ".join(out)
-
-def sql_const(node, document):
- out = []
- out.append(str(node.const))
- return " ".join(out)
-
-def sql_And(node, document):
- out = []
- out.append("(")
- out.append("%s AND %s" % (recursive_parse(node.a, document), recursive_parse(node.b, document)))
- out.append(")")
- return " ".join(out)
-
-def sql_Or(node, document):
- out = []
- out.append("(")
- out.append("%s OR %s" % (recursive_parse(node.a, document), recursive_parse(node.b, document)))
- out.append(")")
- return " ".join(out)
-
-def sql_OperatorBinary(node, document):
- out = []
- operator = ""
- if node.operator.__name__ == "GT":
- operator = ">"
- out.append("%s %s %s" % (recursive_parse(node.a, document), operator, recursive_parse(node.b, document)))
- elif node.operator.__name__ == "EQ":
- operator = "="
- out.append("%s %s %s" % (recursive_parse(node.a, document), operator, recursive_parse(node.b, document)))
- elif node.operator.__name__ == "GE":
- operator = ">="
- out.append("%s %s %s" % (recursive_parse(node.a, document), operator, recursive_parse(node.b, document)))
- elif node.operator.__name__ == "DAYS":
- operator = "-"
- out.append("abs(%s %s %s)" % (recursive_parse(node.a, document), operator, recursive_parse(node.b, document)))
- else:
- raise Exception('Can not parse %s' % node)
- return " ".join(out)
-
-def sql_OperatorUnary(node, document):
- out = []
- if node.operator.__name__ == "NOT":
- out.append("NOT")
- out.append("(")
- out.append(recursive_parse(node.a, document))
- out.append(")")
- elif node.operator.__name__ == "ISSET":
- out.append("(")
- out.append("%s IS NOT NULL" % recursive_parse(node.a, document))
- out.append(")")
- elif node.operator.__name__ == "KNOWN":
- if not isinstance(node.a, Var):
- raise Exception('Can not parse %s' % node)
- n = document.getNodeComponentByName(node.a.var_name)
- if isinstance(n, data.DateLeaf):
- ukv = "'0001-01-01'"
- elif isinstance(n, data.IntLeaf):
- ukv = '-9999999'
- elif isinstance(n, data.TextLeaf):
- ukv = "'unbekannt'"
- out.append("%s != %s" % (recursive_parse(node.a, document), ukv))
- else:
- print "Unkown OperatorUnary %s" % node.operator.__name__
- out.append(recursive_parse(node.a, document))
- return " ".join(out)
-
-def recursive_parse(node, document):
- out = []
- if isinstance(node, OperatorUnary):
- out.append(sql_OperatorUnary(node, document))
- elif isinstance(node, OperatorBinary):
- out.append(sql_OperatorBinary(node, document))
- elif isinstance(node, And):
- out.append(sql_And(node, document))
- elif isinstance(node, Or):
- out.append(sql_Or(node, document))
- elif isinstance(node, Var):
- out.append(sql_var(node, document))
- elif isinstance(node, Const):
- out.append(sql_const(node, document))
- elif isinstance(node, Today):
- out.append(sql_today(node, document))
- else:
- raise Exception('Can not parse %s' % node)
- return " ".join(out)
-
-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 SH Query...")
-
- def getDescription(self):
- return _("Exports rules as a SH")
-
- def wildcards(self):
- return _("SH files (*.sh)|*.sh")
-
- def dialogMessage(self):
- return _("Export rules as SH document")
-
- def errorTitle(self):
- return _("Error occured while exporting rules to SH")
-
- def doExport(self, path, main, dummy):
- document = main.getDocument()
- root = document.root
- mode = main.getSelectedMode()
- allModes = main.getAllModes()
- out = None
- sql = None
- try:
- out = codecs.open(path, "wb", "UTF-8")
- all_rules = list(document.findAllByClass(data.RuleLeaf, mode, allModes))
- error_rules = []
- for idx, rule in enumerate(all_rules):
- if rule.getMark().find('error') < 0: continue
- error_rules.append(rule)
- sql = generate_analyse(error_rules, document)
- print >> out, sql
- finally:
- if out:
- try: out.close()
- except: pass
-
-# vim:set ts=4 sw=4 si et sta sts=4 enc=utf-8 fenc=utf-8:
More information about the Formed-commits
mailing list