[Formed-commits] r371 - in trunk: . formed/formed/model formed/formed/plugins/modify
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Jul 26 09:38:59 CEST 2010
Author: roland
Date: 2010-07-26 09:38:53 +0200 (Mon, 26 Jul 2010)
New Revision: 371
Modified:
trunk/ChangeLog
trunk/formed/formed/model/nodecomponents.py
trunk/formed/formed/plugins/modify/rules.py
Log:
generated date rules take into consideration repeat groups
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog 2010-07-21 14:54:10 UTC (rev 370)
+++ trunk/ChangeLog 2010-07-26 07:38:53 UTC (rev 371)
@@ -1,3 +1,9 @@
+2010-07-26 Roland Geider <roland.geider at intevation.de>
+
+ * formed/formed/model/nodecomponents.py,
+ formed/formed/plugins/modify/rules.py: generated date rules take
+ into consideration repeat groups
+
2010-07-21 Torsten Irländer <torsten.irlaender at intevation.de>
Issue870
Modified: trunk/formed/formed/model/nodecomponents.py
===================================================================
--- trunk/formed/formed/model/nodecomponents.py 2010-07-21 14:54:10 UTC (rev 370)
+++ trunk/formed/formed/model/nodecomponents.py 2010-07-26 07:38:53 UTC (rev 371)
@@ -113,11 +113,21 @@
def getParent(self):
return self.parent
- def pathToRoot(self):
+ def pathToRoot(self, reverse=True):
+
+ """ Return the path to the root node from the current node.
+
+ The reverse options controlls the sorting (from root to node or from
+ node to root)
+ """
+
current = self
path = []
while not current is None:
- path.insert(0, current)
+ if reverse:
+ path.insert(0, current)
+ else:
+ path.append(current)
current = current.parent
return path
Modified: trunk/formed/formed/plugins/modify/rules.py
===================================================================
--- trunk/formed/formed/plugins/modify/rules.py 2010-07-21 14:54:10 UTC (rev 370)
+++ trunk/formed/formed/plugins/modify/rules.py 2010-07-26 07:38:53 UTC (rev 371)
@@ -55,14 +55,14 @@
self.parentModes = parentModes
self.entities = {}
- def substitute(self, name):
+ def substitute(self, name, prefix='$', cache=True):
try:
return self.entities[name]
except KeyError:
nc = self.formed.getNodeComponentByName(name)
if not nc:
print >> sys.stderr, "WARNING: node '%s' not found."
- value = "$%s" % name
+ value = "%s%s" % (prefix, name)
else:
is_repeat = False
while nc:
@@ -70,8 +70,9 @@
is_repeat = True
break
nc = nc.parent
- value = "%s%s" % ("$", name)
- self.entities[name] = value
+ value = "%s%s" % (prefix, name)
+ if cache:
+ self.entities[name] = value
return value
def traverse_recursive(current, graph, chain, found):
@@ -135,6 +136,19 @@
def getMenuName(self):
return u"Generate required rules"
+
+ def is_in_repeat_group(self, node, reverse=True):
+
+ """ Return a list with all the (nested) repeat groups the node is in
+ (walking up to the node) """
+
+ rg = set()
+
+ for parent in node.pathToRoot(reverse):
+ if isinstance(parent, data.RepeatNode):
+ #rg.add(parent)
+ rg.add(parent.getName())
+ return rg
def getDescription(self):
return u"Generate rules for fields that are required."
@@ -145,10 +159,20 @@
allModes = main.getAllModes()
var_type = VariableType(document, mode, allModes)
-
+
all_dates = list(document.findAllByClass(data.DateLeaf, mode, allModes))
-
+
+ # Iterate through all the dates to generate the requiered rules, taking
+ # into cosideration whether they are in a repeat group or not
+
+ rg_graph = {}
+ for date in all_dates:
+ rg = self.is_in_repeat_group(date, False)
+ if rg != set():
+ rg_graph[date.getName()] = rg
+
date_graph = dict([(d.getName(), (set(), set())) for d in all_dates])
+
today_in_future = set()
today_in_past = set()
@@ -159,10 +183,12 @@
if not m: continue
name = date.getName()
+
+ preds = m.group(1)
+ succs = m.group(2)
+ p_set = date_graph[name][0]
+ s_set = date_graph[name][1]
- preds, succs = m.group(1), m.group(2)
- p_set, s_set = date_graph[name]
-
for pred in preds.split(","):
pred = pred.strip()
if not pred: continue
@@ -223,6 +249,34 @@
dates = dict([(d.getName(), d) for d in all_dates])
for idx, pair in enumerate(pairs):
+ # Generate the rules according to who is in which repeat group.
+
+ # NOTICE: at the moment we are assuming that there are no nested
+ # repeat groups
+ #
+ # For this reason the repeat groups' names are saved in rg_graph, so
+ # once nested repeat groups are added to libformed/formedtree this
+ # function can be adapted
+
+
+
+ # Both in (the same) RG or both outside of a RG (most usual case)
+ # both: $
+ prefix_rule_1 = '$'
+ prefix_rule_2 = '$'
+
+ # 1st Date in a RG, 2nd not
+ # 1st: % 2nd: $
+ if pair[0] in rg_graph and pair[1] not in rg_graph:
+ prefix_rule_1 = '%'
+ prefix_rule_2 = '$'
+
+ # 1st Date not in a RG, 2nd in one
+ # 1st: $ 2nd: %
+ elif pair[0] not in rg_graph and pair[1] in rg_graph:
+ prefix_rule_1 = '$'
+ prefix_rule_2 = '%'
+
rule1 = data.RuleLeaf()
rule2 = data.RuleLeaf()
rule1.setName("date-sequence-rule-%d-1" % idx)
@@ -243,7 +297,9 @@
param = { 'a': pair[0], 'b': pair[1] }
rule1.setDescription(DATE_RULE_DESC % param)
rule2.setDescription(DATE_RULE_DESC % param)
- param = dict([(k, var_type.substitute(v)) for k, v in param.iteritems()])
+ param = { 'a': var_type.substitute(pair[0], prefix_rule_1, False),
+ 'b': var_type.substitute(pair[1], prefix_rule_2, False) }
+
rule1.setExpr(DATE_RULE_EXPR % param)
rule2.setExpr(DATE_RULE_EXPR % param)
More information about the Formed-commits
mailing list