diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2014-10-29 21:23:16 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2014-10-29 21:26:32 +0100 |
commit | 827c268c089cb0591e5adb94cd0d661ef1e71a49 (patch) | |
tree | 2f54c1784705b4ec40a9d4bb83e2faa6b7b68fd1 /ishtar_common/ooo_replace.py | |
parent | ebf87a7cc0d21034c387bc2eff85323c63c49680 (diff) | |
download | Ishtar-827c268c089cb0591e5adb94cd0d661ef1e71a49.tar.bz2 Ishtar-827c268c089cb0591e5adb94cd0d661ef1e71a49.zip |
Add global variables (+admin) - implement an easier ooo parsing (refs #2111)
Diffstat (limited to 'ishtar_common/ooo_replace.py')
-rw-r--r-- | ishtar_common/ooo_replace.py | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py index 346ef75a4..2fb01724b 100644 --- a/ishtar_common/ooo_replace.py +++ b/ishtar_common/ooo_replace.py @@ -17,7 +17,7 @@ # See the file COPYING for details. -import locale +import locale, re from zipfile import ZipFile, ZIP_DEFLATED from cStringIO import StringIO from xml.etree.cElementTree import ElementTree, fromstring @@ -78,9 +78,34 @@ def _format_value(value, default_value): value = unicode(value) if value else default_value return value +VAR_EXPR = u"###VAR %s###" +RE_VAR = re.compile(u"###VAR %s###" % u"([-a-zA-Z0-9_]+)") +IF_EXPR = u"###IF %s###(.*)###ENDIF###" +RE_IF = re.compile(IF_EXPR % u"([-a-zA-Z0-9_]+)") + +def _ishtar_parsing(context, value): + """ + ###VAR nom_var### for displaying a variable name + ###IF nom_var### ###ENDIF### for conditionnal display + Be carreful nested condition are not yet managed! + """ + for key, val in RE_IF.findall(value): + v = "" + if key in context and context[key]: + v = _ishtar_parsing(context, val) + value = re.sub(IF_EXPR % key, v, value) + for key in RE_VAR.findall(value): + v = "" + if key in context and context[key]: + v = unicode(context[key]) + value = re.sub(VAR_EXPR % key, v, value) + return value + def ooo_replace(infile, outfile, context, default_value=''): inzip = ZipFile(infile, 'r', ZIP_DEFLATED) outzip = ZipFile(outfile, 'w', ZIP_DEFLATED) + + # regular ooo parsing content = ElementTree(fromstring(inzip.read('content.xml'))) missing_keys = set() for xp in ('variable-set', 'variable-get'): @@ -102,11 +127,16 @@ def ooo_replace(infile, outfile, context, default_value=''): if value.strip() in context: value = context[value.strip()] p.text = value + + # raw content parsing + str_io = StringIO() + content.write(str_io) + value = str_io.getvalue() + value = _ishtar_parsing(context, value).encode('utf-8') + for f in inzip.infolist(): if f.filename == 'content.xml': - s = StringIO() - content.write(s) - outzip.writestr('content.xml', s.getvalue()) + outzip.writestr('content.xml', value) else: outzip.writestr(f, inzip.read(f.filename)) inzip.close() |