diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-12-02 14:51:09 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-12-02 14:59:06 +0100 |
commit | 6dfc238b654c17e2d95a8eff8ac0262498535214 (patch) | |
tree | e7c44902b0dc74675ae3dda5658fc29c14ec71ba /ishtar_common/ooo_replace.py | |
parent | 5f12ca50e8273f0215fa0a0a92735ff8c4dd6a84 (diff) | |
download | Ishtar-6dfc238b654c17e2d95a8eff8ac0262498535214.tar.bz2 Ishtar-6dfc238b654c17e2d95a8eff8ac0262498535214.zip |
Manage document template
* ooo_replace: generate a document by mapping ooo variables with
a given dict
* DocumentTemplate model: store templates associated with a type
of objects
* get_values method: generate a dict of value from a model
* new form/view to generate document from administrativ acts
Diffstat (limited to 'ishtar_common/ooo_replace.py')
-rw-r--r-- | ishtar_common/ooo_replace.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py new file mode 100644 index 000000000..63b983b7b --- /dev/null +++ b/ishtar_common/ooo_replace.py @@ -0,0 +1,102 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (C) 2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# See the file COPYING for details. + + +from zipfile import ZipFile, ZIP_DEFLATED +from cStringIO import StringIO +from xml.etree.cElementTree import ElementTree, fromstring + +OOO_NS = "{urn:oasis:names:tc:opendocument:xmlns:text:1.0}" + +def _set_value_from_formula(value, context): + value = value.strip() + if value.startswith("ooow:") and len(value) >= 5: + value = value[5:] + if value.startswith('"') and value.endswith('"') and len(value) > 1: + value = value[1:-1] + elif value in context: + value = unicode(context[value]) + else: + value = None + return value + +def _parse_condition(condition, context): + # parse only == and != operator + operator = "" + if "!=" in condition: + operator = "!=" + elif "==" in condition: + operator = "==" + else: + return + var1, var2 = condition.split(operator) + var1 = _set_value_from_formula(var1, context) + var2 = _set_value_from_formula(var2, context) + res = var1 == var2 + if operator == '!=': + res = not res + return res + +def _format_value(value, default_value): + value = unicode(value) if value else default_value + #if hasattr(value, 'strftime'): + # value = value.strftime() + return value + +def ooo_replace(infile, outfile, context, default_value=''): + inzip = ZipFile(infile, 'r', ZIP_DEFLATED) + outzip = ZipFile(outfile, 'w', ZIP_DEFLATED) + content = ElementTree(fromstring(inzip.read('content.xml'))) + missing_keys = set() + for xp in ('variable-set', 'variable-get'): + for p in content.findall(".//"+OOO_NS+xp): + name = p.get(OOO_NS+"name") + if name in context: + value = context[name] + p.text = _format_value(value, default_value) + else: + if default_value != None: + p.text = default_value + missing_keys.add(name) + for p in content.findall(".//"+OOO_NS+"conditional-text"): + condition = p.get(OOO_NS+"condition") + res = 'true' if _parse_condition(condition, context) else 'false' + value = p.get(OOO_NS+'string-value-if-' + res) + value = _format_value(value, default_value) + if value.strip() in context: + value = context[value.strip()] + p.text = value + for f in inzip.infolist(): + if f.filename == 'content.xml': + s = StringIO() + content.write(s) + outzip.writestr('content.xml', s.getvalue()) + else: + outzip.writestr(f, inzip.read(f.filename)) + inzip.close() + outzip.close() + return missing_keys + +if __name__ == '__main__': + infile = "../archaeological_files/tests/AR_dossier_DRAC_modele_ishtar_1-MOD.odt" + outfile = "../archaeological_files/tests/AR_dossier_DRAC_modele_ishtar-test.odt" + rep = {"file_incharge_surname":u"Yann", + "file_incharge_name":u"Le Jeune", + "fileact_ref":u"ref"} + ooo_replace(infile, outfile, rep, default_value="") |