summaryrefslogtreecommitdiffstats
path: root/utils_xml.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-10-15 19:13:36 +0200
committerJan Pokorný <jpokorny@redhat.com>2014-10-21 16:13:48 +0200
commit58e69100883edc17bd6c4f737171e9931e0b57be (patch)
tree80ec058775704e109ac1ac18c86adc435e98a5d2 /utils_xml.py
parent3f32a96c01512bab5501571127a4bc468db251ce (diff)
downloadclufter-58e69100883edc17bd6c4f737171e9931e0b57be.tar.gz
clufter-58e69100883edc17bd6c4f737171e9931e0b57be.tar.xz
clufter-58e69100883edc17bd6c4f737171e9931e0b57be.zip
utils_xml: nselem: allow to add children/text at once
Key is simple: instance of basestring will contribute on the associated text, other positional arguments are supposed the child nodes. Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'utils_xml.py')
-rw-r--r--utils_xml.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/utils_xml.py b/utils_xml.py
index 8fea76d..e31c1be 100644
--- a/utils_xml.py
+++ b/utils_xml.py
@@ -9,6 +9,7 @@ from lxml import etree
from .error import ClufterPlainError
from .utils import selfaware
+from .utils_func import bifilter
NAMESPACES = {
@@ -38,8 +39,16 @@ def namespaced(ns, ident):
return ret
-def nselem(ns, tag, **kwargs):
- return etree.Element(namespaced(ns, tag), **kwargs)
+def nselem(ns, tag, *args, **kwargs):
+ ret = etree.Element(namespaced(ns, tag), **kwargs)
+ strings, nonstrings = bifilter(lambda x: isinstance(x, basestring), args)
+ ret.extend(nonstrings)
+ # conditionally assigned so as to support self-closed tags where possible
+ text = ' '.join(strings)
+ if text:
+ ret.text = text
+ return ret
+
rng_get_start = etree.ETXPath("/{0}/{1}"
.format(namespaced(RNG, 'grammar'),