[Mpuls-commits] r2780 - in base/trunk: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu May 27 11:22:39 CEST 2010


Author: bh
Date: 2010-05-27 11:22:38 +0200 (Thu, 27 May 2010)
New Revision: 2780

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/helpers.py
   base/trunk/mpulsweb/lib/renderer.py
Log:
* mpulsweb/lib/helpers.py (tag): Moved here from renderer because
it's useful in other modules besides the renderer.

* mpulsweb/lib/renderer.py (tag): Removed.  It's now imported from
helpers.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-05-26 16:59:18 UTC (rev 2779)
+++ base/trunk/ChangeLog	2010-05-27 09:22:38 UTC (rev 2780)
@@ -1,3 +1,11 @@
+2010-05-27  Bernhard Herzog  <bh at intevation.de>
+
+	* mpulsweb/lib/helpers.py (tag): Moved here from renderer because
+	it's useful in other modules besides the renderer.
+
+	* mpulsweb/lib/renderer.py (tag): Removed.  It's now imported from
+	helpers.
+
 2010-05-26  Bernhard Herzog  <bh at intevation.de>
 
 	* mpulsweb/controllers/usersettings.py

Modified: base/trunk/mpulsweb/lib/helpers.py
===================================================================
--- base/trunk/mpulsweb/lib/helpers.py	2010-05-26 16:59:18 UTC (rev 2779)
+++ base/trunk/mpulsweb/lib/helpers.py	2010-05-27 09:22:38 UTC (rev 2780)
@@ -34,6 +34,7 @@
 import re
 import datetime
 from string import printable
+from xml.sax.saxutils import quoteattr
 
 from pylons import c, cache, config, g, request, response, session, url
 from pylons.i18n import _
@@ -68,7 +69,68 @@
         return s
     return unicode(str(s), 'utf-8')
 
+# html helper
+def tag(tagname, _close=False,
+        _booleans=('checked', 'disabled', 'multiple', 'selected'), **attrs):
+    """Create an opening HTML tag with properly escaped attributes.
 
+    The tagname parameter gives the name of the tag.  The attributes and
+    their values are given as keyword parameters.  Most attributes have
+    string values.  Boolean attributes are treated as described below.
+    The values of string attributes are converted to strings if they are
+    not aleady strings (byte-strings or unicode) using these rules: None
+    is converted to the empty string, other values are converted using
+    the str function.  After conversion the values of string attributes
+    will be properly escaped as attribute values when inserted into the
+    tag.
+
+    Boolean flags are treated differently: if the value of such an
+    attribute is true, the attribute is included in the tag without a
+    value, if the value is false, the attribute is omitted.  The boolean
+    attributes handled in this way are given by the _booleans parameter,
+    which is a sequence of attribute names.  Its default value is
+    ('checked', 'disabled', 'multiple', 'selected')
+
+    To allow for attribute names that are also python keywords, a single
+    trailing underscore character is removed from an attribute name if
+    it is preset, so that e.g. the class attribute can be specified
+    using the class_ keyword parameters.
+
+    A few attribute names are not valid Python identifiers because they
+    contain a minus-sign (e.g. accept-charset).  However, since there
+    are no attribute names that contain underscores, underscores in a
+    keyword argument will be converted to minus-signs to determine the
+    attribute name.  For instance the accept-charset attribute can be
+    given as accept_charset to this function.
+
+    Examples:
+    >>> from mpulsweb.lib.renderer import tag
+    >>> tag("input", type="text", name="title", disabled=False)
+    '<input type="text" name="title">'
+    >>> 
+    """
+    out = ["<%s" % tagname]
+    for name, value in attrs.items():
+        if name.endswith("_"):
+            name = name[:-1]
+        name = name.replace("_", "-")
+        if name in _booleans:
+            if value:
+                out.append(' %s="%s"' % (name, name))
+        else:
+            if value is None:
+                value = ""
+            elif not isinstance(value, basestring):
+                value = str(value)
+            out.append(" %s=%s" % (name, quoteattr(value)))
+    if _close:
+        out.append(">")
+    else:
+        out.append(">")
+    return "".join(out)
+
+
+
 # Helper functions used in the UI to show some information like status messages
 # or icons
 

Modified: base/trunk/mpulsweb/lib/renderer.py
===================================================================
--- base/trunk/mpulsweb/lib/renderer.py	2010-05-26 16:59:18 UTC (rev 2779)
+++ base/trunk/mpulsweb/lib/renderer.py	2010-05-27 09:22:38 UTC (rev 2780)
@@ -30,7 +30,6 @@
 
 import re
 from cgi import escape
-from xml.sax.saxutils import quoteattr
 import logging
 
 from pylons.i18n import _
@@ -41,7 +40,7 @@
 from formed.instance.backends.common import RuleEvalContext
 
 from mpulsweb.lib.helper.filters import NA
-from mpulsweb.lib.helpers import url_for, dd_mm_YYYY as format_date
+from mpulsweb.lib.helpers import tag, url_for, dd_mm_YYYY as format_date
 
 
 log = logging.getLogger(__name__)
@@ -73,66 +72,6 @@
     return depth
 
 
-def tag(tagname, _close=False,
-        _booleans=('checked', 'disabled', 'multiple', 'selected'), **attrs):
-    """Create an opening HTML tag with properly escaped attributes.
-
-    The tagname parameter gives the name of the tag.  The attributes and
-    their values are given as keyword parameters.  Most attributes have
-    string values.  Boolean attributes are treated as described below.
-    The values of string attributes are converted to strings if they are
-    not aleady strings (byte-strings or unicode) using these rules: None
-    is converted to the empty string, other values are converted using
-    the str function.  After conversion the values of string attributes
-    will be properly escaped as attribute values when inserted into the
-    tag.
-
-    Boolean flags are treated differently: if the value of such an
-    attribute is true, the attribute is included in the tag without a
-    value, if the value is false, the attribute is omitted.  The boolean
-    attributes handled in this way are given by the _booleans parameter,
-    which is a sequence of attribute names.  Its default value is
-    ('checked', 'disabled', 'multiple', 'selected')
-
-    To allow for attribute names that are also python keywords, a single
-    trailing underscore character is removed from an attribute name if
-    it is preset, so that e.g. the class attribute can be specified
-    using the class_ keyword parameters.
-
-    A few attribute names are not valid Python identifiers because they
-    contain a minus-sign (e.g. accept-charset).  However, since there
-    are no attribute names that contain underscores, underscores in a
-    keyword argument will be converted to minus-signs to determine the
-    attribute name.  For instance the accept-charset attribute can be
-    given as accept_charset to this function.
-
-    Examples:
-    >>> from mpulsweb.lib.renderer import tag
-    >>> tag("input", type="text", name="title", disabled=False)
-    '<input type="text" name="title">'
-    >>> 
-    """
-    out = ["<%s" % tagname]
-    for name, value in attrs.items():
-        if name.endswith("_"):
-            name = name[:-1]
-        name = name.replace("_", "-")
-        if name in _booleans:
-            if value:
-                out.append(' %s="%s"' % (name, name))
-        else:
-            if value is None:
-                value = ""
-            elif not isinstance(value, basestring):
-                value = str(value)
-            out.append(" %s=%s" % (name, quoteattr(value)))
-    if _close:
-        out.append(">")
-    else:
-        out.append(">")
-    return "".join(out)
-
-
 def hidden_bool_tag(id):
     """Return the hidden HTML input tag for the plain boolean field given by id.
 



More information about the Mpuls-commits mailing list