[Mpuls-commits] r74 - in waska/trunk: . formed waskaweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Aug 18 18:18:20 CEST 2008


Author: teichmann
Date: 2008-08-18 18:18:19 +0200 (Mon, 18 Aug 2008)
New Revision: 74

Added:
   waska/trunk/formed/annotations.xhtml
Modified:
   waska/trunk/ChangeLog.txt
   waska/trunk/waskaweb/lib/app_globals.py
   waska/trunk/waskaweb/lib/formular_help.py
Log:
Added support for form annotations. TODO: add controller and adapt renderer.



Modified: waska/trunk/ChangeLog.txt
===================================================================
--- waska/trunk/ChangeLog.txt	2008-08-14 15:32:33 UTC (rev 73)
+++ waska/trunk/ChangeLog.txt	2008-08-18 16:18:19 UTC (rev 74)
@@ -1,3 +1,13 @@
+2008-08-18 	Torsten Irlaender  <torsten.irlaender at intevation.de>
+
+	Added support for form annotations. TODO: add controller and adapt renderer.
+
+	* formed/annotations.xhtml: New. File containing annonations.
+
+	* waskaweb/lib/formular_help.py: Add dictionary for annotations.
+
+	* waskaweb/lib/app_globals.py: Load annotations file.
+
 2008-08-13 	Torsten Irlaender  <torsten.irlaender at intevation.de>
 
 	Issue515

Added: waska/trunk/formed/annotations.xhtml
===================================================================
--- waska/trunk/formed/annotations.xhtml	2008-08-14 15:32:33 UTC (rev 73)
+++ waska/trunk/formed/annotations.xhtml	2008-08-18 16:18:19 UTC (rev 74)
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>Anmerkungen</title>
+    </head>
+    <body>
+        <div id="auswertung1:required:kontaktdaten_agentur_fuer_arbeit">
+            <h2>Kontaktdaten Agentur für Arbeit</h2>
+            Je nach Bedarf ausschließlich zu Ihrer eigenen Benutzung. Fließt nicht in die Auswertung ein.
+        </div>
+    </body>
+</html>

Modified: waska/trunk/waskaweb/lib/app_globals.py
===================================================================
--- waska/trunk/waskaweb/lib/app_globals.py	2008-08-14 15:32:33 UTC (rev 73)
+++ waska/trunk/waskaweb/lib/app_globals.py	2008-08-18 16:18:19 UTC (rev 74)
@@ -33,7 +33,7 @@
 
 from waskaweb.lib.timelog import setupTimeLogging
 
-import os, sys
+import os, sys, traceback
 
 class Globals(object):
     """Globals acts as a container for objects available throughout the
@@ -47,19 +47,24 @@
         """
         # XXX: Dead ugly!
         path = os.path.join(os.path.dirname(__file__), '..', '..', 'formed')
-        treeFile = os.path.join(path, 'formedtree_web.xml')
-        helpFile = os.path.join(path, 'waska-hilfetexte.xhtml')
+        treeFile  = os.path.join(path, 'formedtree_web.xml')
+        helpFile  = os.path.join(path, 'waska-hilfetexte.xhtml')
+        annonFile = os.path.join(path, 'annotations.xhtml')
 
         # XXX: Check why the following throws key exceptions. Is config var not
         # availiable in Globals?!?
         #treeFile = os.path.join(os.path.dirname(__file__), config['formed_tree'])
         self.formedTree = openDocument(treeFile)
         try:
-            f = open(helpFile, "r")
-            xhtml = f.read()
+            f = open(helpFile, "rb")
+            help_xhtml = f.read()
             f.close()
-            self.helpData = HelpProvider(xhtml) 
+            f = open(annonFile)
+            annon_xhtml = f.read()
+            f.close()
+            self.helpData = HelpProvider(help_xhtml, annon_xhtml) 
         except:
+            traceback.print_exc(file=sys.stderr)
             print >> sys.stderr, "Could not open Helpfile"
             self.helpData = None
 

Modified: waska/trunk/waskaweb/lib/formular_help.py
===================================================================
--- waska/trunk/waskaweb/lib/formular_help.py	2008-08-14 15:32:33 UTC (rev 73)
+++ waska/trunk/waskaweb/lib/formular_help.py	2008-08-18 16:18:19 UTC (rev 74)
@@ -29,33 +29,100 @@
 #
 
 from xml.dom.minidom import parseString
-
 from xml.dom.ext     import Print
-
 from cStringIO       import StringIO
 
+import re
+
+ANNOTATION_DIV_ID = re.compile("([a-zA-Z0-9_]+):([a-zA-Z]+):([a-zA-Z0-9_,]+)")
+
+class Annotation:
+
+    def __init__(self, kind, msg):
+        self.kind = kind
+        self.msg  = msg
+
+    def __str__(self):
+        return repr(self.kind) + ": " + repr(self.msg)
+
+    def __repr__(self):
+        return self.__str__()
+
+def buildHelps(help_xhtml):
+    helps = {}
+    dom = None
+    try:
+        dom = parseString(help_xhtml)
+        for div in dom.getElementsByTagName("div"):
+            id = div.getAttribute("id")
+            if not id: continue
+            out = StringIO()
+            Print(div, out, 'UTF-8')
+            helps[id] = out.getvalue()
+            out.close()
+    finally:
+        if dom: dom.unlink()
+
+    return helps
+
+def buildAnnotations(annotation_xhtml):
+    annotations = {}
+    if not annotation_xhtml:
+        return annotations
+
+    dom = None
+    try:
+        dom = parseString(annotation_xhtml)
+        for div in dom.getElementsByTagName("div"):
+            id = div.getAttribute("id")
+            if not id: continue
+            m = ANNOTATION_DIV_ID.match(id)
+            if not m: continue
+            id    = m.group(1)
+            kind  = m.group(2)
+            items = [item for item in m.group(3).split(",") if item]
+            if not items: continue
+            div.setAttribute("id", id)
+
+            out = StringIO()
+            Print(div, out, 'UTF-8')
+            msg = out.getvalue()
+            out.close()
+
+            for item in items:
+                annons = annotations.setdefault(item, [])
+                annons.append(Annotation(kind, msg))
+    finally:
+        if dom: dom.unlink()
+
+    return annotations
+
 class HelpProvider:
 
-    def __init__(self, xhtml):
-        helps = {}
-        dom = None
-        try:
-            dom = parseString(xhtml)
-            for div in dom.getElementsByTagName("div"):
-                id = div.getAttribute("id")
-                if not id: continue
-                out = StringIO()
-                Print(div, out, 'UTF-8')
-                helps[id] = out.getvalue()
-                out.close()
-        finally:
-            if dom: dom.unlink()
-        self.helps = helps
+    def __init__(self, help_xhtml, annotation_xhtml = None):
 
+        self.helps       = buildHelps(help_xhtml)
+        self.annotations = buildAnnotations(annotation_xhtml)
+
     def hasHelp(self, id):
         return self.helps.has_key(id)
 
     def getHelp(self, id, default=None):
         return self.helps.get(id, default)
 
+    def hasAnnotations(self, id, kinds = ("required", )):
+        annotations = self.annotations.get(key)
+        if not annotations: return False
+        for annotation in annotations:
+            if annotation.kind in kinds:
+                return True
+        return False
+
+    def getAnnotations(self, id, kinds = ("required", )):
+        try:
+            x = [a.msg for a in self.annotations[id] if a.kind in kinds]
+        except KeyError:
+            x = []
+        return ''.join(x)
+
 # vim:set ts=4 sw=4 si et sta sts=4:



More information about the Mpuls-commits mailing list